When vfork is called is parent process really suspended

forkprocess

As far as I know, when vfork is called, the child process uses the same address space as that of the parent and any changes made by the child process in parent'ss variables are reflected onto the parent process. My questions are:

  • When a child process is spawned, is the parent process suspended?
  • If yes, why?
  • They can run in parallel (like threads)? After all, both threads and process call the same clone() function.

After bit of research and Googling, I found out that the parent process is not really suspended, but the calling thread is suspended. Even if this is the case, when the child process does an exit() or exec(), how does the parent process know that the child has exited? And what will happen if we return from a child process?

Best Answer

Your question is partly based on bad naming convention. A "thread of control" in kernel-speak is a process in user-speak. So when you read that vfork "the calling thread is suspended" think "process" (or "heavyweight thread" if you like) not "thread" as in "multi-threaded process".

  • So yes, the parent process is suspended.

vfork semantics were defined for the very common case where a process (the shell most often) would fork, mess with some file descriptors, and then exec another process in place. The kernel folks realized they could save a huge amount of page copying overhead if they skipped the copy since the exec was just going to throw those copied pages away. A vforked child does have its own file descriptor table in the kernel, so manipulating that doesn't affect the parent process, keeping the semantics of fork unchanged.

  • Why? Because fork/exec was common, expensive, and wasteful

Given the more accurate definition of "kernel thread of control", the answer to can they run in parallel is clearly

  • No, the parent will be blocked by the kernel until the child exits or execs

How does the parent know the child has exited?

  • It doesn't, the kernel knows and keeps the parent from getting any CPU at all until the child has gone away.

As for the last question, I would suspect that the kernel would detect the child stack operations involved in a return and signal the child with an uncatchable signal or just kill it, but I don't know the details.