Anonymous user menu

number of process created?

number of process created?

5Comments
Dhrupit Dave @dhrupitdave
30 Oct 2014 05:41 pm

Here it should be 3.(because of 2 fork will execute). The IF condition will be violated here because t1 will be "0" since it a newly created process.

Mahesh Kumar @maheshkumars
30 Oct 2014 05:57 pm
5 process will be created.total 6.
Vivek Vikram Singh @vivek14
10 Nov 2014 10:27 pm

Ok let clarify each thing carefully. I will be enumerating fork calls as 1st ,2nd and 3rd in order. 

1st I will tell u execution of parent only.
Parents:
When the parent excutes 1st fork, parent's t1 will receive PID1 which is non zero, whereas Child C1's t1 will be having 0 and all children of C1 will use t1=0.
When parent executes 2nd fork,parent's t2=PID2, child C2 will be created and it will have value of t2=0.parent's t1 will be PID1 which will be passed to C2.
Now parent's t1 =PID1 which is non zero ,so it will execute 3rd fork and create process C3. t3 of parent will be PID3 and non zeros,where as C3's t3 will be Zero. 

Children:

Now turning to children's fork execution part,C1 will execute fork and create process C11. C1 's t2 will be PID11 and non zero whereas C11's t2 will be zero.
It is worth remembering that C1 and C11 will be using value of t1 of C1 ,which is zero, so neither of them will be able to execute 3rd fork which is inside "if" clause. they will not call neither fork nor print and they terminate. Here we count 2 processes. C1 and C11

C2 is having t1=PID1 which is non zero, whereas its t2=0. Now C2 will be able to execute 3rd fork and create process C21. C2's t3 = PID21 non zero,whereas C21's t3 =0. Now C21 will call print function and terminate. same with C2.here we count 2 processes C2 and C21.

total process , Parent, C1,C2,C3, C11,C21 = 6.
Please let me know if I am unclear.

Vivek Vikram Singh @vivek14
30 Oct 2014 09:07 pm

Ok let clarify each thing carefully. I will be enumerating fork calls as 1st ,2nd and 3rd in order. 

1st I will tell u execution of parent only.
Parents:
When the parent excutes 1st fork, parent's t1 will receive PID1 which is non zero, whereas Child C1's t1 will be having 0 and all children of C1 will use t1=0.
When parent executes 2nd fork,parent's t2=PID2, child C2 will be created and it will have value of t2=0.parent's t1 will be PID1 which will be passed to C2.
Now parent's t1 =PID1 which is non zero ,so it will execute 3rd fork and create process C3. t3 of parent will be PID3 and non zeros,where as C3's t3 will be Zero. 

Children:

Now turning to children's fork execution part,C1 will execute fork and create process C11. C1 's t2 will be PID11 and non zero whereas C11's t2 will be zero.
It is worth remembering that C1 and C11 will be using value of t1 of C1 ,which is zero, so neither of them will be able to execute 3rd fork which is inside "if" clause. they will not call neither fork nor print and they terminate. Here we count 2 processes. C1 and C11

C2 is having t1=PID1 which is non zero, whereas its t2=0. Now C2 will be able to execute 3rd fork and create process C21. C2's t3 = PID21 non zero,whereas C21's t3 =0. Now C21 will call print function and terminate. same with C2.here we count 2 processes C2 and C21.

total process , Parent, C1,C2,C3, C11,C21 = 6.
Please let me know if I am unclear.

Parimal Andhalkar @parimal_andhalkar
4 Nov 2014 04:48 pm

if n fork();

no. of processes = 2n-1

there are 2 fork();  in program,

ans = 22-1 = 3