Question: #include #include #include #include int value = 0; void *runner(void *param); int main() { int pid; pthread_t tid; pthread_attr_t attr; pid = fork(); if (pid

#include

#include

#include

#include

int value = 0;

void *runner(void *param);

int main()

{

int pid;

pthread_t tid;

pthread_attr_t attr;

pid = fork();

if (pid == 0) {

pthread_attr_init(&attr);

pthread_create(&tid,&attr,runner,NULL);

pthread_join(tid,NULL);

printf("A: value = %d ", value);

}

else if (pid > 0) {

wait(NULL);

printf("B: value = %d ", value);

}

}

void *runner(void *param)

{

value = 5;

pthread_exit(0);

}

Given the above code, answer the following three questions and one modification of the code.

1. What does this program output at A: and B:? (A: value = 5, B: value = 0)

2. Mark each line of code in the program as either (1) executed by parent process, (2) executed as child process, (3) executed by a new thread spawned by the parent or (4) executed by a thread spawned by the child process.

3. Provide a one line description (as an inline comment) for each program line above. Make sure you concisely but clearly describe what the program is doing at each line.

4. Modify the code so that both threads keep printing the same line, so that the terminal show A and B interleavely.

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!