ago
87 views

1 Answer

5 votes
5 votes

Given code : 

main()

{

if(!fork()) // FORK-1

{

    if(!fork()) // FORK-2

    fork(); // FORK-3

}

fork(); // FORK-4

}


NOTE : When fork() command is called then copy of the parent process will be created. 

Let, P be the parent process and C be its child, then fork() will return child Id(Some non-zero

value) to parent process and 0 will be returned to Child Process C.


NOTE : If(Non-zero value) --> TRUE | If(Zero Value) --> FALSE

If(!Non-zero value) --> FALSE | If(!Zero Value) --> TRUE


Let P be the parent process which start execution of given code :

1. P will call FORK-1. 

P will get Process ID of child which is non-zero value and negation of that will be FALSE. So, P will call

FORK-4 and will go out

But, when FORK-1 is called Child A will get 0 or FALSE and negation of FALSE will be TRUE. So, A will

go inside $1^{st}$ If clause.

 

2. For the execution of $2^{nd}$ we can use the same understanding that we have developed above and

tree will look something like this : 

Here, A will be the parent and after calling FORK-2, A will execute FORK-4 and will go out. Whereas, After

calling FORK-2, D will be it's child and D will also execute FORK-3.

 

3. Now D & F both will execute FORK-4 & tree will look something like this :


All the leaf node except P are child processes.

Parent Process : P

Child Process : A, B, D, E, F, G, H 

No. of Child Processes : 7

Related questions

436
views
1 answers
2 votes
Sparkboy asked Apr 4
436 views
Consider the following pieces of codes for fork( ) system call. Solve and explain how many child processes are created upon execution of this program? Snippet 1: ... 0; }Learning Outcomes. Understand the working of fork ( ) system call.
1.2k
views
4 answers
2 votes
Philosophical_Virus asked Dec 10, 2023
1,184 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
866
views
1 answers
0 votes
Erwin Smith asked Apr 11, 2023
866 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not possible?2,4,1,701,7041,2,4,704,7012,704,4,701,11,704,2,4,701
879
views
0 answers
1 votes
Souvik33 asked Nov 28, 2022
879 views
MSQ Consider the following statements, which one of the following is/are TRUEIn fork() system call, child process inherits all the open file descriptors of ... of the parent process User level threads shares the code segment of the process