Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't use <code>vfork</code>. That's the simplest advice you can get. The only thing that <code>vfork</code> gives you is suspending the parent until the child either calls <code>exec*</code> or <code>_exit</code>. The part about sharing the address space is incorrect, some operating systems do it, other choose not to because it's very unsafe and has caused serious bugs.</p> <p>Last time I looked at how applications use <code>vfork</code> in reality the absolute majority did it wrong. It was so bad that I threw away the 6 character change that enabled address space sharing on the operating system I was working on at that time. Almost everyone who uses <code>vfork</code> at least leaks memory if not worse.</p> <p>If you really want to use <code>vfork</code>, don't do anything other than immediately call <code>_exit</code> or <code>execve</code> after it returns in the child process. Anything else and you're entering undefined territory. And I really mean "anything". You start parsing your strings to make arguments for your exec call and you're pretty much guaranteed that something will touch something it's not supposed to touch. And I also mean <code>execve</code>, not some other function from the exec family. Many libc out there do things in <code>execvp</code>, <code>execl</code>, <code>execle</code>, etc. that are unsafe in a <code>vfork</code> context.</p> <p><em>What is specifically happening in your example:</em> </p> <blockquote> <p>If your operating system shares address space the child returning from main means that your environment cleans things up (flush stdout since you called printf, free memory that was allocated by printf and such things). This means that there are other functions called that will overwrite the stack frame the parent was stuck in. <code>vfork</code> returning in the parent returns to a stack frame that has been overwritten and anything can happen, it might not even have a return address on the stack to return to anymore. You first entered undefined behavior country by calling printf, then the return from main brought you into undefined behavior continent and the cleanup run after the return from main made you travel to undefined behavior planet.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload