Posted by: prankster March 11, 2011
Login in to Rate this Post:
0
?
@troy,
Parent Child
var1 = 1; var1 =1; (reason var1 is intialized before fork())
var2 = fork(); (creates child process)
if (var2<=0) (checks if child process is created or not(<0)/ Also check if this fork is being called from child (0))
{
var1 =33; var1=33; (if child is not created or this is new child)
}
else
{
var1 =22; (child is created successfull, assigns var1 =22 to parent only)
}
printf("var1=%d\n", var1) (the parent should print 22 and child should print 33)
wait(&temp); //i think this is so that you can read the output
I might be wrong, try running the code and see if that is the o/p.
For your second question: see
http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/create.html
Updated in red:
Parent Child
var1 = 1; var1 =1; (reason var1 is intialized before fork())
var2 = fork(); (creates child process)
if (var2<=0) (checks if child process is created or not(<0)/ Also check if this fork is being called from child (0))
{
var1 =33; var1=33; (if child is not created or this is new child)
}
else
{
var1 =22; (child is created successfull, assigns var1 =22 to parent only)
}
printf("var1=%d\n", var1) (the parent should print 22 and child should print 33)
wait(&temp); //i think this is so that you can read the output
I might be wrong, try running the code and see if that is the o/p.
For your second question: see
http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/create.html
Updated in red:
Last edited: 11-Mar-11 05:19 PM