title: The difference between fork(), vfork(), exec() and clone()
body: <p>I was looking to find the difference between these four on Google and I expected there to be a huge amount of information on this, but there really wasn't any solid comparison between the four calls.</p>
  <p>I set about trying to compile a kind of basic at-a-glance look at the differences between these system calls and here's what I got. Is all this information correct/am I missing anything important ?</p>
  <p><code>Fork</code> : The fork call basically makes a duplicate of the current process, identical in almost every way (not everything is copied over, for example, resource limits in some implementations but the idea is to create as close a copy as possible).</p>
  <p>The new process (child) gets a different process ID (PID) and has the PID of the old process (parent) as its parent PID (PPID). Because the two processes are now running exactly the same code, they can tell which is which by the return code of fork - the child gets 0, the parent gets the PID of the child. This is all, of course, assuming the fork call works - if not, no child is created and the parent gets an error code.</p>
  <p><code>Vfork</code>: The basic difference between <code>vfork()</code> and <code>fork()</code> is that when a new process is created with <code>vfork()</code>, the parent process is temporarily suspended, and the child process might borrow the parent's address space. This strange state of affairs continues until the child process either exits, or calls <code>execve()</code>, at which point the parent
  process continues.</p>
  <p>This means that the child process of a <code>vfork()</code> must be careful to avoid unexpectedly modifying variables of the parent process. In particular, the child process must not return from the function containing the <code>vfork()</code> call, and it must not call <code>exit()</code> (if it needs to exit, it should use <code>_exit()</code>; actually, this is also true for the child of a normal <code>fork()</code>).</p>
  <p><code>Exec</code>: The exec call is a way to basically replace the entire current process with a new program. It loads the program into the current process space and runs it from the entry point. <code>exec()</code> replaces the current process with a the executable pointed by the function. Control never returns to the original program unless there is an <code>exec()</code> error.</p>
  <p><code>Clone</code>: <code>clone()</code>, as <code>fork()</code>, creates a new process. Unlike <code>fork()</code>, these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers.</p>
  <p>When the child process is created with <code>clone()</code>, it executes the function application <em>fn(arg)</em> (This differs from <code>fork()</code>, where execution continues in the child from the point of the original <code>fork()</code> call.) The <em>fn</em> argument is a pointer to a function that is called by the child process at the beginning of its execution. The <em>arg</em> argument is passed to the <em>fn</em> function.</p>
  <p>When the <em>fn(arg)</em> function application returns, the child process terminates. The integer returned by <em>fn</em> is the exit code for the child process. The child process may also terminate explicitly by calling <code>exit(2)</code> or after receiving a fatal signal.</p>
  <p>Information gotten from:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/1653340/exec-and-fork">Differences between fork and exec</a></li>
  <li><a href="http://www.allinterview.com/showanswers/59616.html" rel="nofollow noreferrer">http://www.allinterview.com/showanswers/59616.html</a></li>
  <li><a href="http://www.unixguide.net/unix/programming/1.1.2.shtml" rel="nofollow noreferrer">http://www.unixguide.net/unix/programming/1.1.2.shtml</a></li>
  <li><a href="http://linux.about.com/library/cmd/blcmdl2_clone.htm" rel="nofollow noreferrer">http://linux.about.com/library/cmd/blcmdl2_clone.htm</a></li>
  </ul>
  <p>Thanks for taking the time to read this ! :)</p>