Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PERLFORK 1" | |
132 | .TH PERLFORK 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlfork \- Perl's fork() emulation | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 4 | |
138 | \& NOTE: As of the 5.8.0 release, fork() emulation has considerably | |
139 | \& matured. However, there are still a few known bugs and differences | |
140 | \& from real fork() that might affect you. See the "BUGS" and | |
141 | \& "CAVEATS AND LIMITATIONS" sections below. | |
142 | .Ve | |
143 | .PP | |
144 | Perl provides a \fIfork()\fR keyword that corresponds to the Unix system call | |
145 | of the same name. On most Unix-like platforms where the \fIfork()\fR system | |
146 | call is available, Perl's \fIfork()\fR simply calls it. | |
147 | .PP | |
148 | On some platforms such as Windows where the \fIfork()\fR system call is not | |
149 | available, Perl can be built to emulate \fIfork()\fR at the interpreter level. | |
150 | While the emulation is designed to be as compatible as possible with the | |
151 | real \fIfork()\fR at the level of the Perl program, there are certain | |
152 | important differences that stem from the fact that all the pseudo child | |
153 | \&\*(L"processes\*(R" created this way live in the same real process as far as the | |
154 | operating system is concerned. | |
155 | .PP | |
156 | This document provides a general overview of the capabilities and | |
157 | limitations of the \fIfork()\fR emulation. Note that the issues discussed here | |
158 | are not applicable to platforms where a real \fIfork()\fR is available and Perl | |
159 | has been configured to use it. | |
160 | .SH "DESCRIPTION" | |
161 | .IX Header "DESCRIPTION" | |
162 | The \fIfork()\fR emulation is implemented at the level of the Perl interpreter. | |
163 | What this means in general is that running \fIfork()\fR will actually clone the | |
164 | running interpreter and all its state, and run the cloned interpreter in | |
165 | a separate thread, beginning execution in the new thread just after the | |
166 | point where the \fIfork()\fR was called in the parent. We will refer to the | |
167 | thread that implements this child \*(L"process\*(R" as the pseudo\-process. | |
168 | .PP | |
169 | To the Perl program that called \fIfork()\fR, all this is designed to be | |
170 | transparent. The parent returns from the \fIfork()\fR with a pseudo-process | |
171 | \&\s-1ID\s0 that can be subsequently used in any process manipulation functions; | |
172 | the child returns from the \fIfork()\fR with a value of \f(CW0\fR to signify that | |
173 | it is the child pseudo\-process. | |
174 | .Sh "Behavior of other Perl features in forked pseudo-processes" | |
175 | .IX Subsection "Behavior of other Perl features in forked pseudo-processes" | |
176 | Most Perl features behave in a natural way within pseudo\-processes. | |
177 | .ie n .IP "$$ or $PROCESS_ID" 8 | |
178 | .el .IP "$$ or \f(CW$PROCESS_ID\fR" 8 | |
179 | .IX Item "$$ or $PROCESS_ID" | |
180 | This special variable is correctly set to the pseudo-process \s-1ID\s0. | |
181 | It can be used to identify pseudo-processes within a particular | |
182 | session. Note that this value is subject to recycling if any | |
183 | pseudo-processes are launched after others have been \fIwait()\fR\-ed on. | |
184 | .IP "%ENV" 8 | |
185 | .IX Item "%ENV" | |
186 | Each pseudo-process maintains its own virtual environment. Modifications | |
187 | to \f(CW%ENV\fR affect the virtual environment, and are only visible within that | |
188 | pseudo\-process, and in any processes (or pseudo\-processes) launched from | |
189 | it. | |
190 | .IP "\fIchdir()\fR and all other builtins that accept filenames" 8 | |
191 | .IX Item "chdir() and all other builtins that accept filenames" | |
192 | Each pseudo-process maintains its own virtual idea of the current directory. | |
193 | Modifications to the current directory using \fIchdir()\fR are only visible within | |
194 | that pseudo\-process, and in any processes (or pseudo\-processes) launched from | |
195 | it. All file and directory accesses from the pseudo-process will correctly | |
196 | map the virtual working directory to the real working directory appropriately. | |
197 | .IP "\fIwait()\fR and \fIwaitpid()\fR" 8 | |
198 | .IX Item "wait() and waitpid()" | |
199 | \&\fIwait()\fR and \fIwaitpid()\fR can be passed a pseudo-process \s-1ID\s0 returned by \fIfork()\fR. | |
200 | These calls will properly wait for the termination of the pseudo-process | |
201 | and return its status. | |
202 | .IP "\fIkill()\fR" 8 | |
203 | .IX Item "kill()" | |
204 | \&\fIkill()\fR can be used to terminate a pseudo-process by passing it the \s-1ID\s0 returned | |
205 | by \fIfork()\fR. This should not be used except under dire circumstances, because | |
206 | the operating system may not guarantee integrity of the process resources | |
207 | when a running thread is terminated. Note that using \fIkill()\fR on a | |
208 | pseudo\-\fIprocess()\fR may typically cause memory leaks, because the thread that | |
209 | implements the pseudo-process does not get a chance to clean up its resources. | |
210 | .IP "\fIexec()\fR" 8 | |
211 | .IX Item "exec()" | |
212 | Calling \fIexec()\fR within a pseudo-process actually spawns the requested | |
213 | executable in a separate process and waits for it to complete before | |
214 | exiting with the same exit status as that process. This means that the | |
215 | process \s-1ID\s0 reported within the running executable will be different from | |
216 | what the earlier Perl \fIfork()\fR might have returned. Similarly, any process | |
217 | manipulation functions applied to the \s-1ID\s0 returned by \fIfork()\fR will affect the | |
218 | waiting pseudo-process that called \fIexec()\fR, not the real process it is | |
219 | waiting for after the \fIexec()\fR. | |
220 | .IP "\fIexit()\fR" 8 | |
221 | .IX Item "exit()" | |
222 | \&\fIexit()\fR always exits just the executing pseudo\-process, after automatically | |
223 | \&\fIwait()\fR\-ing for any outstanding child pseudo\-processes. Note that this means | |
224 | that the process as a whole will not exit unless all running pseudo-processes | |
225 | have exited. | |
226 | .IP "Open handles to files, directories and network sockets" 8 | |
227 | .IX Item "Open handles to files, directories and network sockets" | |
228 | All open handles are \fIdup()\fR\-ed in pseudo\-processes, so that closing | |
229 | any handles in one process does not affect the others. See below for | |
230 | some limitations. | |
231 | .Sh "Resource limits" | |
232 | .IX Subsection "Resource limits" | |
233 | In the eyes of the operating system, pseudo-processes created via the \fIfork()\fR | |
234 | emulation are simply threads in the same process. This means that any | |
235 | process-level limits imposed by the operating system apply to all | |
236 | pseudo-processes taken together. This includes any limits imposed by the | |
237 | operating system on the number of open file, directory and socket handles, | |
238 | limits on disk space usage, limits on memory size, limits on \s-1CPU\s0 utilization | |
239 | etc. | |
240 | .Sh "Killing the parent process" | |
241 | .IX Subsection "Killing the parent process" | |
242 | If the parent process is killed (either using Perl's \fIkill()\fR builtin, or | |
243 | using some external means) all the pseudo-processes are killed as well, | |
244 | and the whole process exits. | |
245 | .Sh "Lifetime of the parent process and pseudo-processes" | |
246 | .IX Subsection "Lifetime of the parent process and pseudo-processes" | |
247 | During the normal course of events, the parent process and every | |
248 | pseudo-process started by it will wait for their respective pseudo-children | |
249 | to complete before they exit. This means that the parent and every | |
250 | pseudo-child created by it that is also a pseudo-parent will only exit | |
251 | after their pseudo-children have exited. | |
252 | .PP | |
253 | A way to mark a pseudo-processes as running detached from their parent (so | |
254 | that the parent would not have to \fIwait()\fR for them if it doesn't want to) | |
255 | will be provided in future. | |
256 | .Sh "\s-1CAVEATS\s0 \s-1AND\s0 \s-1LIMITATIONS\s0" | |
257 | .IX Subsection "CAVEATS AND LIMITATIONS" | |
258 | .IP "\s-1BEGIN\s0 blocks" 8 | |
259 | .IX Item "BEGIN blocks" | |
260 | The \fIfork()\fR emulation will not work entirely correctly when called from | |
261 | within a \s-1BEGIN\s0 block. The forked copy will run the contents of the | |
262 | \&\s-1BEGIN\s0 block, but will not continue parsing the source stream after the | |
263 | \&\s-1BEGIN\s0 block. For example, consider the following code: | |
264 | .Sp | |
265 | .Vb 5 | |
266 | \& BEGIN { | |
267 | \& fork and exit; # fork child and exit the parent | |
268 | \& print "inner\en"; | |
269 | \& } | |
270 | \& print "outer\en"; | |
271 | .Ve | |
272 | .Sp | |
273 | This will print: | |
274 | .Sp | |
275 | .Vb 1 | |
276 | \& inner | |
277 | .Ve | |
278 | .Sp | |
279 | rather than the expected: | |
280 | .Sp | |
281 | .Vb 2 | |
282 | \& inner | |
283 | \& outer | |
284 | .Ve | |
285 | .Sp | |
286 | This limitation arises from fundamental technical difficulties in | |
287 | cloning and restarting the stacks used by the Perl parser in the | |
288 | middle of a parse. | |
289 | .IP "Open filehandles" 8 | |
290 | .IX Item "Open filehandles" | |
291 | Any filehandles open at the time of the \fIfork()\fR will be \fIdup()\fR\-ed. Thus, | |
292 | the files can be closed independently in the parent and child, but beware | |
293 | that the \fIdup()\fR\-ed handles will still share the same seek pointer. Changing | |
294 | the seek position in the parent will change it in the child and vice\-versa. | |
295 | One can avoid this by opening files that need distinct seek pointers | |
296 | separately in the child. | |
297 | .IP "Forking pipe \fIopen()\fR not yet implemented" 8 | |
298 | .IX Item "Forking pipe open() not yet implemented" | |
299 | The \f(CW\*(C`open(FOO, "|\-")\*(C'\fR and \f(CW\*(C`open(BAR, "\-|")\*(C'\fR constructs are not yet | |
300 | implemented. This limitation can be easily worked around in new code | |
301 | by creating a pipe explicitly. The following example shows how to | |
302 | write to a forked child: | |
303 | .Sp | |
304 | .Vb 15 | |
305 | \& # simulate open(FOO, "|-") | |
306 | \& sub pipe_to_fork ($) { | |
307 | \& my $parent = shift; | |
308 | \& pipe my $child, $parent or die; | |
309 | \& my $pid = fork(); | |
310 | \& die "fork() failed: $!" unless defined $pid; | |
311 | \& if ($pid) { | |
312 | \& close $child; | |
313 | \& } | |
314 | \& else { | |
315 | \& close $parent; | |
316 | \& open(STDIN, "<&=" . fileno($child)) or die; | |
317 | \& } | |
318 | \& $pid; | |
319 | \& } | |
320 | .Ve | |
321 | .Sp | |
322 | .Vb 10 | |
323 | \& if (pipe_to_fork('FOO')) { | |
324 | \& # parent | |
325 | \& print FOO "pipe_to_fork\en"; | |
326 | \& close FOO; | |
327 | \& } | |
328 | \& else { | |
329 | \& # child | |
330 | \& while (<STDIN>) { print; } | |
331 | \& exit(0); | |
332 | \& } | |
333 | .Ve | |
334 | .Sp | |
335 | And this one reads from the child: | |
336 | .Sp | |
337 | .Vb 15 | |
338 | \& # simulate open(FOO, "-|") | |
339 | \& sub pipe_from_fork ($) { | |
340 | \& my $parent = shift; | |
341 | \& pipe $parent, my $child or die; | |
342 | \& my $pid = fork(); | |
343 | \& die "fork() failed: $!" unless defined $pid; | |
344 | \& if ($pid) { | |
345 | \& close $child; | |
346 | \& } | |
347 | \& else { | |
348 | \& close $parent; | |
349 | \& open(STDOUT, ">&=" . fileno($child)) or die; | |
350 | \& } | |
351 | \& $pid; | |
352 | \& } | |
353 | .Ve | |
354 | .Sp | |
355 | .Vb 10 | |
356 | \& if (pipe_from_fork('BAR')) { | |
357 | \& # parent | |
358 | \& while (<BAR>) { print; } | |
359 | \& close BAR; | |
360 | \& } | |
361 | \& else { | |
362 | \& # child | |
363 | \& print "pipe_from_fork\en"; | |
364 | \& exit(0); | |
365 | \& } | |
366 | .Ve | |
367 | .Sp | |
368 | Forking pipe \fIopen()\fR constructs will be supported in future. | |
369 | .IP "Global state maintained by XSUBs" 8 | |
370 | .IX Item "Global state maintained by XSUBs" | |
371 | External subroutines (XSUBs) that maintain their own global state may | |
372 | not work correctly. Such XSUBs will either need to maintain locks to | |
373 | protect simultaneous access to global data from different pseudo\-processes, | |
374 | or maintain all their state on the Perl symbol table, which is copied | |
375 | naturally when \fIfork()\fR is called. A callback mechanism that provides | |
376 | extensions an opportunity to clone their state will be provided in the | |
377 | near future. | |
378 | .IP "Interpreter embedded in larger application" 8 | |
379 | .IX Item "Interpreter embedded in larger application" | |
380 | The \fIfork()\fR emulation may not behave as expected when it is executed in an | |
381 | application which embeds a Perl interpreter and calls Perl APIs that can | |
382 | evaluate bits of Perl code. This stems from the fact that the emulation | |
383 | only has knowledge about the Perl interpreter's own data structures and | |
384 | knows nothing about the containing application's state. For example, any | |
385 | state carried on the application's own call stack is out of reach. | |
386 | .IP "Thread-safety of extensions" 8 | |
387 | .IX Item "Thread-safety of extensions" | |
388 | Since the \fIfork()\fR emulation runs code in multiple threads, extensions | |
389 | calling into non-thread-safe libraries may not work reliably when | |
390 | calling \fIfork()\fR. As Perl's threading support gradually becomes more | |
391 | widely adopted even on platforms with a native \fIfork()\fR, such extensions | |
392 | are expected to be fixed for thread\-safety. | |
393 | .SH "BUGS" | |
394 | .IX Header "BUGS" | |
395 | .IP "\(bu" 8 | |
396 | Having pseudo-process IDs be negative integers breaks down for the integer | |
397 | \&\f(CW\*(C`\-1\*(C'\fR because the \fIwait()\fR and \fIwaitpid()\fR functions treat this number as | |
398 | being special. The tacit assumption in the current implementation is that | |
399 | the system never allocates a thread \s-1ID\s0 of \f(CW1\fR for user threads. A better | |
400 | representation for pseudo-process IDs will be implemented in future. | |
401 | .IP "\(bu" 8 | |
402 | In certain cases, the OS-level handles created by the \fIpipe()\fR, \fIsocket()\fR, | |
403 | and \fIaccept()\fR operators are apparently not duplicated accurately in | |
404 | pseudo\-processes. This only happens in some situations, but where it | |
405 | does happen, it may result in deadlocks between the read and write ends | |
406 | of pipe handles, or inability to send or receive data across socket | |
407 | handles. | |
408 | .IP "\(bu" 8 | |
409 | This document may be incomplete in some respects. | |
410 | .SH "AUTHOR" | |
411 | .IX Header "AUTHOR" | |
412 | Support for concurrent interpreters and the \fIfork()\fR emulation was implemented | |
413 | by ActiveState, with funding from Microsoft Corporation. | |
414 | .PP | |
415 | This document is authored and maintained by Gurusamy Sarathy | |
416 | <gsar@activestate.com>. | |
417 | .SH "SEE ALSO" | |
418 | .IX Header "SEE ALSO" | |
419 | \&\*(L"fork\*(R" in perlfunc, perlipc |