7k 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 \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$
edited | 7k views
+1
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
0
72 is correct rt? That is what I can find from some online question papers. Only from 2006 I could find official question papers.
+1
the one that I have is showing 71 for this question...nevermind.
+1
please change 2nd %d to %p in both printf statement.
+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.
0

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

+20

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

+3

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

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

+1
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

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.

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

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

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.

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

## this is superb

0

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

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

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?
+10
@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.
0
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
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
v!=y is true... Answer must be option D
0
NOT GETTING V=Y CONCEPT ????
0

@ please explain how V=Y is true

+1
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 ..
0
@jatin

your logic seems to be correct for me
(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.
0
I didn't understand the part how v=y? Here &a means physical address right?
0
@Arjun: Sir any light on why Virtual Address remains same in fork() function. Any article related to that for reading would be very helpful.
0
@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.
0
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??
0
logical address is the address of the variable in the disk or actual storage?..how will we know when it refers to physical memory?
0
@avinash Here &a means logical address bcoz of security matter ...
+1
it should be option C ?
0
Wrong because child and parent are stored in separate memory locations. V can't be equal to Y.

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

1
2