GATE 2024 CS (CS1) – Question 57
Consider the code:
int x = 3;
while (x > 0) {
fork();
printf("hello");
wait(NULL);
x--;
}The total number of times the printf statement is executed is _______
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 14
Explanation
Let f(x) be prints for a process with counter x: after fork, both parent and child print once and then continue with x−1, so f(x)=2(1+f(x−1)) with f(0)=0. f(1)=2, f(2)=6, f(3)=14.