GATE 2019 CS – Question 27
The following C program is executed on a Unix/Linux system:
#include <unistd.h>
int main()
{
int i;
for (i=0; i<10; i++)
if (i%2 == 0) fork();
return 0;
}The total number of child processes created is ________.
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 31
Explanation
`fork()` is called only when `i` is even: $i=0,2,4,6,8$, which is 5 calls. Each call makes every existing process split in two, so the number of processes doubles.
After 5 doublings there are $2^5=32$ processes in total. One of them is the original parent, so the number of **child** processes is $32-1=\mathbf{31}$.