4.3k views
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 and v = y
2. u = x + 10 and v != y
3. u + 10 = x and v = y
4. u + 10 = x and v != y
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
72 is correct rt? That is what I can find from some online question papers. Only from 2006 I could find official question papers.
the one that I have is showing 71 for this question...nevermind.
please change 2nd %d to %p in both printf statement.
@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.

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

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 :)

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

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

it should be OptionC

#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 : u
• 105 is printed by child  : x
=> u+10=x

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

u+10=x  and v=y

selected
Just write 'child' and 'parent' before printing the values. It will make it clear
@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?
@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.
@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)?

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.

@suarshana. Great :) thanks for the help.

## this is superb

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

you dont have any work other than downvoting...ab ise bhi karo downvote.

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?
@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.
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 ...
(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.
I didn't understand the part how v=y? Here &a means physical address right?
@Arjun: Sir any light on why Virtual Address remains same in fork() function. Any article related to that for reading would be very helpful.
@arjun sir could you help out with logical addressing part? I get the solution but I'm not sure about the same address space being maintained.
For this ques particularly, address spaces of parent or child is the address of "a" (where a is allocated).

You mean here the value of a will be allocated at different location by parent process and child process?

They won't modify eeach other's variables??

What are the physical pages and logical address here??
logical address is the address of the variable in the disk or actual storage?..how will we know when it refers to physical memory?
@avinash Here &a means logical address bcoz of security matter ...
it should be option C ?

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
As you said the given program doesn't print the physical address, can you please explain why? I am not getting it