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
The fork is executed for $i=0,2,4,6,8$, i.e. 5 times, and each execution doubles the number of processes. So there are $2^5=32$ processes in total, and 31 of them are children.