CS FundamentalsHardGATE CS
How many times is "x" printed? (Every fork() succeeds; output is not buffered.)
How many times is "x" printed? (Every fork() succeeds; output is not buffered.)
int main() {
for (int i = 0; i < 3; i++) {
fork();
if (i == 1) printf("x\n");
}
}- A. 4
- B. 8
- C. 2
- D. 6
Correct answer: A. 4
The print runs once in each process alive right after the second fork, i.e. 2² = 4 processes. The third fork happens after the print, so its children print nothing.
More Operating Systems questions
- 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?
- Disk cylinders 0–199; the head is at 50. Requests in arrival order: 82, 170, 43, 140, 24, 16, 190. What is the total head movement under…