xref: /openbsd/gnu/usr.bin/perl/pod/perlfork.pod (revision 52736614)
1c8ca9003Smillert=head1 NAME
2c8ca9003Smillert
355745691Smillertperlfork - Perl's fork() emulation
4c8ca9003Smillert
5c8ca9003Smillert=head1 SYNOPSIS
6c8ca9003Smillert
755745691Smillert    NOTE:  As of the 5.8.0 release, fork() emulation has considerably
855745691Smillert    matured.  However, there are still a few known bugs and differences
955745691Smillert    from real fork() that might affect you.  See the "BUGS" and
1055745691Smillert    "CAVEATS AND LIMITATIONS" sections below.
11c48bdce4Smillert
12c8ca9003SmillertPerl provides a fork() keyword that corresponds to the Unix system call
13c8ca9003Smillertof the same name.  On most Unix-like platforms where the fork() system
14c8ca9003Smillertcall is available, Perl's fork() simply calls it.
15c8ca9003Smillert
16c8ca9003SmillertOn some platforms such as Windows where the fork() system call is not
17c8ca9003Smillertavailable, Perl can be built to emulate fork() at the interpreter level.
18c8ca9003SmillertWhile the emulation is designed to be as compatible as possible with the
19c48bdce4Smillertreal fork() at the level of the Perl program, there are certain
20c8ca9003Smillertimportant differences that stem from the fact that all the pseudo child
21c8ca9003Smillert"processes" created this way live in the same real process as far as the
22c8ca9003Smillertoperating system is concerned.
23c8ca9003Smillert
24c8ca9003SmillertThis document provides a general overview of the capabilities and
25c8ca9003Smillertlimitations of the fork() emulation.  Note that the issues discussed here
26c8ca9003Smillertare not applicable to platforms where a real fork() is available and Perl
27c8ca9003Smillerthas been configured to use it.
28c8ca9003Smillert
29c8ca9003Smillert=head1 DESCRIPTION
30c8ca9003Smillert
31c8ca9003SmillertThe fork() emulation is implemented at the level of the Perl interpreter.
32c8ca9003SmillertWhat this means in general is that running fork() will actually clone the
33c8ca9003Smillertrunning interpreter and all its state, and run the cloned interpreter in
34c8ca9003Smillerta separate thread, beginning execution in the new thread just after the
35c8ca9003Smillertpoint where the fork() was called in the parent.  We will refer to the
36c8ca9003Smillertthread that implements this child "process" as the pseudo-process.
37c8ca9003Smillert
38c8ca9003SmillertTo the Perl program that called fork(), all this is designed to be
39c8ca9003Smillerttransparent.  The parent returns from the fork() with a pseudo-process
40898184e3SsthenID that can be subsequently used in any process-manipulation functions;
41c8ca9003Smillertthe child returns from the fork() with a value of C<0> to signify that
42c8ca9003Smillertit is the child pseudo-process.
43c8ca9003Smillert
44c8ca9003Smillert=head2 Behavior of other Perl features in forked pseudo-processes
45c8ca9003Smillert
46c8ca9003SmillertMost Perl features behave in a natural way within pseudo-processes.
47c8ca9003Smillert
48c8ca9003Smillert=over 8
49c8ca9003Smillert
50c8ca9003Smillert=item $$ or $PROCESS_ID
51c8ca9003Smillert
52c8ca9003SmillertThis special variable is correctly set to the pseudo-process ID.
53c8ca9003SmillertIt can be used to identify pseudo-processes within a particular
54c8ca9003Smillertsession.  Note that this value is subject to recycling if any
55c8ca9003Smillertpseudo-processes are launched after others have been wait()-ed on.
56c8ca9003Smillert
57c8ca9003Smillert=item %ENV
58c8ca9003Smillert
59c48bdce4SmillertEach pseudo-process maintains its own virtual environment.  Modifications
60c8ca9003Smillertto %ENV affect the virtual environment, and are only visible within that
61c8ca9003Smillertpseudo-process, and in any processes (or pseudo-processes) launched from
62c8ca9003Smillertit.
63c8ca9003Smillert
64c8ca9003Smillert=item chdir() and all other builtins that accept filenames
65c8ca9003Smillert
66c8ca9003SmillertEach pseudo-process maintains its own virtual idea of the current directory.
67c8ca9003SmillertModifications to the current directory using chdir() are only visible within
68c8ca9003Smillertthat pseudo-process, and in any processes (or pseudo-processes) launched from
69c8ca9003Smillertit.  All file and directory accesses from the pseudo-process will correctly
70c8ca9003Smillertmap the virtual working directory to the real working directory appropriately.
71c8ca9003Smillert
72c8ca9003Smillert=item wait() and waitpid()
73c8ca9003Smillert
74c8ca9003Smillertwait() and waitpid() can be passed a pseudo-process ID returned by fork().
75c8ca9003SmillertThese calls will properly wait for the termination of the pseudo-process
76c8ca9003Smillertand return its status.
77c8ca9003Smillert
78c8ca9003Smillert=item kill()
79c8ca9003Smillert
80898184e3SsthenC<kill('KILL', ...)> can be used to terminate a pseudo-process by
81898184e3Ssthenpassing it the ID returned by fork(). The outcome of kill on a pseudo-process
82898184e3Ssthenis unpredictable and it should not be used except
83898184e3Ssthenunder dire circumstances, because the operating system may not
84898184e3Ssthenguarantee integrity of the process resources when a running thread is
85898184e3Ssthenterminated.  The process which implements the pseudo-processes can be blocked
86898184e3Ssthenand the Perl interpreter hangs. Note that using C<kill('KILL', ...)> on a
87898184e3Ssthenpseudo-process() may typically cause memory leaks, because the thread
88898184e3Ssthenthat implements the pseudo-process does not get a chance to clean up
89898184e3Ssthenits resources.
90898184e3Ssthen
91898184e3SsthenC<kill('TERM', ...)> can also be used on pseudo-processes, but the
92898184e3Ssthensignal will not be delivered while the pseudo-process is blocked by a
93898184e3Ssthensystem call, e.g. waiting for a socket to connect, or trying to read
94898184e3Ssthenfrom a socket with no data available.  Starting in Perl 5.14 the
95898184e3Ssthenparent process will not wait for children to exit once they have been
96898184e3Ssthensignalled with C<kill('TERM', ...)> to avoid deadlock during process
97898184e3Ssthenexit.  You will have to explicitly call waitpid() to make sure the
98898184e3Ssthenchild has time to clean-up itself, but you are then also responsible
99898184e3Ssthenthat the child is not blocking on I/O either.
100c8ca9003Smillert
101c8ca9003Smillert=item exec()
102c8ca9003Smillert
103c8ca9003SmillertCalling exec() within a pseudo-process actually spawns the requested
104c8ca9003Smillertexecutable in a separate process and waits for it to complete before
105c8ca9003Smillertexiting with the same exit status as that process.  This means that the
106c8ca9003Smillertprocess ID reported within the running executable will be different from
107c8ca9003Smillertwhat the earlier Perl fork() might have returned.  Similarly, any process
108c8ca9003Smillertmanipulation functions applied to the ID returned by fork() will affect the
109c8ca9003Smillertwaiting pseudo-process that called exec(), not the real process it is
110c8ca9003Smillertwaiting for after the exec().
111c8ca9003Smillert
112850e2753SmillertWhen exec() is called inside a pseudo-process then DESTROY methods and
113850e2753SmillertEND blocks will still be called after the external process returns.
114850e2753Smillert
115c8ca9003Smillert=item exit()
116c8ca9003Smillert
117c8ca9003Smillertexit() always exits just the executing pseudo-process, after automatically
118c8ca9003Smillertwait()-ing for any outstanding child pseudo-processes.  Note that this means
119c8ca9003Smillertthat the process as a whole will not exit unless all running pseudo-processes
120850e2753Smillerthave exited.  See below for some limitations with open filehandles.
121c8ca9003Smillert
122c8ca9003Smillert=item Open handles to files, directories and network sockets
123c8ca9003Smillert
124c8ca9003SmillertAll open handles are dup()-ed in pseudo-processes, so that closing
125c8ca9003Smillertany handles in one process does not affect the others.  See below for
126c8ca9003Smillertsome limitations.
127c8ca9003Smillert
128c8ca9003Smillert=back
129c8ca9003Smillert
130c8ca9003Smillert=head2 Resource limits
131c8ca9003Smillert
132c8ca9003SmillertIn the eyes of the operating system, pseudo-processes created via the fork()
133c8ca9003Smillertemulation are simply threads in the same process.  This means that any
134c8ca9003Smillertprocess-level limits imposed by the operating system apply to all
135c8ca9003Smillertpseudo-processes taken together.  This includes any limits imposed by the
136c8ca9003Smillertoperating system on the number of open file, directory and socket handles,
137c8ca9003Smillertlimits on disk space usage, limits on memory size, limits on CPU utilization
138c8ca9003Smillertetc.
139c8ca9003Smillert
140c8ca9003Smillert=head2 Killing the parent process
141c8ca9003Smillert
142c8ca9003SmillertIf the parent process is killed (either using Perl's kill() builtin, or
143c8ca9003Smillertusing some external means) all the pseudo-processes are killed as well,
144c8ca9003Smillertand the whole process exits.
145c8ca9003Smillert
146c8ca9003Smillert=head2 Lifetime of the parent process and pseudo-processes
147c8ca9003Smillert
148c8ca9003SmillertDuring the normal course of events, the parent process and every
149c8ca9003Smillertpseudo-process started by it will wait for their respective pseudo-children
150c8ca9003Smillertto complete before they exit.  This means that the parent and every
151c8ca9003Smillertpseudo-child created by it that is also a pseudo-parent will only exit
152c8ca9003Smillertafter their pseudo-children have exited.
153c8ca9003Smillert
154898184e3SsthenStarting with Perl 5.14 a parent will not wait() automatically
155*52736614Safresh1for any child that has been signalled with C<kill('TERM', ...)>
156898184e3Ssthento avoid a deadlock in case the child is blocking on I/O and
157898184e3Ssthennever receives the signal.
158c8ca9003Smillert
159898184e3Ssthen=head1 CAVEATS AND LIMITATIONS
160c8ca9003Smillert
161c8ca9003Smillert=over 8
162c8ca9003Smillert
163c8ca9003Smillert=item BEGIN blocks
164c8ca9003Smillert
165c8ca9003SmillertThe fork() emulation will not work entirely correctly when called from
166c8ca9003Smillertwithin a BEGIN block.  The forked copy will run the contents of the
167c8ca9003SmillertBEGIN block, but will not continue parsing the source stream after the
168c8ca9003SmillertBEGIN block.  For example, consider the following code:
169c8ca9003Smillert
170c8ca9003Smillert    BEGIN {
171c8ca9003Smillert        fork and exit;          # fork child and exit the parent
172c8ca9003Smillert        print "inner\n";
173c8ca9003Smillert    }
174c8ca9003Smillert    print "outer\n";
175c8ca9003Smillert
176c8ca9003SmillertThis will print:
177c8ca9003Smillert
178c8ca9003Smillert    inner
179c8ca9003Smillert
180c8ca9003Smillertrather than the expected:
181c8ca9003Smillert
182c8ca9003Smillert    inner
183c8ca9003Smillert    outer
184c8ca9003Smillert
185c8ca9003SmillertThis limitation arises from fundamental technical difficulties in
186c8ca9003Smillertcloning and restarting the stacks used by the Perl parser in the
187c8ca9003Smillertmiddle of a parse.
188c8ca9003Smillert
189c8ca9003Smillert=item Open filehandles
190c8ca9003Smillert
191c8ca9003SmillertAny filehandles open at the time of the fork() will be dup()-ed.  Thus,
192c8ca9003Smillertthe files can be closed independently in the parent and child, but beware
193c8ca9003Smillertthat the dup()-ed handles will still share the same seek pointer.  Changing
194c8ca9003Smillertthe seek position in the parent will change it in the child and vice-versa.
195c8ca9003SmillertOne can avoid this by opening files that need distinct seek pointers
196c8ca9003Smillertseparately in the child.
197c8ca9003Smillert
198850e2753SmillertOn some operating systems, notably Solaris and Unixware, calling C<exit()>
199850e2753Smillertfrom a child process will flush and close open filehandles in the parent,
200850e2753Smillertthereby corrupting the filehandles.  On these systems, calling C<_exit()>
201850e2753Smillertis suggested instead.  C<_exit()> is available in Perl through the
202898184e3SsthenC<POSIX> module.  Please consult your system's manpages for more information
203850e2753Smillerton this.
204850e2753Smillert
205898184e3Ssthen=item Open directory handles
206898184e3Ssthen
207898184e3SsthenPerl will completely read from all open directory handles until they
208898184e3Ssthenreach the end of the stream.  It will then seekdir() back to the
209898184e3Ssthenoriginal location and all future readdir() requests will be fulfilled
210898184e3Ssthenfrom the cache buffer.  That means that neither the directory handle held
211898184e3Ssthenby the parent process nor the one held by the child process will see
212898184e3Ssthenany changes made to the directory after the fork() call.
213898184e3Ssthen
214898184e3SsthenNote that rewinddir() has a similar limitation on Windows and will not
215898184e3Ssthenforce readdir() to read the directory again either.  Only a newly
216898184e3Ssthenopened directory handle will reflect changes to the directory.
217898184e3Ssthen
218c8ca9003Smillert=item Forking pipe open() not yet implemented
219c8ca9003Smillert
220c8ca9003SmillertThe C<open(FOO, "|-")> and C<open(BAR, "-|")> constructs are not yet
221c8ca9003Smillertimplemented.  This limitation can be easily worked around in new code
222c8ca9003Smillertby creating a pipe explicitly.  The following example shows how to
223c8ca9003Smillertwrite to a forked child:
224c8ca9003Smillert
225c8ca9003Smillert    # simulate open(FOO, "|-")
226c8ca9003Smillert    sub pipe_to_fork ($) {
227c8ca9003Smillert        my $parent = shift;
228c8ca9003Smillert        pipe my $child, $parent or die;
229c8ca9003Smillert        my $pid = fork();
230c8ca9003Smillert        die "fork() failed: $!" unless defined $pid;
231c8ca9003Smillert        if ($pid) {
232c8ca9003Smillert            close $child;
233c8ca9003Smillert        }
234c8ca9003Smillert        else {
235c8ca9003Smillert            close $parent;
236c8ca9003Smillert            open(STDIN, "<&=" . fileno($child)) or die;
237c8ca9003Smillert        }
238c8ca9003Smillert        $pid;
239c8ca9003Smillert    }
240c8ca9003Smillert
241c8ca9003Smillert    if (pipe_to_fork('FOO')) {
242c8ca9003Smillert        # parent
243c8ca9003Smillert        print FOO "pipe_to_fork\n";
244c8ca9003Smillert        close FOO;
245c8ca9003Smillert    }
246c8ca9003Smillert    else {
247c8ca9003Smillert        # child
248c8ca9003Smillert        while (<STDIN>) { print; }
249c8ca9003Smillert        exit(0);
250c8ca9003Smillert    }
251c8ca9003Smillert
252c8ca9003SmillertAnd this one reads from the child:
253c8ca9003Smillert
254c8ca9003Smillert    # simulate open(FOO, "-|")
255c8ca9003Smillert    sub pipe_from_fork ($) {
256c8ca9003Smillert        my $parent = shift;
257c8ca9003Smillert        pipe $parent, my $child or die;
258c8ca9003Smillert        my $pid = fork();
259c8ca9003Smillert        die "fork() failed: $!" unless defined $pid;
260c8ca9003Smillert        if ($pid) {
261c8ca9003Smillert            close $child;
262c8ca9003Smillert        }
263c8ca9003Smillert        else {
264c8ca9003Smillert            close $parent;
265c8ca9003Smillert            open(STDOUT, ">&=" . fileno($child)) or die;
266c8ca9003Smillert        }
267c8ca9003Smillert        $pid;
268c8ca9003Smillert    }
269c8ca9003Smillert
270c8ca9003Smillert    if (pipe_from_fork('BAR')) {
271c8ca9003Smillert        # parent
272c8ca9003Smillert        while (<BAR>) { print; }
273c8ca9003Smillert        close BAR;
274c8ca9003Smillert    }
275c8ca9003Smillert    else {
276c8ca9003Smillert        # child
277c8ca9003Smillert        print "pipe_from_fork\n";
278c8ca9003Smillert        exit(0);
279c8ca9003Smillert    }
280c8ca9003Smillert
281c8ca9003SmillertForking pipe open() constructs will be supported in future.
282c8ca9003Smillert
283c8ca9003Smillert=item Global state maintained by XSUBs
284c8ca9003Smillert
285c8ca9003SmillertExternal subroutines (XSUBs) that maintain their own global state may
286c8ca9003Smillertnot work correctly.  Such XSUBs will either need to maintain locks to
287c8ca9003Smillertprotect simultaneous access to global data from different pseudo-processes,
288c8ca9003Smillertor maintain all their state on the Perl symbol table, which is copied
289c8ca9003Smillertnaturally when fork() is called.  A callback mechanism that provides
290c8ca9003Smillertextensions an opportunity to clone their state will be provided in the
291c8ca9003Smillertnear future.
292c8ca9003Smillert
293c8ca9003Smillert=item Interpreter embedded in larger application
294c8ca9003Smillert
295c8ca9003SmillertThe fork() emulation may not behave as expected when it is executed in an
296c8ca9003Smillertapplication which embeds a Perl interpreter and calls Perl APIs that can
297c8ca9003Smillertevaluate bits of Perl code.  This stems from the fact that the emulation
298c8ca9003Smillertonly has knowledge about the Perl interpreter's own data structures and
299c8ca9003Smillertknows nothing about the containing application's state.  For example, any
300c8ca9003Smillertstate carried on the application's own call stack is out of reach.
301c8ca9003Smillert
302c8ca9003Smillert=item Thread-safety of extensions
303c8ca9003Smillert
304c8ca9003SmillertSince the fork() emulation runs code in multiple threads, extensions
305c8ca9003Smillertcalling into non-thread-safe libraries may not work reliably when
306c8ca9003Smillertcalling fork().  As Perl's threading support gradually becomes more
307c8ca9003Smillertwidely adopted even on platforms with a native fork(), such extensions
308c8ca9003Smillertare expected to be fixed for thread-safety.
309c8ca9003Smillert
310c8ca9003Smillert=back
311c8ca9003Smillert
312898184e3Ssthen=head1 PORTABILITY CAVEATS
313898184e3Ssthen
314898184e3SsthenIn portable Perl code, C<kill(9, $child)> must not be used on forked processes.
315898184e3SsthenKilling a forked process is unsafe and has unpredictable results.
316898184e3SsthenSee L</kill()>, above.
317898184e3Ssthen
318c8ca9003Smillert=head1 BUGS
319c8ca9003Smillert
320c8ca9003Smillert=over 8
321c8ca9003Smillert
322c8ca9003Smillert=item *
323c8ca9003Smillert
324c8ca9003SmillertHaving pseudo-process IDs be negative integers breaks down for the integer
325c8ca9003SmillertC<-1> because the wait() and waitpid() functions treat this number as
326c8ca9003Smillertbeing special.  The tacit assumption in the current implementation is that
327c8ca9003Smillertthe system never allocates a thread ID of C<1> for user threads.  A better
328c8ca9003Smillertrepresentation for pseudo-process IDs will be implemented in future.
329c8ca9003Smillert
330c8ca9003Smillert=item *
331c8ca9003Smillert
33255745691SmillertIn certain cases, the OS-level handles created by the pipe(), socket(),
33355745691Smillertand accept() operators are apparently not duplicated accurately in
33455745691Smillertpseudo-processes.  This only happens in some situations, but where it
33555745691Smillertdoes happen, it may result in deadlocks between the read and write ends
33655745691Smillertof pipe handles, or inability to send or receive data across socket
33755745691Smillerthandles.
33855745691Smillert
33955745691Smillert=item *
34055745691Smillert
341c8ca9003SmillertThis document may be incomplete in some respects.
342c8ca9003Smillert
343c8ca9003Smillert=back
344c8ca9003Smillert
345c8ca9003Smillert=head1 AUTHOR
346c8ca9003Smillert
347c8ca9003SmillertSupport for concurrent interpreters and the fork() emulation was implemented
348c8ca9003Smillertby ActiveState, with funding from Microsoft Corporation.
349c8ca9003Smillert
350c8ca9003SmillertThis document is authored and maintained by Gurusamy Sarathy
351c8ca9003SmillertE<lt>gsar@activestate.comE<gt>.
352c8ca9003Smillert
353c8ca9003Smillert=head1 SEE ALSO
354c8ca9003Smillert
355c8ca9003SmillertL<perlfunc/"fork">, L<perlipc>
356c8ca9003Smillert
357c8ca9003Smillert=cut
358