in Operating System edited by
37,139 views
123 votes
123 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$
in Operating System edited by
37.1k views

20 Comments

are you sure about the numbering of this question..I mean is it 72 ? if not then please change it to relevant number coz people do tend to come here when actually searching for 72. I think it is related to packet optimum size.thanks
1
1
72 is correct rt? That is what I can find from some online question papers. Only from 2006 I could find official question papers.
0
0
the one that I have is showing 71 for this question...nevermind.
1
1
please change 2nd %d to %p in both printf statement.
1
1
@arjun sir..change the options a,b,c,d to this
question no. 5 http://www.geeksforgeeks.org/operating-systems-set-16/
others sites also contain the same options.
1
1

@Arjun SIR , What is the correct ans ? is it C or D ? Please select a best answer

0
0
IS ANSWER B????
0
0

This comment may help you to get the problem of physical and logical address solved. As many of us think that given an integer i, while we print "&i" it gives the physical address. But this is wrong. I was also under the same impression but then i came across this video.

Do watch it till 2:20 and the doubt will get cleared. It is a lecture by an IIT professor so its reliable :)

61
61

Everything you need to know about fork() system call.

http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

24
24
another version of the program and its output,I am changing the value at virtual address of a see the printed output.(from eclipse ide)

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

 

parent -- 95 197701748
parent -- 50 197701748
child -- 105 197701748
child -- 25 197701748
3
3
fork returns 0 when it is a child process. When a fork() is called the entire address space(stack,heap...) of parent is copied for child process separately. So, any modifications to child variable won't affect the parent variable or vice-verse. But this copy is for physical pages of memory(only main memory pages used for execution is copied), But the logical addresses(address of any variable,function call) remains the same between the parent and child processes.
6
6

@MiNiPanda Thnx for d link. It cleared many doubts. 

1
1

@Bikram sir, @MiNiPanda so when we print &i, the logical address of  i gets printed i.e. if i is at position number 5 in the address space of process then 5 will be printed. Due to this, even if the threads have different copy of variable i but the relative position of these two different i variables will be same in the address space of the process therefore &i will be same for both.Is this correct?

2
2
0
0

A lot of discussion happened for this question. A small contribution from my side.

Hope this helps !

$1.$ Go through this link. To understand $COW$

$Why. u+10=x$

$2.$ Now, go through this link.

$Why. v=y$

17
17

 please confirm this – 

before any modification,logical as well as physical address will be same for the variable to be modified.but after modification of the variable,logical address will remain same but physical address will be different due to COW.

like here if we can get to know about physical address of u & v after end of this program then they are not equal,but before starting they were equal.

2
2
His name is Chester.

He is god of OS
0
0
2
2
edited by

Key idea:     printf() prints logical addresses.

When we fork a new process, then the new child process so created (which is an exact copy of parent, except PID etc.) has same logical addresses of global and global static variables. Also local variables and static local variables of functions share same logical addresses.

In below image, we can see child and parent has same logical addresses for different kinds of variables.

Link to code

But we need to be careful in terms of block scope due to if-else constructs or others.

In below image, we see same variables within if-else has different addresses in parent and child (Same variables [eg. local, slocal ] appear to be one after other)

Link to code

1
1

9 Answers

81 votes
81 votes
Best answer

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
by

4 Comments

@jatin

your logic seems to be correct for me
0
0
edited by
why not  v!=y???
0
0
Both variables can never be at same location v can never be equal to y.
1
1
40 votes
40 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

3 Comments

As you said the given program doesn't print the physical address, can you please explain why? I am not getting it
0
0
good explanation🐰🐇
0
0
thanks for the bunny!
2
2
36 votes
36 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.

4 Comments

@avinash Here &a means logical address bcoz of security matter ...
0
0
edited by
it should be option C ?
1
1
Wrong because child and parent are stored in separate memory locations. V can't be equal to Y.
0
0
11 votes
11 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.
Answer:

Related questions