The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+19 votes
2.2k views

A process executes the code

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

The total number of child processes created is

  1. $3$
  2. $4$
  3. $7$
  4. $8$
asked in Operating System by Boss (18.3k points)
edited by | 2.2k views

3 Answers

+29 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$.
answered by Veteran (369k points)
edited by
+2

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

+12 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

answered by Active (1k points)
0
at third fork child_111 will be created :P (pun intended)
+1 vote

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

answered by (427 points)
0
Answer is c there will be 7 child and 1 parent
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

44,284 questions
49,774 answers
164,287 comments
65,856 users