Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program shown uses the Pthreads API. What would be the output from the progr

ID: 3851751 • Letter: T

Question

The program shown uses the Pthreads API. What would be the output from the program at LINE C and LINE P?

#include <pthread.h>

#include <stdio.h>

#include <types.h>

int value = 0;

void *runner(void *param); /* the thread */

int main(int argc, char *argv[])

{

pid t pid;

pthread t tid;

pthread attr t attr;

pid = fork();

if (pid == 0) { /* child process */

pthread attr init(&attr);

pthread create(&tid,&attr,runner,NULL);

pthread join(tid,NULL);

printf("CHILD: value = %d",value); /* LINE C */

}

else if (pid > 0) { /* parent process */

wait(NULL);

printf("PARENT: value = %d",value); /* LINE P */

}

}

void *runner(void *param) {

value = 5;

pthread exit(0);

}

Explanation / Answer

Whenever you call pid=fork() as a system call then pid will be greater then zero and the creation of parent process happens , So it will enter to the parent process but inside it doesn't wait for so because of wait(NULL) it won't print anything and child process starts its execution and output of the line C will be 5 as the runner function was called.

And again it enters in the block of parent procss and printing 5 as the output of line P gets exit() call and terminates the program's execution without bothering about the child's execution .