# Andrew S. Tanenbaum (OS) Edition 4 Exercise 2 Question 39 (Page No. 177)

1 vote
567 views

Consider the following piece of C code:

void main( ) {

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

} 

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

Total child process = 2^n -1  , where n is number of time fork will call

2^2-1 =3
here fork() fnction is executing twice and n=2.

we the nos of child processes are  (2^n)-1= (2^2)-1=4-1=3
Generally when a fork is called for n times then total number of child processes that is generated is 2^(n)-1

In the above problem it is called 2 times so it generates 2^(2)-1 =3 number of child processes will be generated.
No of child processes created is 2^n-1.

Here n is the number of times fork is called.

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)

2^2-1 =3

Hence ans : 3

## Related questions

1
115 views
Five batch jobs. $A$ through $E$, arrive at a computer center at almost the same time. They have estimated running times of $10, 6, 2, 4,$ and $8$ minutes. Their (externally determined) priorities are $3, 5, 2, 1,$ and $4,$ respectively, with $5$ being the highest ... the CPU. For $(b)$ through $(d),$ assume that only one job at a time runs, until it finishes. All jobs are completely CPU bound.
Five jobs are waiting to be run. Their expected run times are $9, 6, 3, 5,$ and $X$. In what order should they be run to minimize average response time? $($Your answer will depend on $X.)$
Measurements of a certain system have shown that the average process runs for a time $T$ before blocking on $I/O$. A process switch requires a time $S$, which is effectively wasted (overhead). For round-robin scheduling with quantum $Q$, give a formula for the CPU efficiency for each of the following: $Q = \infty$ $Q > T$ $S < Q < T$ $Q = S$ $Q\: \text{nearly}\: 0$