CS FundamentalsVery hardGATE CS
What is the sum of all values printed? (Every fork() succeeds.)
What is the sum of all values printed? (Every fork() succeeds.)
int main() {
int c = 0;
for (int i = 0; i < 3; i++)
if (fork() == 0) c++;
printf("%d\n", c);
}- A. 8
- B. 24
- C. 3
- D. 12
Correct answer: D. 12
8 processes print; each value counts how many of the 3 forks returned 0 in that process's history, so the values are distributed binomially: {0: 1, 1: 3, 2: 3, 3: 1}. Sum = 3 × 4 = 12 (each fork contributes 1 to half of the final processes).
More Operating Systems questions
- A process calls fork() three times in sequence and every child also continues the code. Afterwards, how many processes exist and how many…
- What is printed? (fork succeeds.) c int x = 10; int main() { if (fork() == 0) { x += 5; printf("%d ", x); } else { wait(NULL); x -= 5…
- A parent process exits without calling wait() while its child is still running. What happens to the child?
- After a successful exec() call, which of these is preserved from the calling process?
- Disk cylinders 0–199; the head is at 50. Requests in arrival order: 82, 170, 43, 140, 24, 16, 190. What is the total head movement under…
- Disk cylinders 0–199; head at 50, moving toward 199. Requests: 82, 170, 43, 140, 24, 16, 190. What is the total head movement under SCAN…