1,391 views

A process executes the following segment of code :

for(i = 1; i <= n; i++)     fork ();     fork ();

The number of new processes created is

@akash.dinkar12 @ Dharmendra Lodhi Thank you bhaijaan. :)

@iarnav

So we get total new child processes = 2^n-1 * 1 = 2^n - 1

By the last outer fork, the processes up to the point are doubled ===> 2 * (2n-1).

and the parent process also get doubled ===> 1 more child process

∴ Total new processes = 2 * (2n-1) + 1 = 2.2n - 2 + 1 =  2n+1 - 1

thanks bro!

1. When the loop executes, fork called n times so,

Total no of new processes = $2^n -1$

2.Then Loop exits and next fork() executes( which is out of loop) then,

Total no of new processes becomes = 2 x $2^n -1$ = $2^n^+^1 -1$

Example: Let n = 2 then Loop runs 2 times and fork() makes 3 new processes(exclude parent) and After the loop exits

Next fork() executes makes two new processes of each previous 3 new processes and one new process from parent .so, total no of new processes = 7

1 vote