<br><br><div class="gmail_quote">On Sat, Jan 19, 2013 at 8:13 AM, Peter Teoh <span dir="ltr"><<a href="mailto:htmldeveloper@gmail.com" target="_blank">htmldeveloper@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br><br><div class="gmail_quote"><div class="im">On Sat, Jan 19, 2013 at 5:49 AM, <span dir="ltr"><<a href="mailto:Valdis.Kletnieks@vt.edu" target="_blank">Valdis.Kletnieks@vt.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>On Fri, 18 Jan 2013 19:59:38 +0530, Niroj Pokhrel said:<br>
<br>
> I have been trying to create a process using vfork(). And both of the child<br>
> and the parent process execute it in the same address space. So, if I<br>
> execute exit(0) in the child process, it should throw some error right.<br>
<br>
</div>Why do you think it should throw an error?<br>
<div><br>
> Since the execution is happening in child process first and if I release<br>
> all the resources by using exit(0) in the child process then parent should<br>
> be deprived of the resources and should throw some errors right ??<br>
<br>
</div>No, because those resources that were shared across a fork() or vfork() were in<br>
general *multiple references* to the same resource.<br>
<br></blockquote><div><br></div></div><div>Yes, correct, Valdis is right. Normally, when u free resources (which is what "exit()" will do), u must also remember to check something call "reference count". </div>
<div><br></div><div>Basic malloc() and free() memory management internal data structure also comes with other info like size (which is 4 bytes BEHIND the first byte where the pointer points to, and other info). More info:</div>
<div><br></div><div><a href="http://stackoverflow.com/questions/1957099/how-do-free-and-malloc-work-in-c" target="_blank">http://stackoverflow.com/questions/1957099/how-do-free-and-malloc-work-in-c</a></div><div><br></div>
<div>but what is lacking is reference counting. But this feature is available in Java and C++ libraries for memory allocation.</div>
<div><br></div><div>Concept discussed here:</div><div><br></div><div><a href="http://stoneship.org/essays/c-reference-counting-and-you/" target="_blank">http://stoneship.org/essays/c-reference-counting-and-you/</a></div>
<div><br></div><div>
Interesting....</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
As an example - imagine a flagpole. You grab it with your hand, you're<br>
now holding it. You invite your friend to come over and grab it with<br>
his hand - now he's holding it too.<br>
<br>
But either one of you can let go of the flagpole - and the other one is<br>
still holding the flagpole until *they* let go. And the order you let<br>
go doesn't matter in this case - which is important because your example<br>
code has a race condition....<br>
<br>
Note that there are other cases where the order people let go *does* matter.<br>
This is when you start having to worry about "locking order" and things like<br>
that.<br>
<div><br>
> In the following code, however the process ran fine even though I have<br>
> exit(0) in the child process ........<br>
<br>
> #include<stdio.h><br>
> #include<stdlib.h><br>
> #include<sys/types.h><br>
> #include<unistd.h><br>
> int main()<br>
> {<br>
> int val,i=0;<br>
> val=vfork();<br>
> if(val==0)<br>
> {<br>
> printf("\nI am a child process.\n");<br>
<br>
</div>Note that printf() gets interesting due to stdio buffering. You probably<br>
want to call setbuf() and guarantee line-buffering of the output if you're<br>
playing these sorts of games - the buffering can totally mask a real race<br>
condition or other bug.<br>
<div><br>
> printf(" %d ",i++);<br>
> exit(0);<br>
> }<br>
> else<br>
> {<br>
<br>
</div>/* race condition here - may want wait() or waitpid() to synchronize? */<br>
<div><br>
> printf("\nI am a parent process.\n");<br>
> printf(" %d ",i);<br>
> }<br>
> return 0;<br>
> }<br>
> // The program is running fine .....<br>
> But as I have read it should throw some error right ?? I don't know what I<br>
> am missing . Please point out the point I'm missing. Thanking you in<br>
> advance.<br>
<br>
</div>You're also missing the fact that after the vfork(), there's no real<br>
guarantee of which will run first - which means that the parent can race<br>
and output the 'printf("%d",i)" *before* the child process gets a chance<br>
to do the i++.<br>
<br></blockquote><div><br></div></div></div><div>I don't think there is any issue here (racing, or child calling exit before parent called exit()). Read the man-page:</div><div><br></div><div><div> vfork() differs from fork(2) in that the parent is suspended until the</div>
<div> child terminates (either normally, by calling _exit(2), or abnormally,</div><div> after delivery of a fatal signal), or it makes a call to execve(2).</div><div> Until that point, the child shares all memory with its parent, includ‐</div>
<div> ing the stack. The child must not return from the current function or</div><div> call exit(3), but may call _exit(2).</div></div><div><br></div><div><br></div><div>So:</div><div><br></div><div>1. if parent is suspended, it also means no contention issue, child can use the "i++", which is single-copy in memory, and parent is not even touching it.</div>
<div><br></div><div>2. It is clearly documented above - the vfork()'s child CAN exit() before the parent calling exit().</div><div><br></div><div>3. More important is to understand the the use of vfork() - u don't to create a duplicate memory page table because u want to call execve() immediately, which will overwrite everything in memory with the new binary inside execve() command.</div>
<div><br></div><div>Hope I got it right?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
(Aside - for a while, there was a patch in place that ensured that the<br>
child would run first, on the theory that the child would often do something<br>
short that the parent was waiting on, so scheduling parent-first would just<br>
result in the parent running, blocking to wait, and we end up running the<br>
child anyhow before the parent could continue. It broke an *amazing* amount<br>
of stuff in userspace because often the child would exit() before the parent was<br>
ready to deal with the child process's termination. Usual failure mode was<br>
the parent would set a SIGCHLD handler, and wait for the signal which never<br>
happened because the SIGCHLD actually fired *before* the handler was set up).<br>
<br>
(And on non-cache-coherent systems, it's even possible that the i++ happens<br>
on a different CPU first, and the CPU running the parent process never becomes<br>
aware of it. See 'Documentation/memory-barriers.txt' in the Linux source<br>
for more info on how this works for data inside the kernel. This example<br>
is out in userspace, so other techniques are required instead to do cross-CPU<br>
synchronization.<br>
<br></div><div class="im">_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org" target="_blank">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
<br></div></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br>Regards,<br>Peter Teoh
</font></span></blockquote><div><br><br>Hi all, <br>Thank you very much. I have much idea now. However, I am still wondering about these lines <br>
" those resources that were shared across a fork() or vfork() were in general *multiple references* to the same resource. " .<br>Is it that all the resources are shared as multiple references or if it is otherwise. Will you please give me some example of the case which I should not share the resources ? Thanking you all in advance. <br>
<br><br><br><br><br><br> </div></div><br>