CS FundamentalsVery hardGATE CS · Amazon/Microsoft SDE MCQ
What is the sum of all values printed? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through copied buffers.)
What is the sum of all values printed? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through copied buffers.)
int main() {
int c = 0;
for (int i = 0; i < 3; i++)
if (fork()) c++;
printf("%d\n", c);
}- A. 8
- B. 12
- C. 24
- D. 7
Correct answer: B. 12
8 processes print, and each process's c counts how many of its 3 forks returned the parent side, i.e. a 3-bit pattern. The counts follow 0, 1, 1, 1, 2, 2, 2, 3, summing to 12.
More Operating Systems questions
- Semaphores s = 2 and t = 0. T1 runs twice: wait(s); print('a'); signal(t). T2 runs twice: wait(t); print('b'); signal(s). Which outputs…
- Semaphores x = y = z = 0. T1: print A; signal(x); wait(y); print D. T2: wait(x); print B; signal(y); signal(z). T3: wait(z); print C…
- Shared x = 1. Three threads run once each, with no locking: T1 does x = x + 2, T2 does x = x 3, T3 does x = x − 1. Each statement is a…
- Shared x = 1. Thread A executes x = x + 1 twice; thread B executes x = x 2 once; each statement is a separate load and store and there is…
- References at times 1 to 12 are 1, 3, 2, 3, 4, 4, 1, 5, 2, 5, 5, 3. With working-set window Δ = 5 (the last 5 references, including the…
- Three processes share one resource type with identical instances. P1 may need at most 3 instances, P2 at most 4 and P3 at most 5, and each…