in Operating System edited by
37,142 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

4 Comments

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

20 Comments

Just write 'child' and 'parent' before printing the values. It will make it clear
2
2
@pc @sudarshana.

Lets consider single level paging system. Now, if virtual addresses are same means offset in the page table is same. So, from the output shown above, how can Page table entry(PTE) be different for child and parent?
1
1
@Sushant

Virtual adress of 'a' will be same for child unless execv() is called immediately after fork for child. When a parent forks, the child will get the same VA space as the parent, but all the page table entries are marked read-only. When either child/parent tries modifying the page, a page fault will ensue (copy-on-write) and the OS pages in a new page, copy the old contents and mark the the page as RW for both parent and child. Now, the PTE will be different for both parent and child. (But virtual address is still the same)

Please correct me if I am wrong.
2
2
@Sudarshana. You agree copy-on-write is required here. So, physical pages for child and parent are different.  Now, question is are they accessing the same location in page table ? Now, as you said PTE's will be different. SO, what will happen if parent and child access the same location in page table at the same time?

Could you draw a diagram as to how the virtual addresses can be same for 2 processed(either 1 level paging or multi-level paging)?
2
2
edited by

SO, what will happen if parent and child access the same location in page table at the same time?

We have 2 process here-child and parent and both will have separate page tables. 

I found this explaination SO: It may help: 

Lets say your process is got var name X that have a virtual address 100 and physical address 200. the PTE is holding a mapping of addresses from virtual 100 to physical 200. After the fork, each process (parent and child) will have his private PTE. at this point both PTEs will map virtual 100 to physical 200. as long as both process just read from there they both will read from physical address 200. When the first one will try to write there, the data from physical address will be copy to a new physical space, lets say 300, and his (and only his) PTE will be update so virtual 100 will be mapped to physical 300. that way it's transparent to the process because he is still using the same (virtual) address.                 *Note: this is just an abstract, and the real thing is happening in page resolution.

Link of SO: http://stackoverflow.com/a/6200398 

61
61
@suarshana. Great :) thanks for the help.
0
0

this is superb

0
0

Awesome question. Good doubt @ Sushant Gokhale  and nicely answered :)

0
0
you dont have any work other than downvoting...ab ise bhi karo downvote.
1
1
hey, the address printed should be physical address ,no?

and physical address is different for both child process and parent process.

can you provide me the reason as to why u r considering logical address?
1
1
@sachi, physical addresses are transparent to the process (it means processes are not aware of their physical addresses).They can only see their virtual address..so. the program will print virtual address which are always same for both parent and child process.
34
34
Every process will hav its own PCB .... So here the values of child will not affect the values of parent ... Bt the addresses will be same as fork copies data from parent and creates child process ...
0
0
edited by
they didn't mentioned about it is logical address yet.in question.we should consider it as physical or logical.

if physical address of parent is stored in v

if physical address of child is stored in y

then v!=y.explain me where i am wrong.
0
0
v!=y is true... Answer must be option D
1
1
NOT GETTING V=Y CONCEPT ????
0
0

@ please explain how V=Y is true

0
0
V and Y are the logical/addresses that maps to different physical location[differnet copies of a here] after if either parent/child writes ..so these are same logical addresses , mapping to different physical page ..hence v = y ..
6
6
@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