Redirected
9,043 views
11 votes
11 votes

Consider the following Pseudo code

main()  
{
    int t1=0,t2=0,t3=0;
    t1=fork();
    t2=fork();
    if(t1!=0)
    {
        t3=fork();
        printf("0");
    }  
}  

Find the total number of processes that will be created by the above program execution. 

8 Answers

Best answer
28 votes
28 votes

At the call of fork, a child process is created which executes the same code of the parent from that point. The return value of fork is 0 for the child and is child pid (not 0) for the parent and this value is used to distinguish between child-parent while writing code using fork. 

Thus 5 child processes are created. Since the question asks for "total number of processes created" we must include parent also making this 6 processes in total, 

selected by
8 votes
8 votes

Plz have a look at this :

For more conceptual clarity , u can refer the following example on fork() system call implementation.

https://www.youtube.com/watch?v=WcsZvdlLkPw

2 votes
2 votes
child process gets 0 as process id and parent gets child's process id as its process id. so t1 's child process won't execute 3rd fork(). therefore 4 times 0 will be printed and total process = 6.
2 votes
2 votes
no of new child process reaed are 5 total new proocesses created are 5

here main point to be noted is value of t1 of parent process is sharedd by its children

WHEN A PROCESS IS CREATED FOR PARENT IT RETURNS THE ID OF THE CHILD PROCESS

FOR CHILD PROCESS IT RETURNS THE VALUE 0
Answer:

Related questions

1 votes
1 votes
2 answers
1
1 votes
1 votes
1 answer
3
Mak Indus asked Jan 11, 2019
2,879 views
How many processes will be created when we run the below program?main ( ){Printf (“Hi”);fork ( );Printf (“Hello”);fork ( );fork ( );}(a) 3 ...