Consider the following code fragment:
if (fork() == 0)
{
a = a + 5;
printf("%d, %p n", a, &a);
}
else
{
a = a - 5;
printf ("%d, %p n", a,& a);
}
Let be the values printed by the parent process and be the values printed by the child process. Which one of the following is TRUE?
View worked solution
Answer: C
When a process calls fork(), a duplicate child process is created. The execution behavior of the parent and child is determined by the return value of fork():
- In the child process:
fork()returns0. Theifblock is executed.- Value printed ():
- Address printed ():
- In the parent process:
fork()returns the non-zero PID of the child. Theelseblock is executed.- Value printed ():
- Address printed ():
1. Relationship between and :
2. Relationship between and :
During a fork(), the child process gets an exact duplicate of the parent's address space. This means the virtual address of the variable a is identical in both the parent and child processes.
Since the printf function prints the virtual address of the variable (using the %p format specifier), the printed address will be the same in both processes. Therefore, .
Thus, the correct statement is .
Free practice · Your answers on this page are not saved.
