Debian Codegraph @

upstream/mageia-cauldron/man1/perlfork.1

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