edited by
12,797 views
34 votes
34 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$
edited by

4 Answers

Best answer
50 votes
50 votes
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
15 votes
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

Answer:

Related questions

9 votes
9 votes
4 answers
1
gatecse asked Sep 29, 2014
2,662 views
Given the sequence of terms, $\text{AD CG FK JP}$, the next term is$\text{OV}$$\text{OW}$$\text{PV}$$\text{PW}$
13 votes
13 votes
1 answer
2
gatecse asked Sep 29, 2014
2,795 views
Which one of the following options is the closest in meaning to the word given below?Mitigate DiminishDivulgeDedicateDenote
10 votes
10 votes
2 answers
3
gatecse asked Sep 29, 2014
2,939 views
Choose the most appropriate alternative from the options given below to complete the following sentence:Despite several _________ the mission succeeded in its attempt to ...
25 votes
25 votes
5 answers
4
Arjun asked Sep 25, 2014
6,240 views
What is the minimal form of the Karnaugh map shown below? Assume that $X$ denotes a don’t care term $\bar{b} \bar{d}$$ \bar { b } \bar { d } + \bar{b} \bar{c} $$ \bar{b...