CS FundamentalsHardGATE CS
How many times is "x" printed? (Every fork() succeeds; output unbuffered.)
How many times is "x" printed? (Every fork() succeeds; output unbuffered.)
int main() {
for (int i = 0; i < 2; i++) {
if (fork() == 0) continue;
printf("x");
}
}- A. 2
- B. 3
- C. 4
- D. 6
Correct answer: B. 3
In each iteration every existing process forks; the parent prints and the child continues. Iteration 0: 1 parent prints; iteration 1: 2 processes each fork and print once. Total = 1 + 2 = 3.
More Operating Systems questions
- What values are printed (in any order)? (Every fork() succeeds.) c int main() { int x = 1; if (fork() == 0) { x = 2; if (fork() == 0) x =…
- How many processes are created in total, including the original? (Every fork() succeeds.) c int main() { for (int i = 0; i < 3; i++) if…
- Processes (arrival, burst): P1 (0, 7), P2 (2, 3), P3 (3, 1), P4 (6, 2). SRTF is used and every switch to a DIFFERENT process costs 1 ms…
- Processes on one CPU, Round Robin quantum 2: A arrives at 0 and needs CPU 3, then I/O 2, then CPU 1; B arrives at 1 and needs CPU 4. A…
- Processes (arrival, burst): P1 (0, 2), P2 (1, 9), P3 (2, 1), P4 (3, 1), P5 (4, 1). Rank FCFS, non-preemptive SJF, SRTF and RR (q = 1) by…
- Reference string: 1, 2, 3, 4, 5, 3, 4, 1, 6, 7, 8, 7, 8, 9, 7, 8, 9, 5, 4, 5, 4, 2. With 4 frames (initially empty), how many page faults…