Operating Systems | Process Management | Question 6
Consider the following code fragment:
if (fork() == 0) { a = a + 5; printf ( "%d,%d\n" , a, &a); } else { a = a –5; printf ( "%d, %d\n" , a, &a); } |
Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?
(A) u = x + 10 and v = y
(B) u = x + 10 and v != y
(C) u + 10 = x and v = y
(D) u + 10 = x and v != y
Answer: (C)
Explanation: fork() returns 0 in child process and process ID of child process in parent process.
In Child (x), a = a + 5
In Parent (u), a = a – 5;
Therefore x = u + 10.
The physical addresses of ‘a’ in parent and child must be different. But our program accesses virtual addresses (assuming we are running on an OS that uses virtual memory). The child process gets an exact copy of parent process and virtual address of ‘a’ doesn’t change in child process. Therefore, we get same addresses in both parent and child. See this run for example.
Quiz of this Question
Recommended Posts:
- Operating Systems | Process Management | Question 6
- Operating Systems | Process Management | Question 6
- Operating Systems | Process Management | Question 6
- Operating Systems | Process Management | Question 6
- Operating Systems | Memory Management | Question 1
- Operating Systems | Process Synchronization | Question 5
- States of a Process in Operating Systems
- Operating Systems | Input Output Systems | Question 5
- Operating Systems | CPU Scheduling | Question 2
- Operating Systems | CPU Scheduling | Question 1
- Introduction of Process Management
- Multi Threading Models in Process Management
- Free space management in Operating System
- Operating Systems | Set 17
- Operating Systems | Set 7
Improved By : ArthurDayne05