CS FundamentalsVery hardGATE CS · Amazon/Microsoft SDE MCQ
How many numbers are printed, and what is their sum? (Every fork() succeeds, and each print is an unbuffered write.)
How many numbers are printed, and what is their sum? (Every fork() succeeds, and each print is an unbuffered write.)
int main() {
for (int i = 0; i < 3; i++) {
printf("%d", i);
fork();
}
}- A. 14 numbers, sum 20
- B. 7 numbers, sum 7
- C. 8 numbers, sum 12
- D. 7 numbers, sum 10
Correct answer: D. 7 numbers, sum 10
The print comes before each fork: at i = 0 one process prints 0, at i = 1 two print 1, at i = 2 four print 2. That is 7 numbers summing to 0 + 2 + 8 = 10. With stdout buffered to a pipe the copies would multiply, which is why unbuffered writes are assumed.
More Operating Systems questions
- Which letters are printed, and how many times each? (Every fork() succeeds, and each print is an unbuffered write.) c int main() { if…
- 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…