İhsan Doğramacı Bilkent University
CS 342
CS342 Operating Systems
Fall 2024
Homework #2
Assigned: Oct 23, 2024.
Due date: Nov 7, 2024. Document version: 1.0
Q1. We have the following program. What are the pairs of values printed out
by this program?
int a = 3200;
int b = 1000;
int ret;
int main()
{
ret = fork();
a = a + 50;
if (re
...[Show More]
CS342 Operating Systems
Fall 2024
Homework #2
Assigned: Oct 23, 2024.
Due date: Nov 7, 2024. Document version: 1.0
Q1. We have the following program. What are the pairs of values printed out
by this program?
int a = 3200;
int b = 1000;
int ret;
int main()
{
ret = fork();
a = a + 50;
if (ret == 0) {
a = a * 3;
b = b + 150;
printf ("%d %d\n", a, b);
if (fork() == 0) {
a = a + 2000;
printf ("%d %d\n", a, b);
exit(0);
}
b = b + 115;
}
else {
a = a + 210;
b = 3 * b;
printf ("%d %d\n", a, b);
b = b + 90;
}
printf ("%d %d\n", a, b);
} Q
2. Consider the following program. What are the values that are printed out
by this program?
unsigned long x = 1000;
unsigned long y = 2000;
unsigned long z = 400;
void * func1(void *p)
{
unsigned long x, z;
x = 7000;
y = 4000;
z = *((unsigned long *)p);
z = z + 5000;
*((unsigned long *)p) += 500;
printf ("%lu %lu %lu\n", x, y, z);
2
pthread_exit(NULL);
}
int
main()
{
pthread_t tid;
unsigned long a = 3000;
pthread_create (&tid, NULL, &func1, (void *)&a);
pthread_join(tid, NULL);
printf ("%lu %lu %lu\n", x, y, z);
} Q
3. Consider the following program. Find out the triples printed out by this
program. What can you say about their order?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
unsigned long a=100;
unsigned long b=200;
#define COUNT 5
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void * func(void *p)
{
unsigned long a = 50;
pthread_mutex_lock(&lock);
a = a + 20;
b = b + 30;
pthread_mutex_unlock(&lock);
printf ("%lu %lu %lu\n", a, b, (unsigned long)p);
pthread_exit(NULL);
}
int main()
{
pthread_t tids[COUNT];
unsigned long k;
int ret;
for (k = 0; k < COUNT; ++k)
pthread_create (&(tids[k]), NULL, &func, (void *) (k+1));
for (k = 0; k < COUNT; ++k)
pthread_join(tids[k], NULL);
printf ("%lu %lu %lu\n", a, b, k);
exit (0);
}
[Show Less]
Access Full Document
Instant download after payment
Card Payments
₿
Crypto Accepted