CS FundamentalsModerateGATE CS
How many times is "x" printed? (Every fork() succeeds.)
How many times is "x" printed? (Every fork() succeeds.)
int main() {
if (fork() == 0) {
fork();
fork();
} else {
fork();
}
printf("x\n");
return 0;
}- A. 7
- B. 6
- C. 3
- D. 9
Correct answer: B. 6
The child of the first fork makes 2 more forks (4 processes); the parent makes 1 more (2 processes). Total 6 processes, each printing once.
More Operating Systems questions
- How many processes print "x" (including the original)? (Every fork() succeeds.) c int main() { for (int i = 0; i < 4; i++) if (fork() !=…
- 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…