Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>fork()</code> is how you create new processes in Unix. When you call <code>fork</code>, you're creating a copy of your own process that has its own <a href="http://en.wikipedia.org/wiki/Address_space" rel="noreferrer">address space</a>. This allows multiple tasks to run independently of one another as though they each had the full memory of the machine to themselves.</p> <p>Here are some example usages of <code>fork</code>:</p> <ol> <li>Your <a href="http://en.wikipedia.org/wiki/Shell_(computing)" rel="noreferrer">shell</a> uses <code>fork</code> to run the programs you invoke from the command line.</li> <li>Web servers like <a href="http://www.apache.org/" rel="noreferrer">apache</a> use <code>fork</code> to create multiple server processes, each of which handles requests in its own address space. If one dies or leaks memory, others are unaffected, so it functions as a mechanism for fault tolerance.</li> <li><a href="http://www.google.com/chrome" rel="noreferrer">Google Chrome</a> uses <code>fork</code> to handle each page within a separate process. This will prevent client-side code on one page from bringing your whole browser down.</li> <li><code>fork</code> is used to spawn processes in some parallel programs (like those written using <a href="http://www.mcs.anl.gov/research/projects/mpi/" rel="noreferrer">MPI</a>). Note this is different from using <a href="http://en.wikipedia.org/wiki/Thread_(computer_science)" rel="noreferrer">threads</a>, which don't have their own address space and exist <em>within</em> a process.</li> <li>Scripting languages use <code>fork</code> indirectly to start child processes. For example, every time you use a command like <a href="http://docs.python.org/library/subprocess.html#module-subprocess" rel="noreferrer"><code>subprocess.Popen</code></a> in Python, you <code>fork</code> a child process and read its output. This enables programs to work together.</li> </ol> <p>Typical usage of <code>fork</code> in a shell might look something like this:</p> <pre><code>int child_process_id = fork(); if (child_process_id) { // Fork returns a valid pid in the parent process. Parent executes this. // wait for the child process to complete waitpid(child_process_id, ...); // omitted extra args for brevity // child process finished! } else { // Fork returns 0 in the child process. Child executes this. // new argv array for the child process const char *argv[] = {"arg1", "arg2", "arg3", NULL}; // now start executing some other program exec("/path/to/a/program", argv); } </code></pre> <p>The shell spawns a child process using <code>exec</code> and waits for it to complete, then continues with its own execution. Note that you don't have to use fork this way. You can always spawn off lots of child processes, as a parallel program might do, and each might run a program concurrently. Basically, any time you're creating new processes in a Unix system, you're using <code>fork()</code>. For the Windows equivalent, take a look at <a href="http://msdn.microsoft.com/en-us/library/ms682425.aspx" rel="noreferrer"><code>CreateProcess</code></a>.</p> <p>If you want more examples and a longer explanation, <a href="http://en.wikipedia.org/wiki/Fork_(operating_system)" rel="noreferrer">Wikipedia</a> has a decent summary. And <a href="http://courses.cs.vt.edu/~cs3204/spring2009/butta/local/lectures/lecture-07.pdf" rel="noreferrer">here are some slides</a> here on how processes, threads, and concurrency work in modern operating systems.</p>
    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. 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.
 

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