• edited by
19,892 views
51 51 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
67 67 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

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

Answer:
Position:
Show:

Related questions

16 16 votes
4 answers 4 answers
4.9k
4.9k views
gatecse asked Sep 29, 2014
4,933 views
Given the sequence of terms, $\text{AD CG FK JP}$, the next term is$\text{OV}$$\text{OW}$$\text{PV}$$\text{PW}$
18 18 votes
2 answers 2 answers
5.4k
5.4k views
gatecse asked Sep 29, 2014
5,384 views
Which one of the following options is the closest in meaning to the word given below?Mitigate DiminishDivulgeDedicateDenote
18 18 votes
2 answers 2 answers
5.2k
5.2k views
gatecse asked Sep 29, 2014
5,202 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
11.0k
11.0k views
Arjun asked Sep 25, 2014
11,012 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} \...