CS FundamentalsVery hardGATE CS
How many times is "x" printed? (Every fork() succeeds.)
How many times is "x" printed? (Every fork() succeeds.)
int main() {
if ((fork() && fork()) || fork())
fork();
printf("x\n");
}- A. 9
- B. 7
- C. 8
- D. 12
Correct answer: C. 8
(fork() && fork()) || fork() leaves 5 processes; for each, the condition is true unless it is the child of the last fork(), which got 0. Four of the five have a true condition and fork once more: 5 + 4 = 8.
More Operating Systems questions
- 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…
- A parent process exits without calling wait() while its child is still running. What happens to the child?
- After a successful exec() call, which of these is preserved from the calling process?