CS FundamentalsVery hardGATE CS
stdout is a pipe (fully buffered) and every fork() succeeds. What is the complete output, up to the order of the lines?
stdout is a pipe (fully buffered) and every fork() succeeds. What is the complete output, up to the order of the lines?
int main() {
printf("A");
fork();
printf("B\n");
}- A. A once and B twice ("AB" and "B")
- B. A once and B once
- C. B twice only
- D. AB printed twice (two lines "AB")
Correct answer: D. AB printed twice (two lines "AB")
With full buffering, "A" is still in the stdio buffer at fork(), so the child inherits a copy. Each process then appends "B\n" and flushes at exit: "AB" appears twice. On a line-buffered terminal "A" would still be buffered too (no newline), so the same happens there.
More Operating Systems questions
- How many times is "x" printed? (Every fork() succeeds; output unbuffered.) c int main() { for (int i = 0; i < 2; i++) { if (fork() == 0)…
- What values are printed (in any order)? (Every fork() succeeds.) c int main() { int x = 1; if (fork() == 0) { x = 2; if (fork() == 0) x =…
- How many processes are created in total, including the original? (Every fork() succeeds.) c int main() { for (int i = 0; i < 3; i++) if…
- Processes (arrival, burst): P1 (0, 7), P2 (2, 3), P3 (3, 1), P4 (6, 2). SRTF is used and every switch to a DIFFERENT process costs 1 ms…
- Processes on one CPU, Round Robin quantum 2: A arrives at 0 and needs CPU 3, then I/O 2, then CPU 1; B arrives at 1 and needs CPU 4. A…
- Processes (arrival, burst): P1 (0, 2), P2 (1, 9), P3 (2, 1), P4 (3, 1), P5 (4, 1). Rank FCFS, non-preemptive SJF, SRTF and RR (q = 1) by…