Debian Codegraph @

upstream/debian-unstable/man1/perlfork.1

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