Analyze the C program and state what the output of the program should be. Treat
ID: 3666211 • Letter: A
Question
Analyze the C program and state what the output of the program should be. Treat the C program primarily as pseudo-code.
1.)Process Creation: What is the output of the following C program? Explain.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value =5;
int main()
{
pid_t pid;
pid = fork();
if (pid ==0){
pid_t pid2;
pid2 = fork();
value += 15;
if (pid2 == 0){
value += 15;
printf("CHILD 2: value = %d ", value);
}
else if (pid2>0){
wait(NULL);
printf("CHILD 1: value = %d ", value);
}
}
else if (pid >0){
wait(NULL);
printf("PARENT: value = %d ", value);
exit(0);
}
}
Explanation / Answer
output:
CHILD 2: value = 5
CHILD 1: value = 5
PARENT: value =5,
As the value has assigned with 5 so the all fork class will get same value.