Question: Operating system help. Please explain the answer Please explain int main(int argc, char ** argv) { int child = fork(); int x = 5; int

Operating system help. Please explain the answer Please explain

int main(int argc, char ** argv) {

int child = fork();

int x = 5; int y =0;

if (child == 0){

x+=5;

y= x;

} else {

child = fork();

x += 10;

if (child){

x += y;

x +=5;

}

}

}

Question 1: In this program, how many independent instances of x are there?

Questions 2: What is the final value of x in the last child created?

#define NTHREADS 10

main (){

thread_t threads[NTHREADS];

for (i = 0; i < NTHREADS; i++)

thread_create(&(threads[i]), &go, i);

for(i = 0; i < NTHREADS; i++){

exitValue = thread_join(threads[i]);

printf("Thread %d returned with %ld ", i, exitValue);

}

printf("Main thread done. ");

}

void go (int n) {

printf("Hello from thread %d ", n);

thread_exit(100 + n);

// Not reached

}

Question 3: What is the maximum number of threads running when thread 5 prints "Hello from thread 5"?

Question 4: What is the minimum number of threads running when thread 5 prints "Hello from thread 5"?

Question 5: It is possible that

"Thread 8 returned 108"

could be printed before

"Thread 5 returned 105". is printed?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!