Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
11 KiB

  1. =head1 NAME
  2. perlfork - Perl's fork() emulation (EXPERIMENTAL, subject to change)
  3. =head1 SYNOPSIS
  4. WARNING: As of the 5.6.1 release, the fork() emulation continues
  5. to be an experimental feature. Use in production applications is
  6. not recommended. See the "BUGS" and "CAVEATS AND LIMITATIONS"
  7. sections below.
  8. Perl provides a fork() keyword that corresponds to the Unix system call
  9. of the same name. On most Unix-like platforms where the fork() system
  10. call is available, Perl's fork() simply calls it.
  11. On some platforms such as Windows where the fork() system call is not
  12. available, Perl can be built to emulate fork() at the interpreter level.
  13. While the emulation is designed to be as compatible as possible with the
  14. real fork() at the level of the Perl program, there are certain
  15. important differences that stem from the fact that all the pseudo child
  16. "processes" created this way live in the same real process as far as the
  17. operating system is concerned.
  18. This document provides a general overview of the capabilities and
  19. limitations of the fork() emulation. Note that the issues discussed here
  20. are not applicable to platforms where a real fork() is available and Perl
  21. has been configured to use it.
  22. =head1 DESCRIPTION
  23. The fork() emulation is implemented at the level of the Perl interpreter.
  24. What this means in general is that running fork() will actually clone the
  25. running interpreter and all its state, and run the cloned interpreter in
  26. a separate thread, beginning execution in the new thread just after the
  27. point where the fork() was called in the parent. We will refer to the
  28. thread that implements this child "process" as the pseudo-process.
  29. To the Perl program that called fork(), all this is designed to be
  30. transparent. The parent returns from the fork() with a pseudo-process
  31. ID that can be subsequently used in any process manipulation functions;
  32. the child returns from the fork() with a value of C<0> to signify that
  33. it is the child pseudo-process.
  34. =head2 Behavior of other Perl features in forked pseudo-processes
  35. Most Perl features behave in a natural way within pseudo-processes.
  36. =over 8
  37. =item $$ or $PROCESS_ID
  38. This special variable is correctly set to the pseudo-process ID.
  39. It can be used to identify pseudo-processes within a particular
  40. session. Note that this value is subject to recycling if any
  41. pseudo-processes are launched after others have been wait()-ed on.
  42. =item %ENV
  43. Each pseudo-process maintains its own virtual environment. Modifications
  44. to %ENV affect the virtual environment, and are only visible within that
  45. pseudo-process, and in any processes (or pseudo-processes) launched from
  46. it.
  47. =item chdir() and all other builtins that accept filenames
  48. Each pseudo-process maintains its own virtual idea of the current directory.
  49. Modifications to the current directory using chdir() are only visible within
  50. that pseudo-process, and in any processes (or pseudo-processes) launched from
  51. it. All file and directory accesses from the pseudo-process will correctly
  52. map the virtual working directory to the real working directory appropriately.
  53. =item wait() and waitpid()
  54. wait() and waitpid() can be passed a pseudo-process ID returned by fork().
  55. These calls will properly wait for the termination of the pseudo-process
  56. and return its status.
  57. =item kill()
  58. kill() can be used to terminate a pseudo-process by passing it the ID returned
  59. by fork(). This should not be used except under dire circumstances, because
  60. the operating system may not guarantee integrity of the process resources
  61. when a running thread is terminated. Note that using kill() on a
  62. pseudo-process() may typically cause memory leaks, because the thread that
  63. implements the pseudo-process does not get a chance to clean up its resources.
  64. =item exec()
  65. Calling exec() within a pseudo-process actually spawns the requested
  66. executable in a separate process and waits for it to complete before
  67. exiting with the same exit status as that process. This means that the
  68. process ID reported within the running executable will be different from
  69. what the earlier Perl fork() might have returned. Similarly, any process
  70. manipulation functions applied to the ID returned by fork() will affect the
  71. waiting pseudo-process that called exec(), not the real process it is
  72. waiting for after the exec().
  73. =item exit()
  74. exit() always exits just the executing pseudo-process, after automatically
  75. wait()-ing for any outstanding child pseudo-processes. Note that this means
  76. that the process as a whole will not exit unless all running pseudo-processes
  77. have exited.
  78. =item Open handles to files, directories and network sockets
  79. All open handles are dup()-ed in pseudo-processes, so that closing
  80. any handles in one process does not affect the others. See below for
  81. some limitations.
  82. =back
  83. =head2 Resource limits
  84. In the eyes of the operating system, pseudo-processes created via the fork()
  85. emulation are simply threads in the same process. This means that any
  86. process-level limits imposed by the operating system apply to all
  87. pseudo-processes taken together. This includes any limits imposed by the
  88. operating system on the number of open file, directory and socket handles,
  89. limits on disk space usage, limits on memory size, limits on CPU utilization
  90. etc.
  91. =head2 Killing the parent process
  92. If the parent process is killed (either using Perl's kill() builtin, or
  93. using some external means) all the pseudo-processes are killed as well,
  94. and the whole process exits.
  95. =head2 Lifetime of the parent process and pseudo-processes
  96. During the normal course of events, the parent process and every
  97. pseudo-process started by it will wait for their respective pseudo-children
  98. to complete before they exit. This means that the parent and every
  99. pseudo-child created by it that is also a pseudo-parent will only exit
  100. after their pseudo-children have exited.
  101. A way to mark a pseudo-processes as running detached from their parent (so
  102. that the parent would not have to wait() for them if it doesn't want to)
  103. will be provided in future.
  104. =head2 CAVEATS AND LIMITATIONS
  105. =over 8
  106. =item BEGIN blocks
  107. The fork() emulation will not work entirely correctly when called from
  108. within a BEGIN block. The forked copy will run the contents of the
  109. BEGIN block, but will not continue parsing the source stream after the
  110. BEGIN block. For example, consider the following code:
  111. BEGIN {
  112. fork and exit; # fork child and exit the parent
  113. print "inner\n";
  114. }
  115. print "outer\n";
  116. This will print:
  117. inner
  118. rather than the expected:
  119. inner
  120. outer
  121. This limitation arises from fundamental technical difficulties in
  122. cloning and restarting the stacks used by the Perl parser in the
  123. middle of a parse.
  124. =item Open filehandles
  125. Any filehandles open at the time of the fork() will be dup()-ed. Thus,
  126. the files can be closed independently in the parent and child, but beware
  127. that the dup()-ed handles will still share the same seek pointer. Changing
  128. the seek position in the parent will change it in the child and vice-versa.
  129. One can avoid this by opening files that need distinct seek pointers
  130. separately in the child.
  131. =item Forking pipe open() not yet implemented
  132. The C<open(FOO, "|-")> and C<open(BAR, "-|")> constructs are not yet
  133. implemented. This limitation can be easily worked around in new code
  134. by creating a pipe explicitly. The following example shows how to
  135. write to a forked child:
  136. # simulate open(FOO, "|-")
  137. sub pipe_to_fork ($) {
  138. my $parent = shift;
  139. pipe my $child, $parent or die;
  140. my $pid = fork();
  141. die "fork() failed: $!" unless defined $pid;
  142. if ($pid) {
  143. close $child;
  144. }
  145. else {
  146. close $parent;
  147. open(STDIN, "<&=" . fileno($child)) or die;
  148. }
  149. $pid;
  150. }
  151. if (pipe_to_fork('FOO')) {
  152. # parent
  153. print FOO "pipe_to_fork\n";
  154. close FOO;
  155. }
  156. else {
  157. # child
  158. while (<STDIN>) { print; }
  159. close STDIN;
  160. exit(0);
  161. }
  162. And this one reads from the child:
  163. # simulate open(FOO, "-|")
  164. sub pipe_from_fork ($) {
  165. my $parent = shift;
  166. pipe $parent, my $child or die;
  167. my $pid = fork();
  168. die "fork() failed: $!" unless defined $pid;
  169. if ($pid) {
  170. close $child;
  171. }
  172. else {
  173. close $parent;
  174. open(STDOUT, ">&=" . fileno($child)) or die;
  175. }
  176. $pid;
  177. }
  178. if (pipe_from_fork('BAR')) {
  179. # parent
  180. while (<BAR>) { print; }
  181. close BAR;
  182. }
  183. else {
  184. # child
  185. print "pipe_from_fork\n";
  186. close STDOUT;
  187. exit(0);
  188. }
  189. Forking pipe open() constructs will be supported in future.
  190. =item Global state maintained by XSUBs
  191. External subroutines (XSUBs) that maintain their own global state may
  192. not work correctly. Such XSUBs will either need to maintain locks to
  193. protect simultaneous access to global data from different pseudo-processes,
  194. or maintain all their state on the Perl symbol table, which is copied
  195. naturally when fork() is called. A callback mechanism that provides
  196. extensions an opportunity to clone their state will be provided in the
  197. near future.
  198. =item Interpreter embedded in larger application
  199. The fork() emulation may not behave as expected when it is executed in an
  200. application which embeds a Perl interpreter and calls Perl APIs that can
  201. evaluate bits of Perl code. This stems from the fact that the emulation
  202. only has knowledge about the Perl interpreter's own data structures and
  203. knows nothing about the containing application's state. For example, any
  204. state carried on the application's own call stack is out of reach.
  205. =item Thread-safety of extensions
  206. Since the fork() emulation runs code in multiple threads, extensions
  207. calling into non-thread-safe libraries may not work reliably when
  208. calling fork(). As Perl's threading support gradually becomes more
  209. widely adopted even on platforms with a native fork(), such extensions
  210. are expected to be fixed for thread-safety.
  211. =back
  212. =head1 BUGS
  213. =over 8
  214. =item *
  215. Perl's regular expression engine currently does not play very nicely
  216. with the fork() emulation. There are known race conditions arising
  217. from the regular expression engine modifying state carried in the opcode
  218. tree at run time (the fork() emulation relies on the opcode tree being
  219. immutable). This typically happens when the regex contains paren groups
  220. or variables interpolated within it that force a run time recompilation
  221. of the regex. Due to this major bug, the fork() emulation is not
  222. recommended for use in production applications at this time.
  223. =item *
  224. Having pseudo-process IDs be negative integers breaks down for the integer
  225. C<-1> because the wait() and waitpid() functions treat this number as
  226. being special. The tacit assumption in the current implementation is that
  227. the system never allocates a thread ID of C<1> for user threads. A better
  228. representation for pseudo-process IDs will be implemented in future.
  229. =item *
  230. This document may be incomplete in some respects.
  231. =back
  232. =head1 AUTHOR
  233. Support for concurrent interpreters and the fork() emulation was implemented
  234. by ActiveState, with funding from Microsoft Corporation.
  235. This document is authored and maintained by Gurusamy Sarathy
  236. E<lt>[email protected]<gt>.
  237. =head1 SEE ALSO
  238. L<perlfunc/"fork">, L<perlipc>
  239. =cut