in Operating System
3,449 views
5 votes
5 votes

Consider the following piece of C code:
 

void main( ) {

      fork( );
      fork( ); 
      exit( ); 

} 

How many child processes are created upon execution of this program?

in Operating System
by
3.4k views

8 Answers

0 votes
0 votes

Given statements ,

  1. void main( ) {
  2.       fork( );
  3.       fork( );
  4.       exit( );
  5. }

At line #2,

           one child process will be created.

Now we have total 2 processes, 1 child process and another parent,

And the next statement to execute is line#3 in both processes.

 

At line #3


Now both processes(1 parent and 1 child process)  executes line#3 ,  and this will create two child processes.

One created by parent , And another by child (which was earlier created at line #2).


So,

total child processes are 1(line#2) + 2(line#3) → 3 child processes

 

 

General formula for calculating no. of child processes  (2^n) -1  (unless the fork is not called upon any condition)

0 votes
0 votes
2^2-1 =3

Hence ans : 3
by
0 votes
0 votes
yes  i am agree  with that fork is always creted process 2^n-1.(where n is the number of fork ()) and  finaly put the value of n and calculated the number of  child process … so calculated  answer is 3..
0 votes
0 votes

n=no of fork call

formula = 2^n-1

Related questions