1PERLFORK(1)            Perl Programmers Reference Guide            PERLFORK(1)
2
3
4

NAME

6       perlfork - Perl's fork() emulation
7

SYNOPSIS

9           NOTE:  As of the 5.8.0 release, fork() emulation has considerably
10           matured.  However, there are still a few known bugs and differences
11           from real fork() that might affect you.  See the "BUGS" and
12           "CAVEATS AND LIMITATIONS" sections below.
13
14       Perl provides a fork() keyword that corresponds to the Unix system call
15       of the same name.  On most Unix-like platforms where the fork() system
16       call is available, Perl's fork() simply calls it.
17
18       On some platforms such as Windows where the fork() system call is not
19       available, Perl can be built to emulate fork() at the interpreter
20       level.  While the emulation is designed to be as compatible as possible
21       with the real fork() at the level of the Perl program, there are cer‐
22       tain important differences that stem from the fact that all the pseudo
23       child "processes" created this way live in the same real process as far
24       as the operating system is concerned.
25
26       This document provides a general overview of the capabilities and limi‐
27       tations of the fork() emulation.  Note that the issues discussed here
28       are not applicable to platforms where a real fork() is available and
29       Perl has been configured to use it.
30

DESCRIPTION

32       The fork() emulation is implemented at the level of the Perl inter‐
33       preter.  What this means in general is that running fork() will actu‐
34       ally clone the running interpreter and all its state, and run the
35       cloned interpreter in a separate thread, beginning execution in the new
36       thread just after the point where the fork() was called in the parent.
37       We will refer to the thread that implements this child "process" as the
38       pseudo-process.
39
40       To the Perl program that called fork(), all this is designed to be
41       transparent.  The parent returns from the fork() with a pseudo-process
42       ID that can be subsequently used in any process manipulation functions;
43       the child returns from the fork() with a value of 0 to signify that it
44       is the child pseudo-process.
45
46       Behavior of other Perl features in forked pseudo-processes
47
48       Most Perl features behave in a natural way within pseudo-processes.
49
50       $$ or $PROCESS_ID
51               This special variable is correctly set to the pseudo-process
52               ID.  It can be used to identify pseudo-processes within a par‐
53               ticular session.  Note that this value is subject to recycling
54               if any pseudo-processes are launched after others have been
55               wait()-ed on.
56
57       %ENV    Each pseudo-process maintains its own virtual environment.
58               Modifications to %ENV affect the virtual environment, and are
59               only visible within that pseudo-process, and in any processes
60               (or pseudo-processes) launched from it.
61
62       chdir() and all other builtins that accept filenames
63               Each pseudo-process maintains its own virtual idea of the cur‐
64               rent directory.  Modifications to the current directory using
65               chdir() are only visible within that pseudo-process, and in any
66               processes (or pseudo-processes) launched from it.  All file and
67               directory accesses from the pseudo-process will correctly map
68               the virtual working directory to the real working directory
69               appropriately.
70
71       wait() and waitpid()
72               wait() and waitpid() can be passed a pseudo-process ID returned
73               by fork().  These calls will properly wait for the termination
74               of the pseudo-process and return its status.
75
76       kill()  kill() can be used to terminate a pseudo-process by passing it
77               the ID returned by fork().  This should not be used except
78               under dire circumstances, because the operating system may not
79               guarantee integrity of the process resources when a running
80               thread is terminated.  Note that using kill() on a
81               pseudo-process() may typically cause memory leaks, because the
82               thread that implements the pseudo-process does not get a chance
83               to clean up its resources.
84
85       exec()  Calling exec() within a pseudo-process actually spawns the
86               requested executable in a separate process and waits for it to
87               complete before exiting with the same exit status as that
88               process.  This means that the process ID reported within the
89               running executable will be different from what the earlier Perl
90               fork() might have returned.  Similarly, any process manipula‐
91               tion functions applied to the ID returned by fork() will affect
92               the waiting pseudo-process that called exec(), not the real
93               process it is waiting for after the exec().
94
95       exit()  exit() always exits just the executing pseudo-process, after
96               automatically wait()-ing for any outstanding child pseudo-pro‐
97               cesses.  Note that this means that the process as a whole will
98               not exit unless all running pseudo-processes have exited.
99
100       Open handles to files, directories and network sockets
101               All open handles are dup()-ed in pseudo-processes, so that
102               closing any handles in one process does not affect the others.
103               See below for some limitations.
104
105       Resource limits
106
107       In the eyes of the operating system, pseudo-processes created via the
108       fork() emulation are simply threads in the same process.  This means
109       that any process-level limits imposed by the operating system apply to
110       all pseudo-processes taken together.  This includes any limits imposed
111       by the operating system on the number of open file, directory and
112       socket handles, limits on disk space usage, limits on memory size, lim‐
113       its on CPU utilization etc.
114
115       Killing the parent process
116
117       If the parent process is killed (either using Perl's kill() builtin, or
118       using some external means) all the pseudo-processes are killed as well,
119       and the whole process exits.
120
121       Lifetime of the parent process and pseudo-processes
122
123       During the normal course of events, the parent process and every
124       pseudo-process started by it will wait for their respective pseudo-
125       children to complete before they exit.  This means that the parent and
126       every pseudo-child created by it that is also a pseudo-parent will only
127       exit after their pseudo-children have exited.
128
129       A way to mark a pseudo-processes as running detached from their parent
130       (so that the parent would not have to wait() for them if it doesn't
131       want to) will be provided in future.
132
133       CAVEATS AND LIMITATIONS
134
135       BEGIN blocks
136               The fork() emulation will not work entirely correctly when
137               called from within a BEGIN block.  The forked copy will run the
138               contents of the BEGIN block, but will not continue parsing the
139               source stream after the BEGIN block.  For example, consider the
140               following code:
141
142                   BEGIN {
143                       fork and exit;          # fork child and exit the parent
144                       print "inner\n";
145                   }
146                   print "outer\n";
147
148               This will print:
149
150                   inner
151
152               rather than the expected:
153
154                   inner
155                   outer
156
157               This limitation arises from fundamental technical difficulties
158               in cloning and restarting the stacks used by the Perl parser in
159               the middle of a parse.
160
161       Open filehandles
162               Any filehandles open at the time of the fork() will be
163               dup()-ed.  Thus, the files can be closed independently in the
164               parent and child, but beware that the dup()-ed handles will
165               still share the same seek pointer.  Changing the seek position
166               in the parent will change it in the child and vice-versa.  One
167               can avoid this by opening files that need distinct seek point‐
168               ers separately in the child.
169
170       Forking pipe open() not yet implemented
171               The "open(FOO, "⎪-")" and "open(BAR, "-⎪")" constructs are not
172               yet implemented.  This limitation can be easily worked around
173               in new code by creating a pipe explicitly.  The following exam‐
174               ple shows how to write to a forked child:
175
176                   # simulate open(FOO, "⎪-")
177                   sub pipe_to_fork ($) {
178                       my $parent = shift;
179                       pipe my $child, $parent or die;
180                       my $pid = fork();
181                       die "fork() failed: $!" unless defined $pid;
182                       if ($pid) {
183                           close $child;
184                       }
185                       else {
186                           close $parent;
187                           open(STDIN, "<&=" . fileno($child)) or die;
188                       }
189                       $pid;
190                   }
191
192                   if (pipe_to_fork('FOO')) {
193                       # parent
194                       print FOO "pipe_to_fork\n";
195                       close FOO;
196                   }
197                   else {
198                       # child
199                       while (<STDIN>) { print; }
200                       exit(0);
201                   }
202
203               And this one reads from the child:
204
205                   # simulate open(FOO, "-⎪")
206                   sub pipe_from_fork ($) {
207                       my $parent = shift;
208                       pipe $parent, my $child or die;
209                       my $pid = fork();
210                       die "fork() failed: $!" unless defined $pid;
211                       if ($pid) {
212                           close $child;
213                       }
214                       else {
215                           close $parent;
216                           open(STDOUT, ">&=" . fileno($child)) or die;
217                       }
218                       $pid;
219                   }
220
221                   if (pipe_from_fork('BAR')) {
222                       # parent
223                       while (<BAR>) { print; }
224                       close BAR;
225                   }
226                   else {
227                       # child
228                       print "pipe_from_fork\n";
229                       exit(0);
230                   }
231
232               Forking pipe open() constructs will be supported in future.
233
234       Global state maintained by XSUBs
235               External subroutines (XSUBs) that maintain their own global
236               state may not work correctly.  Such XSUBs will either need to
237               maintain locks to protect simultaneous access to global data
238               from different pseudo-processes, or maintain all their state on
239               the Perl symbol table, which is copied naturally when fork() is
240               called.  A callback mechanism that provides extensions an
241               opportunity to clone their state will be provided in the near
242               future.
243
244       Interpreter embedded in larger application
245               The fork() emulation may not behave as expected when it is exe‐
246               cuted in an application which embeds a Perl interpreter and
247               calls Perl APIs that can evaluate bits of Perl code.  This
248               stems from the fact that the emulation only has knowledge about
249               the Perl interpreter's own data structures and knows nothing
250               about the containing application's state.  For example, any
251               state carried on the application's own call stack is out of
252               reach.
253
254       Thread-safety of extensions
255               Since the fork() emulation runs code in multiple threads,
256               extensions calling into non-thread-safe libraries may not work
257               reliably when calling fork().  As Perl's threading support
258               gradually becomes more widely adopted even on platforms with a
259               native fork(), such extensions are expected to be fixed for
260               thread-safety.
261

BUGS

263       ·       Having pseudo-process IDs be negative integers breaks down for
264               the integer "-1" because the wait() and waitpid() functions
265               treat this number as being special.  The tacit assumption in
266               the current implementation is that the system never allocates a
267               thread ID of 1 for user threads.  A better representation for
268               pseudo-process IDs will be implemented in future.
269
270       ·       In certain cases, the OS-level handles created by the pipe(),
271               socket(), and accept() operators are apparently not duplicated
272               accurately in pseudo-processes.  This only happens in some sit‐
273               uations, but where it does happen, it may result in deadlocks
274               between the read and write ends of pipe handles, or inability
275               to send or receive data across socket handles.
276
277       ·       This document may be incomplete in some respects.
278

AUTHOR

280       Support for concurrent interpreters and the fork() emulation was imple‐
281       mented by ActiveState, with funding from Microsoft Corporation.
282
283       This document is authored and maintained by Gurusamy Sarathy
284       <gsar@activestate.com>.
285

SEE ALSO

287       "fork" in perlfunc, perlipc
288
289
290
291perl v5.8.8                       2006-01-07                       PERLFORK(1)
Impressum