DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: OutPut of a Program
*****************************************************
#Post#: 84--------------------------------------------------
output
By: ravu Date: September 28, 2012, 4:30 am
---------------------------------------------------------
Consider the following code snippet...
if (fork() == 0)
{
a = a + 5;
printf("%d, %d \n", a, &a);
}
else
{
a = a - 5;
printf ("%d, %d \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? [ a ? b means that the relationship between a
and b cannot be determined and will depend on whether child runs
first or parent runs first or some other factors]
a) x ? u y ? v
b) x ? u y = v
c) x = u+10 y ? v
d) x = u+10 y = v
#Post#: 86--------------------------------------------------
Re: output
By: kranthipls Date: November 15, 2012, 2:48 am
---------------------------------------------------------
Is it D? :o
#Post#: 131--------------------------------------------------
Re: output
By: kpr29 Date: November 19, 2012, 12:29 am
---------------------------------------------------------
@Kranthi: Yes......... It is .D........... But for this to be
right.......... the value of a should be equal to
5...............
kp
#Post#: 165--------------------------------------------------
Re: output
By: satya Date: November 19, 2012, 11:23 pm
---------------------------------------------------------
@kpleader: can u clarify why the value of a must be only 5??
??? ???
#Post#: 167--------------------------------------------------
Re: output
By: kranthipls Date: November 19, 2012, 11:29 pm
---------------------------------------------------------
@KP The value of a can be anything.
#Post#: 183--------------------------------------------------
Re: output
By: kpr29 Date: November 21, 2012, 12:41 am
---------------------------------------------------------
@Satti & Kranthi:vPart IFirst the parent process will be
executed that is else part........ if a = 0......... then the
value in the else part is -5
Part II: the value a=0 will be copied to the child
process........ so the value that will be printed is 5...... I
am not clear how both will be point to same address........
#Post#: 188--------------------------------------------------
Re: output
By: dinesh Date: November 21, 2012, 8:54 am
---------------------------------------------------------
since the values of 'a' is different for both parent and child,
shouldn't they have different addresses?
#Post#: 193--------------------------------------------------
Re: output
By: kranthipls Date: November 25, 2012, 10:19 pm
---------------------------------------------------------
I hope you all understood why y=u+10 (I PART)................
Looks like the confusion is only with the second part.
So let me clarify this according my understanding.
When a new process is created using fork, the child process will
be an exact replica of the parent process.
So literally all their values and everything will be same. So
when you print &a what gets printed is the virtual address but
not the physical address.
So even the child process gets the same virtual address for a.
This does not mean that both a's in the parent and the child
point to the same location in the physical memory. So the
address for both the parent's a and child's a are same......
#Post#: 196--------------------------------------------------
Re: output
By: kpr29 Date: November 26, 2012, 12:32 am
---------------------------------------------------------
@Kranthi: Awesome.......
#Post#: 335--------------------------------------------------
Re: output
By: satya Date: December 4, 2012, 11:11 pm
---------------------------------------------------------
i am still not clear why the value of a must be only 5.......it
can be anything right................
*****************************************************
DIR Next Page