CS FundamentalsModerateGATE CS
What is printed? (fork succeeds.)
What is printed? (fork succeeds.)
int x = 10;
int main() {
if (fork() == 0) { x += 5; printf("%d ", x); }
else { wait(NULL); x -= 5; printf("%d ", x); }
}- A. 15 10
- B. 5 15
- C. 15 5
- D. 15 15
Correct answer: C. 15 5
The child gets its own copy of x, so its change does not affect the parent. wait() makes the parent print only after the child exits: 15 then 5.
More Operating Systems questions
- 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…
- Disk cylinders 0–199; head at 50, moving toward 199. Requests: 82, 170, 43, 140, 24, 16, 190. Under C-SCAN, counting the return sweep from…
- Disk cylinders 0–199; head at 50, moving toward 199. Requests: 82, 170, 43, 140, 24, 16, 190. Under C-LOOK, counting the jump from the…