CS FundamentalsVery hardGATE CS
How many times is "x" printed? (Every fork() succeeds.)
How many times is "x" printed? (Every fork() succeeds.)
int main() {
if (fork() || fork())
fork();
printf("x\n");
}- A. 5
- B. 4
- C. 6
- D. 8
Correct answer: A. 5
fork() || fork() yields 3 processes: the original (true), the first child whose second fork returned nonzero (true), and the grandchild (false). The two true ones fork once more: 3 + 2 = 5.
More Operating Systems questions
- Semaphores a = 1, b = 0. P: wait(a); print 1; signal(b); signal(b). Q: wait(b); print 2; signal(a). R: wait(b); print 3. Each runs once…
- Shared x = 1. T1 does x = x + 1; T2 and T3 each do x = x 2; (load, compute, store, no locks). How many distinct final values of x are…
- How many of these statements about threads are correct? 1. A thread that calls exit() terminates the whole process. 2. If one thread…
- A server uses a fixed pool of 8 worker threads, and each request does a blocking database call taking 90% of its time. Roughly how many…
- Which statement about user-mode and kernel-mode stacks is correct?
- A process gets SIGSEGV writing to a page marked read-only that belongs to a copy-on-write mapping after fork(). Why is this NOT what…