CS FundamentalsHardGATE CS
How many times is "x" printed? (Every fork() succeeds.)
How many times is "x" printed? (Every fork() succeeds.)
int main() {
pid_t a = fork();
pid_t b = fork();
if (a > 0 && b > 0)
fork();
printf("x\n");
}- A. 5
- B. 4
- C. 6
- D. 8
Correct answer: A. 5
After two forks there are 4 processes; only the original (a > 0 and b > 0) forks again, adding 1. Total 5.
More Operating Systems questions
- 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…
- A parent process exits without calling wait() while its child is still running. What happens to the child?