| 1 | =head1 NAME |
| 2 | |
| 3 | perlfork - Perl's fork() emulation |
| 4 | |
| 5 | =head1 SYNOPSIS |
| 6 | |
| 7 | NOTE: As of the 5.8.0 release, fork() emulation has considerably |
| 8 | matured. However, there are still a few known bugs and differences |
| 9 | from real fork() that might affect you. See the "BUGS" and |
| 10 | "CAVEATS AND LIMITATIONS" sections below. |
| 11 | |
| 12 | Perl provides a fork() keyword that corresponds to the Unix system call |
| 13 | of the same name. On most Unix-like platforms where the fork() system |
| 14 | call is available, Perl's fork() simply calls it. |
| 15 | |
| 16 | On some platforms such as Windows where the fork() system call is not |
| 17 | available, Perl can be built to emulate fork() at the interpreter level. |
| 18 | While the emulation is designed to be as compatible as possible with the |
| 19 | real fork() at the level of the Perl program, there are certain |
| 20 | important differences that stem from the fact that all the pseudo child |
| 21 | "processes" created this way live in the same real process as far as the |
| 22 | operating system is concerned. |
| 23 | |
| 24 | This document provides a general overview of the capabilities and |
| 25 | limitations of the fork() emulation. Note that the issues discussed here |
| 26 | are not applicable to platforms where a real fork() is available and Perl |
| 27 | has been configured to use it. |
| 28 | |
| 29 | =head1 DESCRIPTION |
| 30 | |
| 31 | The fork() emulation is implemented at the level of the Perl interpreter. |
| 32 | What this means in general is that running fork() will actually clone the |
| 33 | running interpreter and all its state, and run the cloned interpreter in |
| 34 | a separate thread, beginning execution in the new thread just after the |
| 35 | point where the fork() was called in the parent. We will refer to the |
| 36 | thread that implements this child "process" as the pseudo-process. |
| 37 | |
| 38 | To the Perl program that called fork(), all this is designed to be |
| 39 | transparent. The parent returns from the fork() with a pseudo-process |
| 40 | ID that can be subsequently used in any process-manipulation functions; |
| 41 | the child returns from the fork() with a value of C<0> to signify that |
| 42 | it is the child pseudo-process. |
| 43 | |
| 44 | =head2 Behavior of other Perl features in forked pseudo-processes |
| 45 | |
| 46 | Most Perl features behave in a natural way within pseudo-processes. |
| 47 | |
| 48 | =over 8 |
| 49 | |
| 50 | =item $$ or $PROCESS_ID |
| 51 | |
| 52 | This special variable is correctly set to the pseudo-process ID. |
| 53 | It can be used to identify pseudo-processes within a particular |
| 54 | session. Note that this value is subject to recycling if any |
| 55 | pseudo-processes are launched after others have been wait()-ed on. |
| 56 | |
| 57 | =item %ENV |
| 58 | |
| 59 | Each pseudo-process maintains its own virtual environment. Modifications |
| 60 | to %ENV affect the virtual environment, and are only visible within that |
| 61 | pseudo-process, and in any processes (or pseudo-processes) launched from |
| 62 | it. |
| 63 | |
| 64 | =item chdir() and all other builtins that accept filenames |
| 65 | |
| 66 | Each pseudo-process maintains its own virtual idea of the current directory. |
| 67 | Modifications to the current directory using chdir() are only visible within |
| 68 | that pseudo-process, and in any processes (or pseudo-processes) launched from |
| 69 | it. All file and directory accesses from the pseudo-process will correctly |
| 70 | map the virtual working directory to the real working directory appropriately. |
| 71 | |
| 72 | =item wait() and waitpid() |
| 73 | |
| 74 | wait() and waitpid() can be passed a pseudo-process ID returned by fork(). |
| 75 | These calls will properly wait for the termination of the pseudo-process |
| 76 | and return its status. |
| 77 | |
| 78 | =item kill() |
| 79 | |
| 80 | C<kill('KILL', ...)> can be used to terminate a pseudo-process by |
| 81 | passing it the ID returned by fork(). The outcome of kill on a pseudo-process |
| 82 | is unpredictable and it should not be used except |
| 83 | under dire circumstances, because the operating system may not |
| 84 | guarantee integrity of the process resources when a running thread is |
| 85 | terminated. The process which implements the pseudo-processes can be blocked |
| 86 | and the Perl interpreter hangs. Note that using C<kill('KILL', ...)> on a |
| 87 | pseudo-process() may typically cause memory leaks, because the thread |
| 88 | that implements the pseudo-process does not get a chance to clean up |
| 89 | its resources. |
| 90 | |
| 91 | C<kill('TERM', ...)> can also be used on pseudo-processes, but the |
| 92 | signal will not be delivered while the pseudo-process is blocked by a |
| 93 | system call, e.g. waiting for a socket to connect, or trying to read |
| 94 | from a socket with no data available. Starting in Perl 5.14 the |
| 95 | parent process will not wait for children to exit once they have been |
| 96 | signalled with C<kill('TERM', ...)> to avoid deadlock during process |
| 97 | exit. You will have to explicitly call waitpid() to make sure the |
| 98 | child has time to clean-up itself, but you are then also responsible |
| 99 | that the child is not blocking on I/O either. |
| 100 | |
| 101 | =item exec() |
| 102 | |
| 103 | Calling exec() within a pseudo-process actually spawns the requested |
| 104 | executable in a separate process and waits for it to complete before |
| 105 | exiting with the same exit status as that process. This means that the |
| 106 | process ID reported within the running executable will be different from |
| 107 | what the earlier Perl fork() might have returned. Similarly, any process |
| 108 | manipulation functions applied to the ID returned by fork() will affect the |
| 109 | waiting pseudo-process that called exec(), not the real process it is |
| 110 | waiting for after the exec(). |
| 111 | |
| 112 | When exec() is called inside a pseudo-process then DESTROY methods and |
| 113 | END blocks will still be called after the external process returns. |
| 114 | |
| 115 | =item exit() |
| 116 | |
| 117 | exit() always exits just the executing pseudo-process, after automatically |
| 118 | wait()-ing for any outstanding child pseudo-processes. Note that this means |
| 119 | that the process as a whole will not exit unless all running pseudo-processes |
| 120 | have exited. See below for some limitations with open filehandles. |
| 121 | |
| 122 | =item Open handles to files, directories and network sockets |
| 123 | |
| 124 | All open handles are dup()-ed in pseudo-processes, so that closing |
| 125 | any handles in one process does not affect the others. See below for |
| 126 | some limitations. |
| 127 | |
| 128 | =back |
| 129 | |
| 130 | =head2 Resource limits |
| 131 | |
| 132 | In the eyes of the operating system, pseudo-processes created via the fork() |
| 133 | emulation are simply threads in the same process. This means that any |
| 134 | process-level limits imposed by the operating system apply to all |
| 135 | pseudo-processes taken together. This includes any limits imposed by the |
| 136 | operating system on the number of open file, directory and socket handles, |
| 137 | limits on disk space usage, limits on memory size, limits on CPU utilization |
| 138 | etc. |
| 139 | |
| 140 | =head2 Killing the parent process |
| 141 | |
| 142 | If the parent process is killed (either using Perl's kill() builtin, or |
| 143 | using some external means) all the pseudo-processes are killed as well, |
| 144 | and the whole process exits. |
| 145 | |
| 146 | =head2 Lifetime of the parent process and pseudo-processes |
| 147 | |
| 148 | During the normal course of events, the parent process and every |
| 149 | pseudo-process started by it will wait for their respective pseudo-children |
| 150 | to complete before they exit. This means that the parent and every |
| 151 | pseudo-child created by it that is also a pseudo-parent will only exit |
| 152 | after their pseudo-children have exited. |
| 153 | |
| 154 | Starting with Perl 5.14 a parent will not wait() automatically |
| 155 | for any child that has been signalled with C<kill('TERM', ...)> |
| 156 | to avoid a deadlock in case the child is blocking on I/O and |
| 157 | never receives the signal. |
| 158 | |
| 159 | =head1 CAVEATS AND LIMITATIONS |
| 160 | |
| 161 | =over 8 |
| 162 | |
| 163 | =item BEGIN blocks |
| 164 | |
| 165 | The fork() emulation will not work entirely correctly when called from |
| 166 | within a BEGIN block. The forked copy will run the contents of the |
| 167 | BEGIN block, but will not continue parsing the source stream after the |
| 168 | BEGIN block. For example, consider the following code: |
| 169 | |
| 170 | BEGIN { |
| 171 | fork and exit; # fork child and exit the parent |
| 172 | print "inner\n"; |
| 173 | } |
| 174 | print "outer\n"; |
| 175 | |
| 176 | This will print: |
| 177 | |
| 178 | inner |
| 179 | |
| 180 | rather than the expected: |
| 181 | |
| 182 | inner |
| 183 | outer |
| 184 | |
| 185 | This limitation arises from fundamental technical difficulties in |
| 186 | cloning and restarting the stacks used by the Perl parser in the |
| 187 | middle of a parse. |
| 188 | |
| 189 | =item Open filehandles |
| 190 | |
| 191 | Any filehandles open at the time of the fork() will be dup()-ed. Thus, |
| 192 | the files can be closed independently in the parent and child, but beware |
| 193 | that the dup()-ed handles will still share the same seek pointer. Changing |
| 194 | the seek position in the parent will change it in the child and vice-versa. |
| 195 | One can avoid this by opening files that need distinct seek pointers |
| 196 | separately in the child. |
| 197 | |
| 198 | On some operating systems, notably Solaris and Unixware, calling C<exit()> |
| 199 | from a child process will flush and close open filehandles in the parent, |
| 200 | thereby corrupting the filehandles. On these systems, calling C<_exit()> |
| 201 | is suggested instead. C<_exit()> is available in Perl through the |
| 202 | C<POSIX> module. Please consult your system's manpages for more information |
| 203 | on this. |
| 204 | |
| 205 | =item Open directory handles |
| 206 | |
| 207 | Perl will completely read from all open directory handles until they |
| 208 | reach the end of the stream. It will then seekdir() back to the |
| 209 | original location and all future readdir() requests will be fulfilled |
| 210 | from the cache buffer. That means that neither the directory handle held |
| 211 | by the parent process nor the one held by the child process will see |
| 212 | any changes made to the directory after the fork() call. |
| 213 | |
| 214 | Note that rewinddir() has a similar limitation on Windows and will not |
| 215 | force readdir() to read the directory again either. Only a newly |
| 216 | opened directory handle will reflect changes to the directory. |
| 217 | |
| 218 | =item Forking pipe open() not yet implemented |
| 219 | |
| 220 | The C<open(FOO, "|-")> and C<open(BAR, "-|")> constructs are not yet |
| 221 | implemented. This limitation can be easily worked around in new code |
| 222 | by creating a pipe explicitly. The following example shows how to |
| 223 | write to a forked child: |
| 224 | |
| 225 | # simulate open(FOO, "|-") |
| 226 | sub pipe_to_fork ($) { |
| 227 | my $parent = shift; |
| 228 | pipe my $child, $parent or die; |
| 229 | my $pid = fork(); |
| 230 | die "fork() failed: $!" unless defined $pid; |
| 231 | if ($pid) { |
| 232 | close $child; |
| 233 | } |
| 234 | else { |
| 235 | close $parent; |
| 236 | open(STDIN, "<&=" . fileno($child)) or die; |
| 237 | } |
| 238 | $pid; |
| 239 | } |
| 240 | |
| 241 | if (pipe_to_fork('FOO')) { |
| 242 | # parent |
| 243 | print FOO "pipe_to_fork\n"; |
| 244 | close FOO; |
| 245 | } |
| 246 | else { |
| 247 | # child |
| 248 | while (<STDIN>) { print; } |
| 249 | exit(0); |
| 250 | } |
| 251 | |
| 252 | And this one reads from the child: |
| 253 | |
| 254 | # simulate open(FOO, "-|") |
| 255 | sub pipe_from_fork ($) { |
| 256 | my $parent = shift; |
| 257 | pipe $parent, my $child or die; |
| 258 | my $pid = fork(); |
| 259 | die "fork() failed: $!" unless defined $pid; |
| 260 | if ($pid) { |
| 261 | close $child; |
| 262 | } |
| 263 | else { |
| 264 | close $parent; |
| 265 | open(STDOUT, ">&=" . fileno($child)) or die; |
| 266 | } |
| 267 | $pid; |
| 268 | } |
| 269 | |
| 270 | if (pipe_from_fork('BAR')) { |
| 271 | # parent |
| 272 | while (<BAR>) { print; } |
| 273 | close BAR; |
| 274 | } |
| 275 | else { |
| 276 | # child |
| 277 | print "pipe_from_fork\n"; |
| 278 | exit(0); |
| 279 | } |
| 280 | |
| 281 | Forking pipe open() constructs will be supported in future. |
| 282 | |
| 283 | =item Global state maintained by XSUBs |
| 284 | |
| 285 | External subroutines (XSUBs) that maintain their own global state may |
| 286 | not work correctly. Such XSUBs will either need to maintain locks to |
| 287 | protect simultaneous access to global data from different pseudo-processes, |
| 288 | or maintain all their state on the Perl symbol table, which is copied |
| 289 | naturally when fork() is called. A callback mechanism that provides |
| 290 | extensions an opportunity to clone their state will be provided in the |
| 291 | near future. |
| 292 | |
| 293 | =item Interpreter embedded in larger application |
| 294 | |
| 295 | The fork() emulation may not behave as expected when it is executed in an |
| 296 | application which embeds a Perl interpreter and calls Perl APIs that can |
| 297 | evaluate bits of Perl code. This stems from the fact that the emulation |
| 298 | only has knowledge about the Perl interpreter's own data structures and |
| 299 | knows nothing about the containing application's state. For example, any |
| 300 | state carried on the application's own call stack is out of reach. |
| 301 | |
| 302 | =item Thread-safety of extensions |
| 303 | |
| 304 | Since the fork() emulation runs code in multiple threads, extensions |
| 305 | calling into non-thread-safe libraries may not work reliably when |
| 306 | calling fork(). As Perl's threading support gradually becomes more |
| 307 | widely adopted even on platforms with a native fork(), such extensions |
| 308 | are expected to be fixed for thread-safety. |
| 309 | |
| 310 | =back |
| 311 | |
| 312 | =head1 PORTABILITY CAVEATS |
| 313 | |
| 314 | In portable Perl code, C<kill(9, $child)> must not be used on forked processes. |
| 315 | Killing a forked process is unsafe and has unpredictable results. |
| 316 | See L</kill()>, above. |
| 317 | |
| 318 | =head1 BUGS |
| 319 | |
| 320 | =over 8 |
| 321 | |
| 322 | =item * |
| 323 | |
| 324 | Having pseudo-process IDs be negative integers breaks down for the integer |
| 325 | C<-1> because the wait() and waitpid() functions treat this number as |
| 326 | being special. The tacit assumption in the current implementation is that |
| 327 | the system never allocates a thread ID of C<1> for user threads. A better |
| 328 | representation for pseudo-process IDs will be implemented in future. |
| 329 | |
| 330 | =item * |
| 331 | |
| 332 | In certain cases, the OS-level handles created by the pipe(), socket(), |
| 333 | and accept() operators are apparently not duplicated accurately in |
| 334 | pseudo-processes. This only happens in some situations, but where it |
| 335 | does happen, it may result in deadlocks between the read and write ends |
| 336 | of pipe handles, or inability to send or receive data across socket |
| 337 | handles. |
| 338 | |
| 339 | =item * |
| 340 | |
| 341 | This document may be incomplete in some respects. |
| 342 | |
| 343 | =back |
| 344 | |
| 345 | =head1 AUTHOR |
| 346 | |
| 347 | Support for concurrent interpreters and the fork() emulation was implemented |
| 348 | by ActiveState, with funding from Microsoft Corporation. |
| 349 | |
| 350 | This document is authored and maintained by Gurusamy Sarathy |
| 351 | E<lt>gsar@activestate.comE<gt>. |
| 352 | |
| 353 | =head1 SEE ALSO |
| 354 | |
| 355 | L<perlfunc/"fork">, L<perlipc> |
| 356 | |
| 357 | =cut |