edited by
19,446 views
49 49 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$

6 Answers

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

 
edited by
18 18 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

3 3 votes

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

Answer:
Position:
Show:

Related questions

15 15 votes
4 answers 4 answers
4.8k
4.8k views
gatecse asked Sep 29, 2014
4,838 views
Given the sequence of terms, $\text{AD CG FK JP}$, the next term is$\text{OV}$$\text{OW}$$\text{PV}$$\text{PW}$
17 17 votes
2 answers 2 answers
5.3k
5.3k views
gatecse asked Sep 29, 2014
5,311 views
Which one of the following options is the closest in meaning to the word given below?Mitigate DiminishDivulgeDedicateDenote
17 17 votes
2 answers 2 answers
5.1k
5.1k views
gatecse asked Sep 29, 2014
5,115 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 ...
40 40 votes
6 answers 6 answers
10.8k
10.8k views
Arjun asked Sep 25, 2014
10,815 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} \...