CS FundamentalsModerateGATE CS · Amazon/Microsoft SDE MCQ
Which letters are printed, and how many times each? (Every fork() succeeds, and each print is an unbuffered write.)
Which letters are printed, and how many times each? (Every fork() succeeds, and each print is an unbuffered write.)
int main() {
if (fork() == 0) {
if (fork() == 0) printf("C");
else printf("B");
} else {
fork();
printf("A");
}
}- A. A once, B once, C once
- B. A twice, B twice, C once
- C. A twice, B once, C once
- D. A once, B twice, C twice
Correct answer: C. A twice, B once, C once
The original takes the else branch and forks, so it and its new child each print A. The first child forks again: it prints B and its own child prints C.
More Operating Systems questions
- Periodic tasks T1 (execution 2, period 5) and T2 (execution 4, period 7), deadlines equal to periods. Which statement is correct?
- Each process spends 50% of its time waiting for I/O, independently of the others. Under the simple model CPU utilisation = 1 − pⁿ, what is…
- Memory access takes 100 ns. A page fault occurs once per million accesses; servicing it takes 8 ms if the victim page is clean and 20 ms…
- An inode has 12 direct pointers, one single-indirect and one double-indirect pointer; blocks are 4 KB and pointers 8 bytes. What is the…
- A parent forks 3 children that each call exit(0) immediately; the parent then sleeps for a minute without calling wait() and without…
- Why is a test-and-test-and-set spinlock (spin on a plain read, and only try the atomic exchange when the lock looks free) usually faster…