in Operating System
1,391 views
3 votes
3 votes

A process executes the following segment of code :

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


The number of new processes created is

in Operating System
by
1.4k views

4 Comments

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

0
0

@iarnav

we will get child processes as 2^n-1 

and from the last outer fork we get 2^1 - 1 = 1 child process.

 

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

1
1
thanks bro!
0
0

1 Answer

2 votes
2 votes

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$ =  

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 

Related questions

2 votes
2 votes
2 answers
2