home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _2e2caa361a8a93cbdd1731bec0db00b4 < prev    next >
Encoding:
Text File  |  2000-03-23  |  16.1 KB  |  323 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>perlfork - Perl's fork emulation</TITLE>
  4. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  5. <LINK REV="made" HREF="mailto:">
  6. </HEAD>
  7.  
  8. <BODY>
  9. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  10. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  11. <STRONG><P CLASS=block> perlfork - Perl's fork emulation</P></STRONG>
  12. </TD></TR>
  13. </TABLE>
  14.  
  15. <A NAME="__index__"></A>
  16. <!-- INDEX BEGIN -->
  17.  
  18. <UL>
  19.  
  20.     <LI><A HREF="#name">NAME</A></LI>
  21.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  22.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  23.     <UL>
  24.  
  25.         <LI><A HREF="#behavior of other perl features in forked pseudoprocesses">Behavior of other Perl features in forked pseudo-processes</A></LI>
  26.         <LI><A HREF="#resource limits">Resource limits</A></LI>
  27.         <LI><A HREF="#killing the parent process">Killing the parent process</A></LI>
  28.         <LI><A HREF="#lifetime of the parent process and pseudoprocesses">Lifetime of the parent process and pseudo-processes</A></LI>
  29.         <LI><A HREF="#caveats and limitations">CAVEATS AND LIMITATIONS</A></LI>
  30.     </UL>
  31.  
  32.     <LI><A HREF="#bugs">BUGS</A></LI>
  33.     <LI><A HREF="#author">AUTHOR</A></LI>
  34.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  35. </UL>
  36. <!-- INDEX END -->
  37.  
  38. <HR>
  39. <P>
  40. <H1><A NAME="name">NAME</A></H1>
  41. <P>perlfork - Perl's <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation</P>
  42. <P>
  43. <HR>
  44. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  45. <P>Perl provides a <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> keyword that corresponds to the Unix system call
  46. of the same name.  On most Unix-like platforms where the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> system
  47. call is available, Perl's <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> simply calls it.</P>
  48. <P>On some platforms such as Windows where the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> system call is not
  49. available, Perl can be built to emulate <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> at the interpreter level.
  50. While the emulation is designed to be as compatible as possible with the
  51. real <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> at the the level of the Perl program, there are certain
  52. important differences that stem from the fact that all the pseudo child
  53. ``processes'' created this way live in the same real process as far as the
  54. operating system is concerned.</P>
  55. <P>This document provides a general overview of the capabilities and
  56. limitations of the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation.  Note that the issues discussed here
  57. are not applicable to platforms where a real <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> is available and Perl
  58. has been configured to use it.</P>
  59. <P>
  60. <HR>
  61. <H1><A NAME="description">DESCRIPTION</A></H1>
  62. <P>The <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation is implemented at the level of the Perl interpreter.
  63. What this means in general is that running <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> will actually clone the
  64. running interpreter and all its state, and run the cloned interpreter in
  65. a separate thread, beginning execution in the new thread just after the
  66. point where the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> was called in the parent.  We will refer to the
  67. thread that implements this child ``process'' as the pseudo-process.</P>
  68. <P>To the Perl program that called fork(), all this is designed to be
  69. transparent.  The parent returns from the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> with a pseudo-process
  70. ID that can be subsequently used in any process manipulation functions;
  71. the child returns from the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> with a value of <CODE>0</CODE> to signify that
  72. it is the child pseudo-process.</P>
  73. <P>
  74. <H2><A NAME="behavior of other perl features in forked pseudoprocesses">Behavior of other Perl features in forked pseudo-processes</A></H2>
  75. <P>Most Perl features behave in a natural way within pseudo-processes.</P>
  76. <DL>
  77. <DT><STRONG><A NAME="item_%24%24_or_%24PROCESS_ID">$$ or $PROCESS_ID</A></STRONG><BR>
  78. <DD>
  79. This special variable is correctly set to the pseudo-process ID.
  80. It can be used to identify pseudo-processes within a particular
  81. session.  Note that this value is subject to recycling if any
  82. pseudo-processes are launched after others have been wait()-ed on.
  83. <P></P>
  84. <DT><STRONG><A NAME="item_%ENV">%ENV</A></STRONG><BR>
  85. <DD>
  86. Each pseudo-process maintains its own virtual enviroment.  Modifications
  87. to %ENV affect the virtual environment, and are only visible within that
  88. pseudo-process, and in any processes (or pseudo-processes) launched from
  89. it.
  90. <P></P>
  91. <DT><STRONG><A NAME="item_chdir"><CODE>chdir()</CODE> and all other builtins that accept filenames</A></STRONG><BR>
  92. <DD>
  93. Each pseudo-process maintains its own virtual idea of the current directory.
  94. Modifications to the current directory using <A HREF="#item_chdir"><CODE>chdir()</CODE></A> are only visible within
  95. that pseudo-process, and in any processes (or pseudo-processes) launched from
  96. it.  All file and directory accesses from the pseudo-process will correctly
  97. map the virtual working directory to the real working directory appropriately.
  98. <P></P>
  99. <DT><STRONG><A NAME="item_wait"><CODE>wait()</CODE> and <A HREF="../../lib/Pod/perlfunc.html#item_waitpid"><CODE>waitpid()</CODE></A></A></STRONG><BR>
  100. <DD>
  101. <A HREF="#item_wait"><CODE>wait()</CODE></A> and <A HREF="../../lib/Pod/perlfunc.html#item_waitpid"><CODE>waitpid()</CODE></A> can be passed a pseudo-process ID returned by fork().
  102. These calls will properly wait for the termination of the pseudo-process
  103. and return its status.
  104. <P></P>
  105. <DT><STRONG><A NAME="item_kill"><CODE>kill()</CODE></A></STRONG><BR>
  106. <DD>
  107. <A HREF="#item_kill"><CODE>kill()</CODE></A> can be used to terminate a pseudo-process by passing it the ID returned
  108. by fork().  This should not be used except under dire circumstances, because
  109. the operating system may not guarantee integrity of the process resources
  110. when a running thread is terminated.  Note that using <A HREF="#item_kill"><CODE>kill()</CODE></A> on a
  111. pseudo-process() may typically cause memory leaks, because the thread that
  112. implements the pseudo-process does not get a chance to clean up its resources.
  113. <P></P>
  114. <DT><STRONG><A NAME="item_exec"><CODE>exec()</CODE></A></STRONG><BR>
  115. <DD>
  116. Calling <A HREF="#item_exec"><CODE>exec()</CODE></A> within a pseudo-process actually spawns the requested
  117. executable in a separate process and waits for it to complete before
  118. exiting with the same exit status as that process.  This means that the
  119. process ID reported within the running executable will be different from
  120. what the earlier Perl <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> might have returned.  Similarly, any process
  121. manipulation functions applied to the ID returned by <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> will affect the
  122. waiting pseudo-process that called exec(), not the real process it is
  123. waiting for after the exec().
  124. <P></P>
  125. <DT><STRONG><A NAME="item_exit"><CODE>exit()</CODE></A></STRONG><BR>
  126. <DD>
  127. <A HREF="#item_exit"><CODE>exit()</CODE></A> always exits just the executing pseudo-process, after automatically
  128. wait()-ing for any outstanding child pseudo-processes.  Note that this means
  129. that the process as a whole will not exit unless all running pseudo-processes
  130. have exited.
  131. <P></P>
  132. <DT><STRONG><A NAME="item_Open_handles_to_files%2C_directories_and_network_s">Open handles to files, directories and network sockets</A></STRONG><BR>
  133. <DD>
  134. All open handles are dup()-ed in pseudo-processes, so that closing
  135. any handles in one process does not affect the others.  See below for
  136. some limitations.
  137. <P></P></DL>
  138. <P>
  139. <H2><A NAME="resource limits">Resource limits</A></H2>
  140. <P>In the eyes of the operating system, pseudo-processes created via the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A>
  141. emulation are simply threads in the same process.  This means that any
  142. process-level limits imposed by the operating system apply to all
  143. pseudo-processes taken together.  This includes any limits imposed by the
  144. operating system on the number of open file, directory and socket handles,
  145. limits on disk space usage, limits on memory size, limits on CPU utilization
  146. etc.</P>
  147. <P>
  148. <H2><A NAME="killing the parent process">Killing the parent process</A></H2>
  149. <P>If the parent process is killed (either using Perl's <A HREF="#item_kill"><CODE>kill()</CODE></A> builtin, or
  150. using some external means) all the pseudo-processes are killed as well,
  151. and the whole process exits.</P>
  152. <P>
  153. <H2><A NAME="lifetime of the parent process and pseudoprocesses">Lifetime of the parent process and pseudo-processes</A></H2>
  154. <P>During the normal course of events, the parent process and every
  155. pseudo-process started by it will wait for their respective pseudo-children
  156. to complete before they exit.  This means that the parent and every
  157. pseudo-child created by it that is also a pseudo-parent will only exit
  158. after their pseudo-children have exited.</P>
  159. <P>A way to mark a pseudo-processes as running detached from their parent (so
  160. that the parent would not have to <A HREF="#item_wait"><CODE>wait()</CODE></A> for them if it doesn't want to)
  161. will be provided in future.</P>
  162. <P>
  163. <H2><A NAME="caveats and limitations">CAVEATS AND LIMITATIONS</A></H2>
  164. <DL>
  165. <DT><STRONG><A NAME="item_BEGIN_blocks">BEGIN blocks</A></STRONG><BR>
  166. <DD>
  167. The <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation will not work entirely correctly when called from
  168. within a BEGIN block.  The forked copy will run the contents of the
  169. BEGIN block, but will not continue parsing the source stream after the
  170. BEGIN block.  For example, consider the following code:
  171. <PRE>
  172.     BEGIN {
  173.         fork and exit;          # fork child and exit the parent
  174.         print "inner\n";
  175.     }
  176.     print "outer\n";</PRE>
  177. <P>This will print:</P>
  178. <PRE>
  179.     inner</PRE>
  180. <P>rather than the expected:</P>
  181. <PRE>
  182.     inner
  183.     outer</PRE>
  184. <P>This limitation arises from fundamental technical difficulties in
  185. cloning and restarting the stacks used by the Perl parser in the
  186. middle of a parse.</P>
  187. <P></P>
  188. <DT><STRONG><A NAME="item_Open_filehandles">Open filehandles</A></STRONG><BR>
  189. <DD>
  190. Any filehandles open at the time of the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> will be dup()-ed.  Thus,
  191. the files can be closed independently in the parent and child, but beware
  192. that the dup()-ed handles will still share the same seek pointer.  Changing
  193. the seek position in the parent will change it in the child and vice-versa.
  194. One can avoid this by opening files that need distinct seek pointers
  195. separately in the child.
  196. <P></P>
  197. <DT><STRONG><A NAME="item_open">Forking pipe <CODE>open()</CODE> not yet implemented</A></STRONG><BR>
  198. <DD>
  199. The <A HREF="#item_open"><CODE>open(FOO, "|-")</CODE></A> and <A HREF="#item_open"><CODE>open(BAR, "-|")</CODE></A> constructs are not yet
  200. implemented.  This limitation can be easily worked around in new code
  201. by creating a pipe explicitly.  The following example shows how to
  202. write to a forked child:
  203. <PRE>
  204.     # simulate open(FOO, "|-")
  205.     sub pipe_to_fork ($) {
  206.         my $parent = shift;
  207.         pipe my $child, $parent or die;
  208.         my $pid = fork();
  209.         die "fork() failed: $!" unless defined $pid;
  210.         if ($pid) {
  211.             close $child;
  212.         }
  213.         else {
  214.             close $parent;
  215.             open(STDIN, "<&=" . fileno($child)) or die;
  216.         }
  217.         $pid;
  218.     }</PRE>
  219. <PRE>
  220.     if (pipe_to_fork('FOO')) {
  221.         # parent
  222.         print FOO "pipe_to_fork\n";
  223.         close FOO;
  224.     }
  225.     else {
  226.         # child
  227.         while (<STDIN>) { print; }
  228.         close STDIN;
  229.         exit(0);
  230.     }</PRE>
  231. <P>And this one reads from the child:</P>
  232. <PRE>
  233.     # simulate open(FOO, "-|")
  234.     sub pipe_from_fork ($) {
  235.         my $parent = shift;
  236.         pipe $parent, my $child or die;
  237.         my $pid = fork();
  238.         die "fork() failed: $!" unless defined $pid;
  239.         if ($pid) {
  240.             close $child;
  241.         }
  242.         else {
  243.             close $parent;
  244.             open(STDOUT, ">&=" . fileno($child)) or die;
  245.         }
  246.         $pid;
  247.     }</PRE>
  248. <PRE>
  249.     if (pipe_from_fork('BAR')) {
  250.         # parent
  251.         while (<BAR>) { print; }
  252.         close BAR;
  253.     }
  254.     else {
  255.         # child
  256.         print "pipe_from_fork\n";
  257.         close STDOUT;
  258.         exit(0);
  259.     }</PRE>
  260. <P>Forking pipe <A HREF="#item_open"><CODE>open()</CODE></A> constructs will be supported in future.</P>
  261. <P></P>
  262. <DT><STRONG><A NAME="item_Global_state_maintained_by_XSUBs">Global state maintained by XSUBs</A></STRONG><BR>
  263. <DD>
  264. External subroutines (XSUBs) that maintain their own global state may
  265. not work correctly.  Such XSUBs will either need to maintain locks to
  266. protect simultaneous access to global data from different pseudo-processes,
  267. or maintain all their state on the Perl symbol table, which is copied
  268. naturally when <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> is called.  A callback mechanism that provides
  269. extensions an opportunity to clone their state will be provided in the
  270. near future.
  271. <P></P>
  272. <DT><STRONG><A NAME="item_Interpreter_embedded_in_larger_application">Interpreter embedded in larger application</A></STRONG><BR>
  273. <DD>
  274. The <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation may not behave as expected when it is executed in an
  275. application which embeds a Perl interpreter and calls Perl APIs that can
  276. evaluate bits of Perl code.  This stems from the fact that the emulation
  277. only has knowledge about the Perl interpreter's own data structures and
  278. knows nothing about the containing application's state.  For example, any
  279. state carried on the application's own call stack is out of reach.
  280. <P></P>
  281. <DT><STRONG><A NAME="item_Thread%2Dsafety_of_extensions">Thread-safety of extensions</A></STRONG><BR>
  282. <DD>
  283. Since the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation runs code in multiple threads, extensions
  284. calling into non-thread-safe libraries may not work reliably when
  285. calling fork().  As Perl's threading support gradually becomes more
  286. widely adopted even on platforms with a native fork(), such extensions
  287. are expected to be fixed for thread-safety.
  288. <P></P></DL>
  289. <P>
  290. <HR>
  291. <H1><A NAME="bugs">BUGS</A></H1>
  292. <UL>
  293. <LI>
  294. Having pseudo-process IDs be negative integers breaks down for the integer
  295. <CODE>-1</CODE> because the <A HREF="#item_wait"><CODE>wait()</CODE></A> and <A HREF="../../lib/Pod/perlfunc.html#item_waitpid"><CODE>waitpid()</CODE></A> functions treat this number as
  296. being special.  The tacit assumption in the current implementation is that
  297. the system never allocates a thread ID of <CODE>1</CODE> for user threads.  A better
  298. representation for pseudo-process IDs will be implemented in future.
  299. <P></P>
  300. <LI>
  301. This document may be incomplete in some respects.
  302. <P></P></UL>
  303. <P>
  304. <HR>
  305. <H1><A NAME="author">AUTHOR</A></H1>
  306. <P>Support for concurrent interpreters and the <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> emulation was implemented
  307. by ActiveState, with funding from Microsoft Corporation.</P>
  308. <P>This document is authored and maintained by Gurusamy Sarathy
  309. <<A HREF="mailto:gsar@activestate.com">gsar@activestate.com</A>>.</P>
  310. <P>
  311. <HR>
  312. <H1><A NAME="see also">SEE ALSO</A></H1>
  313. <P><A HREF="../../lib/Pod/perlfunc.html#fork">fork in the perlfunc manpage</A>, <A HREF="../../lib/Pod/perlipc.html">the perlipc manpage</A></P>
  314. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  315. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  316. <STRONG><P CLASS=block> perlfork - Perl's fork emulation</P></STRONG>
  317. </TD></TR>
  318. </TABLE>
  319.  
  320. </BODY>
  321.  
  322. </HTML>
  323.