CS FundamentalsHardAmazon · Microsoft MCQ pattern
How many lines are printed in total? (Assume every fork() succeeds.)
How many lines are printed in total? (Assume every fork() succeeds.)
int main() {
for (int i = 0; i < 3; i++) {
if (fork() == 0)
printf("%d\n", i);
}
return 0;
}- A. 12
- B. 8
- C. 7
- D. 3
Correct answer: C. 7
Only a newly created child prints, once, at the moment it is created, and then continues the loop. Forks: 1 at i = 0, 2 at i = 1, 4 at i = 2, so 7 lines (one 0, two 1s, four 2s). 8 would count the original process, which never prints.
More Operating Systems questions
- A 64-bit machine uses 48-bit virtual addresses, 4 KB pages and 8-byte page-table entries. Every page table occupies exactly one page. How…
- A system uses two-level paging and a TLB. TLB access = 10 ns, memory access = 100 ns, TLB hit ratio = 90%. The TLB is searched first, and…
- TLB access = 20 ns, memory access = 100 ns, single-level page table, TLB searched first. What is the minimum TLB hit ratio for the…
- Memory access takes 100 ns and a page fault takes 5 ms to service. For the effective access time to be at most 20% slower than a…
- A machine has 1 GB of physical memory, 4 KB pages, 64-bit virtual addresses and an inverted page table. How many entries does the inverted…
- How many of the following statements are correct? 1. Increasing the degree of multiprogramming always increases CPU utilisation. 2…