Skip to content
Official PYQ · 2005

GATE CS 2005 Q72 — System Calls

Explain with AI
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 u,vu,v be the values printed by the parent process and x,yx,y be the values printed by the child process. Which one of the following is TRUE?

Choose one option
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() returns 0. The if block is executed.
    • Value printed (xx): x=ainitial+5x = a_{\text{initial}} + 5
    • Address printed (yy): y=&ay = \&a
  • In the parent process: fork() returns the non-zero PID of the child. The else block is executed.
    • Value printed (uu): u=ainitial−5u = a_{\text{initial}} - 5
    • Address printed (vv): v=&av = \&a

1. Relationship between uu and xx:

x−u=(ainitial+5)−(ainitial−5)=10  ⟹  u+10=xx - u = (a_{\text{initial}} + 5) - (a_{\text{initial}} - 5) = 10 \implies u + 10 = x

2. Relationship between vv and yy:

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, v=yv = y.

Thus, the correct statement is u+10=x and v=yu + 10 = x \text{ and } v = y.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore System Calls questions