Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Processes are usually created with <code>fork</code>, threads (lightweight processes) are usually created with <code>clone</code> nowadays. However, anecdotically, there exist 1:N thread models, too, which don't do either.</p> <p>Both <code>fork</code> and <code>clone</code> map to the same kernel function <code>do_fork</code> internally. This function can create a lightweight process that shares the address space with the old one, or a separate process (and many other options), depending on what flags you feed to it. The <code>clone</code> syscall is more or less a direct forwarding of that kernel function (and used by the higher level threading libraries) whereas <code>fork</code> wraps <code>do_fork</code> into the functionality of the 50 year old traditional Unix function.</p> <p>The important difference is that <code>fork</code> guarantees that a complete, separate copy of the address space is made. This, as Basil points out correctly, is done with copy-on-write nowadays and therefore is not nearly as expensive as one would think.<br> When you create a thread, it just reuses the original address space and the same memory.</p> <p>However, one should not assume that creating processes is generally "lightweight" on unix-like systems because of copy-on-write. It is somewhat less heavy than for example under Windows, but it's nowhere near free.<br> One reason is that although the actual pages are not copied, the new process still needs a copy of the page table. This can be several kilobytes to megabytes of memory for processes that use larger amounts of memory. Another reason is that although copy-on-write is invisible and a clever optimization, it is not free, and it cannot do magic. When data is modified by either process, which inevitably happens, the affected pages fault.</p> <p>Redis is a good example where you can see that <code>fork</code> is everything but lightweight (it uses <code>fork</code> to do background saves).</p>
 

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