Annotation of htdocs/docs/kernel/vfork.html, revision 1.2

1.1       dsieger     1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                      2: <html>
                      3: <head>
                      4: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                      5: <meta name="generator" content="Website XSL Stylesheet V2.6.0">
                      6: <link rel="home" href="../../." title="Welcome to The NetBSD Project: Of course it runs NetBSD.">
                      7: <link rel="up" href="../../docs/kernel/." title="NetBSD Documentation: Kernel">
                      8: <link rel="previous" href="../../docs/kernel/uvm.html" title="NetBSD Documentation: UVM, the new Virtual Memory system">
                      9: <link rel="next" href="../../docs/kernel/converting-ethernet-drivers.html" title="NetBSD Documentation: Converting ancient BSD Ethernet drivers to NetBSD">
                     10: <link rel="first" href="../../docs/kernel/lazyfpu.html" title="NetBSD Documentation: How lazy FPU context switch works">
                     11: <link rel="last" href="../../docs/kernel/elf-notes.html" title="NetBSD Documentation: Vendor-specific ELF Note Elements">
                     12: <link rel="stylesheet" href="/NetBSD.css" type="text/css">
                     13: 
                     14: 
                     15: 
                     16: <title>NetBSD Documentation: Why implement traditional vfork()</title>
                     17: </head>
                     18: <body class="website"><div class="webpage">
                     19: <a name="docs-kernel-vfork"></a><h1>NetBSD Documentation: Why implement traditional vfork()</h1>
                     20: <h3 class="title"><a name="vfork"></a></h3>
                     21: <ul>
                     22: <li><a href="#intro">Introduction</a></li>
                     23: <li><a href="#4bsd-vfork-cow">vfork()/exec() using the 4.4BSD
                     24: vfork() and COW</a></li>
                     25: <li><a href="#3bsd-vfork">The 3.0BSD/NetBSD vfork(), using address space
                     26: sharing</a></li>
                     27: </ul>
                     28: <hr>
                     29: <h3 class="title"></h3>
                     30: <h4 class="title">
                     31: <a name="intro"></a>Introduction (<a href="#vfork">top</a>)
                     32:   </h4>
                     33: <p><code class="function">vfork()</code> is designed to be used in the specific case where
                     34: the child will <code class="function">exec()</code> another program, and the parent can
                     35: block until this happens. A traditional <code class="function">fork()</code> required
                     36: duplicating all the pages of the parent process in the child - a
                     37: significant overhead.</p>
                     38: 
                     39: <p>
                     40: The Mach VM system added Copy On Write (COW), which made the
                     41: <code class="function">fork()</code> much cheaper, and in BSD 4.4, <code class="function">vfork()</code>
                     42: was made synonymous to <code class="function">fork()</code>.  After NetBSD 1.3, a
                     43: traditional <code class="function">vfork()</code> was reimplemented.</p>
                     44: 
                     45: <p>A good amount of effort was directed at making COW better in <a href="uvm.html" target="_top"><span class="bold"><strong>UVM</strong></span></a>, but an
                     46: address space-sharing <code class="function">vfork()</code> <span class="emphasis"><em>still</em></span>
                     47: turns out to be a win.  It shaves several seconds off a build of libc
                     48: on a 200MHz PPro.</p>
                     49: 
                     50: <h4 class="title">
                     51: <a name="4bsd-vfork-cow"></a><code class="function">vfork()</code>/<code class="function">exec()</code> using the 4.4BSD
                     52: <code class="function">vfork()</code> and COW (<a href="#vfork">top</a>)
                     53:   </h4>
                     54: 
                     55: <div class="itemizedlist"><ul type="disc">
                     56: <li><p>Traverse parent's vm_map, marking the writable portions of the
                     57:         address space COW.  This means invoking the pmap, modifying PTEs,
                     58:         and flushing the TLB.</p></li>
                     59: <li><p>Create a vm_map for the child, copy the parent's vm_map entries
                     60:         into the child's vm_map.  Optionally, invoke the pmap to copy
                     61:         PTEs from the parent's page tables into the child's page tables.</p></li>
                     62: <li><p>Block parent.</p></li>
                     63: <li><p>Child runs.  If PTEs were <span class="emphasis"><em>not</em></span> copied, take page fault to get
                     64:         a physical mapping for the text page at the current program counter.</p></li>
                     65: <li><p>Child execs, and unmaps the entire address space that was just
                     66:         created, and creates a new one.  This implies that the parent's
                     67:         vm_map has to be traversed to mark the COW portions not-COW.</p></li>
                     68: <li><p>Unblock parent.</p></li>
                     69: <li><p>Parent runs, takes page fault when modifying previously R/W
                     70:         data that was marked R/O for COW. No data is copied at this
                     71:         time.</p></li>
                     72: </ul></div>
                     73: 
                     74: <h4 class="title">
                     75: <a name="3bsd-vfork"></a>The 3.0BSD/NetBSD <code class="function">vfork()</code>, using address space
                     76: sharing (<a href="#vfork">top</a>)
                     77:   </h4>
                     78: 
                     79: <div class="itemizedlist"><ul type="disc">
                     80: <li><p>Take reference to parent's vmspace structure.</p></li>
                     81: <li><p>Block parent.</p></li>
                     82: <li><p>Child runs.  No page faults occur because the parent's page
                     83:         tables are being used, and the PTEs are already valid.</p></li>
                     84: <li><p>Child execs, deletes the reference it had to the parent's
                     85:        vmspace structure, and creates a new one.</p></li>
                     86: <li><p>Unblock parent.</p></li>
                     87: <li><p>Parent runs.  (No page faults occur because the parent's
                     88:         vm_map was not modified.)</p></li>
                     89: </ul></div>
                     90: 
                     91: <p>
                     92: So, in the case where you're going to fork and then exec, the latter
                     93: case is clearly faster. Even if your COW algorithms are good, you still
                     94: have to do a lot more work compared to the vmspace-sharing case!
                     95: </p>
                     96: <hr>Back to  <em><a href="./">NetBSD Documentation: Kernel</a></em><div class="navfoot">
                     97: <hr>
                     98: <table border="0" summary="Footer navigation"><tr>
                     99: <td class="foothome"><span class="foothome"><a href="../../."><img src="/images/NetBSD-flag.png" alt="[NetBSD flag]" border="0" width="90" height="90"></a></span></td>
                    100: <td class="foothome">
                    101: <span class="foothome"><a href="../../.">Home page</a></span><br>
                    102: </td>
                    103: </tr></table>
                    104: <hr>
                    105: </div>
                    106: <p></p>
                    107: <span class="footfeed"><a href="http://www.NetBSD.org/cgi-bin/feedback.cgi">(contact us)</a></span>
                    108: 
                    109:    
1.2     ! dsieger   110:   <span class="foottag">Generated from <em>%NetBSD: vfork.xml,v 1.1 2007/06/09 11:33:47 dsieger Exp %</em></span><br><span class="footcopy"><a href="/Misc/disclaimer.html"><span class="copyright">Copyright © 1994-2007 The NetBSD Foundation, Inc. </span>ALL RIGHTS RESERVED.<br>NetBSD<sup>®</sup> is a registered trademark of The NetBSD
1.1       dsieger   111:        Foundation, Inc.</a></span>
                    112: </div></body>
                    113: </html>

CVSweb <webmaster@jp.NetBSD.org>