C Programming, Linux, Unix If you already answer this question, please I need al
ID: 3794661 • Letter: C
Question
C Programming, Linux, Unix
If you already answer this question, please I need alternative answer from someone else. Thanks.
Question: Including the initial parent process, how many processes are created by the program? And what is the value of i before each process terminates? Please draw the parent-child process hierarchy to justify your answer.
#include<stdio.h>
#include<unistd.h>
int main() {
int i = 0;
if (fork() == 0) {
++i;
if (fork() != 0)
i = 3;
else ++i; fork();
} else {
i = 5;
if (fork() == 0)
++i;
}
return 0;
}
Explanation / Answer
Lets get the parent child and process by printing the value of i .please check at end
#include <stdio.h>
int main()
{
int i = 0;
if (fork() == 0) {
++i;
printf("%d ",i);
if (fork() != 0){
i = 3;
printf("%d ",i);
}
else {
++i; fork();
printf("%d ",i);
}
} else {
i = 5;
if (fork() == 0)
++i;
printf("%d ",i);
}
return 0;
}
output
5 1 6 3 2 2
Total Number of Processes = 2n where n is number of fork system calls. So here n=3, 2^4 = 16 .there should be 16 process but we have conditions of if and else which reduced it to one parent and 5 child process.the value of i is in output before process terminate are 1 6 3 2 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.