Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The use of <code>fork</code> and <code>exec</code> exemplifies the spirit of UNIX in that it provides a very simple way to start new processes.</p> <p>The <code>fork</code> call basically makes a duplicate of the current process, identical in <em>almost</em> 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 <code>fork</code> - the child gets 0, the parent gets the PID of the child. This is all, of course, assuming the <code>fork</code> call works - if not, no child is created and the parent gets an error code.</p> <p>The <code>exec</code> 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.</p> <p>So, <code>fork</code> and <code>exec</code> are often used in sequence to get a new program running as a child of a current process. Shells typically do this whenever you try to run a program like <code>find</code> - the shell forks, then the child loads the <code>find</code> program into memory, setting up all command line arguments, standard I/O and so forth.</p> <p>But they're not required to be used together. It's perfectly acceptable for a program to <code>fork</code> itself without <code>exec</code>ing if, for example, the program contains both parent and child code (you need to be careful what you do, each implementation may have restrictions). This was used quite a lot (and still is) for daemons which simply listen on a TCP port and <code>fork</code> a copy of themselves to process a specific request while the parent goes back to listening.</p> <p>Similarly, programs that know they're finished and just want to run another program don't need to <code>fork</code>, <code>exec</code> and then <code>wait</code> for the child. They can just load the child directly into their process space.</p> <p>Some UNIX implementations have an optimized <code>fork</code> which uses what they call copy-on-write. This is a trick to delay the copying of the process space in <code>fork</code> until the program attempts to change something in that space. This is useful for those programs using only <code>fork</code> and not <code>exec</code> in that they don't have to copy an entire process space.</p> <p>If the <code>exec</code> <em>is</em> called following <code>fork</code> (and this is what happens mostly), that causes a write to the process space and it is then copied for the child process.</p> <p>Note that there is a whole family of <code>exec</code> calls (<code>execl</code>, <code>execle</code>, <code>execve</code> and so on) but <code>exec</code> in context here means any of them.</p> <p>The following diagram illustrates the typical <code>fork/exec</code> operation where the <code>bash</code> shell is used to list a directory with the <code>ls</code> command:</p> <pre><code>+--------+ | pid=7 | | ppid=4 | | bash | +--------+ | | calls fork V +--------+ +--------+ | pid=7 | forks | pid=22 | | ppid=4 | ----------&gt; | ppid=7 | | bash | | bash | +--------+ +--------+ | | | waits for pid 22 | calls exec to run ls | V | +--------+ | | pid=22 | | | ppid=7 | | | ls | V +--------+ +--------+ | | pid=7 | | exits | ppid=4 | &lt;---------------+ | bash | +--------+ | | continues V </code></pre>
 

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