• edited by
58,182 views
195 195 votes
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,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?

  1. $u = x + 10  \text{ and } v = y$
  2. $u = x + 10 \text{ and } v != y$
  3. $u + 10 = x \text{ and } v = y$
  4. $u + 10 = x \text{ and } v != y$

14 Answers

Best answer
101 101 votes

It should be Option C.

#include<stdio.h>
#include<stdlib.h>
void main()
{
    int a =100;
    if(fork()==0)
    {
     a=a+5;
     printf("%d %d \n",a,&a );
    }
   else
    {
     a=a-5;
     printf("%d %d \n",a,&a );
    }
}

Output:

Fork returns $0$ when it is a child process.

if ( fork == 0)              

Is true when it is child . Child increment valule of a .

In the above output:

  • $95$ is printed by parent : $\mathbf{u}$
  • $105$ is printed by child  : $\mathbf{x}$
    $\mathbf{\Rightarrow u+10=x}$

The logical addresses remains the same between the parent and child processes.

Hence, answer should be:

 $\mathbf{ u+10=x}$  and $\mathbf{v=y}$

• edited by
77 77 votes

answer is c because when child is created, it gets a saparate address space i.e. space in RAM and it clones entire address space of parent into it's own address space. so obviously the variable 'a' is also copied and physical address i.e. location in RAM would be different for both variables. but given program doesn't print the physical address, it prints the logical address i.e. distance between the location of variable in RAM and location of start of address space for the process i.e. relative location of variable with respect to start of it's address space which will be same for both parent and child since data is cloned.

 

in unix tho, address space of parent and child is also same until one of them tries to modify the contents of it's address space. then they are assigned different address spaces and cloning is done.

now before you go on to solve next question, you deserve to see this bunny with a backpack--

• edited by
45 45 votes
(c) is the answer. Child is incrementing a by 5 and parent is decrementing a by 5. So, x = u + 10.

During fork(), address space of parent is copied for the child. So, any modifications to child variable won't affect the parent variable or vice-verse. But this copy is for physical pages of memory. The logical addresses remains the same between the parent and child processes.
13 13 votes
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;

Child process will execute the if part and parent process will execute the else part. Assume that the initial value of a = 6. Then the value of a printed by the child process will be 11, and the value of a printed by the parent process in 1. Therefore u+10=x.

the virtual address is same but virtual addresses exist in different processes’ virtual address space and when we print &a, it’s actually printing the virtual address. Hence the answer is v = y.
5 5 votes

Option C....

In the question, the child and parent processes are as follows :

Child =>  a = a + 5, 
                printf("%d, %p n", a, &a);

Values are gives as x = a, and y = &a

Parent => a = a - 5
                 printf("%d, %p n", a, &a);

Values are gives as u = a, and v = &a

Now, &a prints Logical address, and during fork() we create an exact same copy of the process and the variables are stored at the same logical address, while at different physical addresses.

So that's why, y=v, 

Now as written above, physical address is different, so x and u are different, x = a + 5, u = a - 5, Now we can easily find relation b/w x and u, which is x = u + 10.

4 4 votes
$u, v$ values printed by the parent process.
$u=a-5; v$ be address of $a$
$a=u+5;$
$x, y$ values printed by the child process.
$x=a+5; y$ be the address of $a$
$x=u+5+5; v=y$
$x=u+10$
• edited by
Answer:
Position:
Show:

Related questions

197 197 votes
9 answers 9 answers
77.8k
77.8k views
Kathleen asked Sep 22, 2014
77,787 views
A $5$ stage pipelined CPU has the following sequence of stages:IF – instruction fetch from instruction memoryRD – Instruction decode and register readEX – Execute: ALU op...
32 32 votes
3 answers 3 answers
15.3k
15.3k views
gatecse asked Sep 21, 2014
15,326 views
Let $f(x)$ be the continuous probability density function of a random variable $x$, the probability that $a < x \leq b$, is :$f(b-a)$$f(b) - f(a)$$\int\limits_a^b f(x) dx...
72 72 votes
7 answers 7 answers
26.5k
26.5k views
Kathleen asked Sep 22, 2014
26,465 views
Suppose $n$ processes, $P_1, \dots P_n$ share $m$ identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process ...
48 48 votes
4 answers 4 answers
17.3k
17.3k views
Kathleen asked Sep 22, 2014
17,282 views
Normally user programs are prevented from handling I/O directly by I/O instructions in them. For CPUs having explicit I/O instructions, such I/O protection is ensured by ...