in Operating System edited by
1,051 views
0 votes
0 votes
A process executes the following segment of code:

int main() {

      fork();

      fork() && fork();
}

The number of new processes created is __________

Can someone pls explain me the solution
in Operating System edited by
1.1k views

4 Comments

@srestha Ma'am please help us in getting a clear and accurate explanation for this problem.

0
0
0
0
Thanks.
0
0

2 Answers

0 votes
0 votes
yes i'm also getting 5.

number the forks: 1.fork()

                             2.fork() && 3.fork()

let's say there is process P. P-parent process, C child process

1.fork(): P,C

2.fork(): P,C1,C,C2 out of these processes, process id of P and C are non-zero and process id of C1 and C2 are 0.

               C1 is the child process created after forking P. C2 is the child process after forking C.

3.fork():{only P and C will execute, as only their process id's are non-zero} P,C3,C,C4

             C3 is the child process created after forking P. C4 is the child process created after forking C

finally count the number of child processes created, which is equal to 5.

1 comment

5 child process be there

 

0
0
0 votes
0 votes

at first we have  to learn how the && is executed.

exp=exp1&&exp2

case1

exp1==0 then exp2 will not be executed and overall value of the exp=0

case2

exp2!=0 then exp2 will be executed and overall value will be depends on the value of exp2.

Now here fork() returns pid of child process to parent process which is not equal to 0 and 0 to the child process

In below diagram I have explained how the code is executed.So the number of new processes will be 5.

Related questions

3 votes
3 votes
1 answer
2