in Operating System edited by
8,480 views
28 votes

A process executes the code

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

The total number of child processes created is

  1. $3$
  2. $4$
  3. $7$
  4. $8$
in Operating System edited by
by
8.5k views

9 Comments

Fork() call creates duplicate process but executes onward from line
where it is created not the whole program again.
0

$Ans: 7$


6

@Kushagra गुप्ता

What is the significance of this diagram?

0
It's just one of the approach to solve fork related questions which I am comfortable with. I have commented on some more questions in a similar way. You can go through them in 'fork' tag.
0
Okay. So can you explain what you did in the diagram?

I know the other way but I want to know this.
0

$P$ call first $fork()1$ which creates a child $c1$ but $P$ still remains. 

Now both $P$ and $c1$ will call second $fork()-2$ which creates another child for P named as $c2$ but now $c1$ which is a child of $P$ while calling fork will behave like a parent and will create his own child which I named as $c3$. Now each one of them i.e $P,c2,c1,c3$ again call $fork()$ and follow the same approach.

Go through these question once in which I explained not in a detail way but you might get a good idea:

https://csedoubts.gateoverflow.in/9906/ace-test-series-test-50-os-fork?show=9935#c9935

https://csedoubts.gateoverflow.in/8385/operating-system-test-series?show=9943#c9943 

3
Ok. Thanks.
0
But not a good way of solving this question with respect to exam, right?
0

I think It depends. It suits me :) 

If not yours What will be your approach to tackle this: https://csedoubts.gateoverflow.in/8385/operating-system-test-series?show=9943#c9943

Let me know.

0

Subscribe to GO Classes for GATE CSE 2022

3 Answers

42 votes
 
Best answer
At each fork() the no. of processes becomes doubled. So, after $3$ fork calls, the total no. of processes will be $8$. Out of this $1$ is the parent process and $7$ are child processes. So, total number of child processes created is $7$.
edited by
by

1 comment

edited by

the total no of child =2n-1 hence 23-1=7

2
15 votes

How the fork() system call works:  It is used to create child process. It takes no argument and returns a process ID. Both parent and child will execute the instruction next to fork(). To differentiate between parent and child process we have to examine the value of process id returned by fork().

  • fork() returns negative value if it is unable to create a child process.
  • fork() returns 0 to the child process.
  • fork() returns positive int indicating pid of child process to parent process.

/* code...*/

pid_t pid;

fork(); // create a process

pid = getpid();

if(pid == -1) { // failed to create child process }

else if(pid == 0) { // code for child process }

else { // code for parent process }.

Now let's come to the question.

fork(); // child_1 will be created

fork(); // child_2 and child_11 will be created.

fork(); // child_3 , child_12, child_21, child_112 will be created

So total number of child process created is 7

1 comment

at third fork child_111 will be created :P (pun intended)
0
1 vote

2-1 =8-1 =7(ans)

1 comment

Answer is c there will be 7 child and 1 parent
0
Answer:

Related questions