CS FundamentalsHardGATE CS
How many processes print "x" (including the original)? (Every fork() succeeds.)
How many processes print "x" (including the original)? (Every fork() succeeds.)
int main() {
for (int i = 0; i < 4; i++)
if (fork() != 0)
break;
printf("x\n");
}- A. 16
- B. 4
- C. 8
- D. 5
Correct answer: D. 5
Each parent breaks out after one fork; only the newest child keeps looping. This builds a chain: original + 4 children = 5 processes, not 2⁴ = 16.
More Operating Systems questions
- How many times is "x" printed? (Every fork() succeeds.) c int main() { pidt a = fork(); pidt b = fork(); if (a > 0 && b > 0) fork()…
- How many times is "x" printed? (Every fork() succeeds.) c int main() { if ((fork() && fork()) || fork()) fork(); printf("xn"); }
- How many times is "x" printed? (Every fork() succeeds; output is not buffered.) c int main() { for (int i = 0; i < 3; i++) { fork(); if (i…
- What is the sum of all values printed? (Every fork() succeeds.) c int main() { int c = 0; for (int i = 0; i < 3; i++) if (fork() == 0)…
- A process calls fork() three times in sequence and every child also continues the code. Afterwards, how many processes exist and how many…
- What is printed? (fork succeeds.) c int x = 10; int main() { if (fork() == 0) { x += 5; printf("%d ", x); } else { wait(NULL); x -= 5…