CS FundamentalsHardGATE CS
What values are printed (in any order)? (Every fork() succeeds.)
What values are printed (in any order)? (Every fork() succeeds.)
int main() {
int x = 1;
if (fork() == 0) {
x *= 2;
if (fork() == 0) x *= 3;
}
printf("%d ", x);
}- A. 1, 2, 6, 6
- B. 6, 6, 6
- C. 1, 2, 6
- D. 1, 6
Correct answer: C. 1, 2, 6
The parent keeps x = 1. The first child doubles to 2 and forks; its child multiplies its own copy by 3 to get 6, while it keeps 2. Each process has a private copy of x.
More Operating Systems questions
- 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…
- Under which condition does the clock (second-chance) algorithm make exactly the same replacement decisions as FIFO?