1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>perlfork - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-gb">
<link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
<link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
<link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>
<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
<div id="page">
<div id="header">
<div id="homepage_link">
<a href="index.html"></a>
</div>
<div id="strapline">
Perl Programming Documentation
</div>
<div id="download_link" class="download">
<a href="http://www.perl.org/get.html">Download Perl</a>
</div>
<div id="explore_link" class="download">
<a id="explore_anchor" href="#">Explore</a>
</div>
</div>
<div id="body">
<div id="left_column">
<div class="side_group">
<div class="side_panel doc_panel">
<p>Manual</p>
<ul>
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">History / Changes</a>
<li><a href="index-licence.html">License</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Reference</p>
<ul>
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Special Variables</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platform Specific</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Modules</p>
<ul>
<li>
<a href="index-modules-A.html">A</a>
•
<a href="index-modules-B.html">B</a>
•
<a href="index-modules-C.html">C</a>
•
<a href="index-modules-D.html">D</a>
•
<a href="index-modules-E.html">E</a>
<li>
<a href="index-modules-F.html">F</a>
•
<a href="index-modules-G.html">G</a>
•
<a href="index-modules-H.html">H</a>
•
<a href="index-modules-I.html">I</a>
•
<a href="index-modules-L.html">L</a>
<li>
<a href="index-modules-M.html">M</a>
•
<a href="index-modules-N.html">N</a>
•
<a href="index-modules-O.html">O</a>
•
<a href="index-modules-P.html">P</a>
•
<a href="index-modules-S.html">S</a>
<li>
<a href="index-modules-T.html">T</a>
•
<a href="index-modules-U.html">U</a>
•
<a href="index-modules-X.html">X</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Tools</p>
<ul>
<li><a href="preferences.html">Preferences</a>
</ul>
</div>
</div>
</div>
<div id="centre_column">
<div id="content_header">
<div id="title_bar">
<div id="page_name">
<h1>perlfork</h1>
</div>
<div id="perl_version">
Perl 5 version 22.0 documentation
</div>
<div class="page_links" id="page_links_top">
<a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
</div>
<div class="page_links" id="page_links_bottom">
<a href="#" id="page_index_toggle">Show page index</a> •
<a href="#" id="recent_pages_toggle">Show recent pages</a>
</div>
<div id="search_form">
<form action="search.html" method="GET" id="search">
<input type="text" name="q" id="search_box" alt="Search">
</form>
</div>
</div>
<div id="breadcrumbs">
<a href="index.html">Home</a> >
<a href="index-language.html">Language reference</a> >
perlfork
</div>
</div>
<div id="content_body">
<!--[if lt IE 7]>
<div class="noscript">
<p>
<strong>It looks like you're using Internet Explorer 6. This is a very old
browser which does not offer full support for modern websites.</strong>
</p>
<p>
Unfortunately this means that this website will not work on
your computer.
</p>
<p>
Don't miss out though! To view the site (and get a better experience from
many other websites), simply upgrade to
<a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
or download an alternative browser such as
<a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
<a href="http://www.apple.com/safari/download/">Safari</a>, or
<a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
<p>
All of these browsers are free. If you're using a PC at work, you may
need to contact your IT administrator.
</p>
</div>
<![endif]-->
<noscript>
<div class="noscript">
<p>
<strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
or are running a non-JavaScript capable web browser.</strong>
</p>
<p>
To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
</div>
</noscript>
<div id="recent_pages" class="hud_container">
<div id="recent_pages_header" class="hud_header">
<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
<div id="recent_pages_topright" class="hud_topright"></div>
</div>
<div id="recent_pages_content" class="hud_content">
</div>
<div id="recent_pages_footer" class="hud_footer">
<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="recent_pages_resize" class="hud_resize"></div>
</div>
</div>
<div id="from_search"></div>
<h1>perlfork</h1>
<!-- -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Behavior-of-other-Perl-features-in-forked-pseudo-processes">Behavior of other Perl features in forked pseudo-processes</a><li><a href="#Resource-limits">Resource limits</a><li><a href="#Killing-the-parent-process">Killing the parent process</a><li><a href="#Lifetime-of-the-parent-process-and-pseudo-processes">Lifetime of the parent process and pseudo-processes</a></ul><li><a href="#CAVEATS-AND-LIMITATIONS">CAVEATS AND LIMITATIONS</a><li><a href="#PORTABILITY-CAVEATS">PORTABILITY CAVEATS</a><li><a href="#BUGS">BUGS</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlfork - Perl's fork() emulation</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li> NOTE: As of the 5.8.0 release, fork() emulation has considerably</li><li> matured. However, there are still a few known bugs and differences</li><li> from real fork() that might affect you. See the "BUGS" and</li><li> "CAVEATS AND LIMITATIONS" sections below.</li></ol></pre><p>Perl provides a fork() keyword that corresponds to the Unix system call
of the same name. On most Unix-like platforms where the fork() system
call is available, Perl's fork() simply calls it.</p>
<p>On some platforms such as Windows where the fork() system call is not
available, Perl can be built to emulate fork() at the interpreter level.
While the emulation is designed to be as compatible as possible with the
real fork() at the level of the Perl program, there are certain
important differences that stem from the fact that all the pseudo child
"processes" created this way live in the same real process as far as the
operating system is concerned.</p>
<p>This document provides a general overview of the capabilities and
limitations of the fork() emulation. Note that the issues discussed here
are not applicable to platforms where a real fork() is available and Perl
has been configured to use it.</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>The fork() emulation is implemented at the level of the Perl interpreter.
What this means in general is that running fork() will actually clone the
running interpreter and all its state, and run the cloned interpreter in
a separate thread, beginning execution in the new thread just after the
point where the fork() was called in the parent. We will refer to the
thread that implements this child "process" as the pseudo-process.</p>
<p>To the Perl program that called fork(), all this is designed to be
transparent. The parent returns from the fork() with a pseudo-process
ID that can be subsequently used in any process-manipulation functions;
the child returns from the fork() with a value of <code class="inline"><span class="n">0</span></code>
to signify that
it is the child pseudo-process.</p>
<a name="Behavior-of-other-Perl-features-in-forked-pseudo-processes"></a><h2>Behavior of other Perl features in forked pseudo-processes</h2>
<p>Most Perl features behave in a natural way within pseudo-processes.</p>
<ul>
<li><a name="%24%24-or-%24PROCESS_ID"></a><b>$$ or $PROCESS_ID</b>
<p>This special variable is correctly set to the pseudo-process ID.
It can be used to identify pseudo-processes within a particular
session. Note that this value is subject to recycling if any
pseudo-processes are launched after others have been wait()-ed on.</p>
</li>
<li><a name="%25ENV"></a><b>%ENV</b>
<p>Each pseudo-process maintains its own virtual environment. Modifications
to %ENV affect the virtual environment, and are only visible within that
pseudo-process, and in any processes (or pseudo-processes) launched from
it.</p>
</li>
<li><a name="chdir()-and-all-other-builtins-that-accept-filenames"></a><b>chdir() and all other builtins that accept filenames</b>
<p>Each pseudo-process maintains its own virtual idea of the current directory.
Modifications to the current directory using chdir() are only visible within
that pseudo-process, and in any processes (or pseudo-processes) launched from
it. All file and directory accesses from the pseudo-process will correctly
map the virtual working directory to the real working directory appropriately.</p>
</li>
<li><a name="wait()-and-waitpid()"></a><b>wait() and waitpid()</b>
<p>wait() and waitpid() can be passed a pseudo-process ID returned by fork().
These calls will properly wait for the termination of the pseudo-process
and return its status.</p>
</li>
<li><a name="kill()"></a><b>kill()</b>
<p><code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'KILL'</span><span class="cm">,</span> ...<span class="s">)</span></code>
can be used to terminate a pseudo-process by
passing it the ID returned by fork(). The outcome of kill on a pseudo-process
is unpredictable and it should not be used except
under dire circumstances, because the operating system may not
guarantee integrity of the process resources when a running thread is
terminated. The process which implements the pseudo-processes can be blocked
and the Perl interpreter hangs. Note that using <code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'KILL'</span><span class="cm">,</span> ...<span class="s">)</span></code>
on a
pseudo-process() may typically cause memory leaks, because the thread
that implements the pseudo-process does not get a chance to clean up
its resources.</p>
<p><code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'TERM'</span><span class="cm">,</span> ...<span class="s">)</span></code>
can also be used on pseudo-processes, but the
signal will not be delivered while the pseudo-process is blocked by a
system call, e.g. waiting for a socket to connect, or trying to read
from a socket with no data available. Starting in Perl 5.14 the
parent process will not wait for children to exit once they have been
signalled with <code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'TERM'</span><span class="cm">,</span> ...<span class="s">)</span></code>
to avoid deadlock during process
exit. You will have to explicitly call waitpid() to make sure the
child has time to clean-up itself, but you are then also responsible
that the child is not blocking on I/O either.</p>
</li>
<li><a name="exec()"></a><b>exec()</b>
<p>Calling exec() within a pseudo-process actually spawns the requested
executable in a separate process and waits for it to complete before
exiting with the same exit status as that process. This means that the
process ID reported within the running executable will be different from
what the earlier Perl fork() might have returned. Similarly, any process
manipulation functions applied to the ID returned by fork() will affect the
waiting pseudo-process that called exec(), not the real process it is
waiting for after the exec().</p>
<p>When exec() is called inside a pseudo-process then DESTROY methods and
END blocks will still be called after the external process returns.</p>
</li>
<li><a name="exit()"></a><b>exit()</b>
<p>exit() always exits just the executing pseudo-process, after automatically
wait()-ing for any outstanding child pseudo-processes. Note that this means
that the process as a whole will not exit unless all running pseudo-processes
have exited. See below for some limitations with open filehandles.</p>
</li>
<li><a name="Open-handles-to-files%2c-directories-and-network-sockets"></a><b>Open handles to files, directories and network sockets</b>
<p>All open handles are dup()-ed in pseudo-processes, so that closing
any handles in one process does not affect the others. See below for
some limitations.</p>
</li>
</ul>
<a name="Resource-limits"></a><h2>Resource limits</h2>
<p>In the eyes of the operating system, pseudo-processes created via the fork()
emulation are simply threads in the same process. This means that any
process-level limits imposed by the operating system apply to all
pseudo-processes taken together. This includes any limits imposed by the
operating system on the number of open file, directory and socket handles,
limits on disk space usage, limits on memory size, limits on CPU utilization
etc.</p>
<a name="Killing-the-parent-process"></a><h2>Killing the parent process</h2>
<p>If the parent process is killed (either using Perl's kill() builtin, or
using some external means) all the pseudo-processes are killed as well,
and the whole process exits.</p>
<a name="Lifetime-of-the-parent-process-and-pseudo-processes"></a><h2>Lifetime of the parent process and pseudo-processes</h2>
<p>During the normal course of events, the parent process and every
pseudo-process started by it will wait for their respective pseudo-children
to complete before they exit. This means that the parent and every
pseudo-child created by it that is also a pseudo-parent will only exit
after their pseudo-children have exited.</p>
<p>Starting with Perl 5.14 a parent will not wait() automatically
for any child that has been signalled with <code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'TERM'</span><span class="cm">,</span> ...<span class="s">)</span></code>
to avoid a deadlock in case the child is blocking on I/O and
never receives the signal.</p>
<a name="CAVEATS-AND-LIMITATIONS"></a><h1>CAVEATS AND LIMITATIONS</h1>
<ul>
<li><a name="BEGIN-blocks"></a><b>BEGIN blocks</b>
<p>The fork() emulation will not work entirely correctly when called from
within a BEGIN block. The forked copy will run the contents of the
BEGIN block, but will not continue parsing the source stream after the
BEGIN block. For example, consider the following code:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span></li><li> <a class="l_k" href="functions/fork.html">fork</a> <a class="l_k" href="functions/and.html">and</a> <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span> <span class="c"># fork child and exit the parent</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"inner\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"outer\n"</span><span class="sc">;</span></li></ol></pre><p>This will print:</p>
<pre class="verbatim"><ol><li> <span class="w">inner</span></li></ol></pre><p>rather than the expected:</p>
<pre class="verbatim"><ol><li> <span class="w">inner</span></li><li> <span class="w">outer</span></li></ol></pre><p>This limitation arises from fundamental technical difficulties in
cloning and restarting the stacks used by the Perl parser in the
middle of a parse.</p>
</li>
<li><a name="Open-filehandles"></a><b>Open filehandles</b>
<p>Any filehandles open at the time of the fork() will be dup()-ed. Thus,
the files can be closed independently in the parent and child, but beware
that the dup()-ed handles will still share the same seek pointer. Changing
the seek position in the parent will change it in the child and vice-versa.
One can avoid this by opening files that need distinct seek pointers
separately in the child.</p>
<p>On some operating systems, notably Solaris and Unixware, calling <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code>
from a child process will flush and close open filehandles in the parent,
thereby corrupting the filehandles. On these systems, calling <code class="inline"><span class="i">_exit</span><span class="s">(</span><span class="s">)</span></code>
is suggested instead. <code class="inline"><span class="i">_exit</span><span class="s">(</span><span class="s">)</span></code>
is available in Perl through the
<code class="inline"><span class="w">POSIX</span></code>
module. Please consult your system's manpages for more information
on this.</p>
</li>
<li><a name="Open-directory-handles"></a><b>Open directory handles</b>
<p>Perl will completely read from all open directory handles until they
reach the end of the stream. It will then seekdir() back to the
original location and all future readdir() requests will be fulfilled
from the cache buffer. That means that neither the directory handle held
by the parent process nor the one held by the child process will see
any changes made to the directory after the fork() call.</p>
<p>Note that rewinddir() has a similar limitation on Windows and will not
force readdir() to read the directory again either. Only a newly
opened directory handle will reflect changes to the directory.</p>
</li>
<li><a name="Forking-pipe-open()-not-yet-implemented"></a><b>Forking pipe open() not yet implemented</b>
<p>The <code class="inline"><a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">"|-"</span><span class="s">)</span></code>
and <code class="inline"><a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">BAR</span><span class="cm">,</span> <span class="q">"-|"</span><span class="s">)</span></code>
constructs are not yet
implemented. This limitation can be easily worked around in new code
by creating a pipe explicitly. The following example shows how to
write to a forked child:</p>
<pre class="verbatim"><ol><li> <span class="c"># simulate open(FOO, "|-")</span></li><li><a name="pipe_to_fork"></a> sub <span class="m">pipe_to_fork ($)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$parent</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/pipe.html">pipe</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$child</span><span class="cm">,</span> <span class="i">$parent</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <a class="l_k" href="functions/fork.html">fork</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">"fork() failed: $!"</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$pid</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$pid</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/close.html">close</a> <span class="i">$child</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li> <a class="l_k" href="functions/close.html">close</a> <span class="i">$parent</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">STDIN</span><span class="cm">,</span> <span class="q">"<&="</span> . <a class="l_k" href="functions/fileno.html">fileno</a><span class="s">(</span><span class="i">$child</span><span class="s">)</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="i">$pid</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">pipe_to_fork</span><span class="s">(</span><span class="q">'FOO'</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li> <span class="c"># parent</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">FOO</span> <span class="q">"pipe_to_fork\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/close.html">close</a> <span class="w">FOO</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li> <span class="c"># child</span></li><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q"><STDIN></span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span></li><li> <a class="l_k" href="functions/exit.html">exit</a><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>And this one reads from the child:</p>
<pre class="verbatim"><ol><li> <span class="c"># simulate open(FOO, "-|")</span></li><li><a name="pipe_from_fork"></a> sub <span class="m">pipe_from_fork ($)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$parent</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/pipe.html">pipe</a> <span class="i">$parent</span><span class="cm">,</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$child</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$pid</span> = <a class="l_k" href="functions/fork.html">fork</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">"fork() failed: $!"</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$pid</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$pid</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/close.html">close</a> <span class="i">$child</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li> <a class="l_k" href="functions/close.html">close</a> <span class="i">$parent</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">STDOUT</span><span class="cm">,</span> <span class="q">">&="</span> . <a class="l_k" href="functions/fileno.html">fileno</a><span class="s">(</span><span class="i">$child</span><span class="s">)</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="i">$pid</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">pipe_from_fork</span><span class="s">(</span><span class="q">'BAR'</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li> <span class="c"># parent</span></li><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q"><BAR></span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="s">}</span></li><li> <a class="l_k" href="functions/close.html">close</a> <span class="w">BAR</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li> <span class="c"># child</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"pipe_from_fork\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/exit.html">exit</a><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>Forking pipe open() constructs will be supported in future.</p>
</li>
<li><a name="Global-state-maintained-by-XSUBs"></a><b>Global state maintained by XSUBs</b>
<p>External subroutines (XSUBs) that maintain their own global state may
not work correctly. Such XSUBs will either need to maintain locks to
protect simultaneous access to global data from different pseudo-processes,
or maintain all their state on the Perl symbol table, which is copied
naturally when fork() is called. A callback mechanism that provides
extensions an opportunity to clone their state will be provided in the
near future.</p>
</li>
<li><a name="Interpreter-embedded-in-larger-application"></a><b>Interpreter embedded in larger application</b>
<p>The fork() emulation may not behave as expected when it is executed in an
application which embeds a Perl interpreter and calls Perl APIs that can
evaluate bits of Perl code. This stems from the fact that the emulation
only has knowledge about the Perl interpreter's own data structures and
knows nothing about the containing application's state. For example, any
state carried on the application's own call stack is out of reach.</p>
</li>
<li><a name="Thread-safety-of-extensions"></a><b>Thread-safety of extensions</b>
<p>Since the fork() emulation runs code in multiple threads, extensions
calling into non-thread-safe libraries may not work reliably when
calling fork(). As Perl's threading support gradually becomes more
widely adopted even on platforms with a native fork(), such extensions
are expected to be fixed for thread-safety.</p>
</li>
</ul>
<a name="PORTABILITY-CAVEATS"></a><h1>PORTABILITY CAVEATS</h1>
<p>In portable Perl code, <code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="n">9</span><span class="cm">,</span> <span class="i">$child</span><span class="s">)</span></code>
must not be used on forked processes.
Killing a forked process is unsafe and has unpredictable results.
See <a href="#kill()">kill()</a>, above.</p>
<a name="BUGS"></a><h1>BUGS</h1>
<ul>
<li>
<p>Having pseudo-process IDs be negative integers breaks down for the integer
<code class="inline"><span class="n">-1</span></code>
because the wait() and waitpid() functions treat this number as
being special. The tacit assumption in the current implementation is that
the system never allocates a thread ID of <code class="inline"><span class="n">1</span></code>
for user threads. A better
representation for pseudo-process IDs will be implemented in future.</p>
</li>
<li>
<p>In certain cases, the OS-level handles created by the pipe(), socket(),
and accept() operators are apparently not duplicated accurately in
pseudo-processes. This only happens in some situations, but where it
does happen, it may result in deadlocks between the read and write ends
of pipe handles, or inability to send or receive data across socket
handles.</p>
</li>
<li>
<p>This document may be incomplete in some respects.</p>
</li>
</ul>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Support for concurrent interpreters and the fork() emulation was implemented
by ActiveState, with funding from Microsoft Corporation.</p>
<p>This document is authored and maintained by Gurusamy Sarathy
<gsar@activestate.com>.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="functions/fork.html">fork</a>, <a href="perlipc.html">perlipc</a></p>
<div id="page_index" class="hud_container">
<div id="page_index_header" class="hud_header">
<div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
<div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
<div id="page_index_topright" class="hud_topright"></div>
</div>
<div id="page_index_content" class="hud_content">
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Behavior-of-other-Perl-features-in-forked-pseudo-processes">Behavior of other Perl features in forked pseudo-processes</a><li><a href="#Resource-limits">Resource limits</a><li><a href="#Killing-the-parent-process">Killing the parent process</a><li><a href="#Lifetime-of-the-parent-process-and-pseudo-processes">Lifetime of the parent process and pseudo-processes</a></ul><li><a href="#CAVEATS-AND-LIMITATIONS">CAVEATS AND LIMITATIONS</a><li><a href="#PORTABILITY-CAVEATS">PORTABILITY CAVEATS</a><li><a href="#BUGS">BUGS</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
</div>
<div id="page_index_footer" class="hud_footer">
<div id="page_index_bottomleft" class="hud_bottomleft"></div>
<div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="page_index_resize" class="hud_resize"></div>
</div>
</div>
</div>
<div id="content_footer">
</div>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div id="footer_content">
<div id="footer_strapline">
perldoc.perl.org - Official documentation for the Perl programming language
</div>
<div id="footer_links">
<div id="address">
<p class="name">Contact details</p>
<p class="address">
Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
</p>
<p class="contact">
Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
</p>
</div>
<ul class="f1">
<li>Manual
<ul class="f2">
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">Changes</a>
</ul>
<li>Reference
<ul class="f2">
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Variables</a>
</ul>
<li>Modules
<ul class="f2">
<li><a href="index-modules-A.html">Modules</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
</ul>
<li>Misc
<ul class="f2">
<li><a href="index-licence.html">License</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platforms</a>
</ul> </ul>
<div class="clear"></div>
</div>
</div>
<div id="footer_end">
</div>
</div>
</div>
<script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
<script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
perldoc.setPath(0);
perldoc.pageName = 'perlfork';
perldoc.pageAddress = 'perlfork.html';
perldoc.contentPage = 1;
explorePerl.render();
explorePerl.addEvents('explore_anchor');
</script>
</body>
</html>
|