CS FundamentalsVery hardGATE CS · Amazon/Microsoft SDE MCQ
How many numbers are printed in total, and what is their sum? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through copied buffers.)
How many numbers are printed in total, and what is their sum? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through copied buffers.)
int main() {
for (int i = 0; i < 3; i++)
if (fork() && i % 2 == 0)
printf("%d ", i);
}- A. 7 numbers, sum 8
- B. 5 numbers, sum 8
- C. 4 numbers, sum 8
- D. 5 numbers, sum 10
Correct answer: B. 5 numbers, sum 8
At i = 0 one process forks and its parent side prints 0. At i = 1 nothing prints. At i = 2 four processes exist, and each one's parent side prints 2. That is 5 numbers: 0 + 4 × 2 = 8.
More Operating Systems questions
- What is the sum of all values printed? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through…
- 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…