1 | =head1 NAME
|
---|
2 |
|
---|
3 | perlfork - Perl's fork() emulation
|
---|
4 |
|
---|
5 | =head1 SYNOPSIS
|
---|
6 |
|
---|
7 | NOTE: As of the 5.8.0 release, fork() emulation has considerably
|
---|
8 | matured. However, there are still a few known bugs and differences
|
---|
9 | from real fork() that might affect you. See the "BUGS" and
|
---|
10 | "CAVEATS AND LIMITATIONS" sections below.
|
---|
11 |
|
---|
12 | Perl provides a fork() keyword that corresponds to the Unix system call
|
---|
13 | of the same name. On most Unix-like platforms where the fork() system
|
---|
14 | call is available, Perl's fork() simply calls it.
|
---|
15 |
|
---|
16 | On some platforms such as Windows where the fork() system call is not
|
---|
17 | available, Perl can be built to emulate fork() at the interpreter level.
|
---|
18 | While the emulation is designed to be as compatible as possible with the
|
---|
19 | real fork() at the level of the Perl program, there are certain
|
---|
20 | important differences that stem from the fact that all the pseudo child
|
---|
21 | "processes" created this way live in the same real process as far as the
|
---|
22 | operating system is concerned.
|
---|
23 |
|
---|
24 | This document provides a general overview of the capabilities and
|
---|
25 | limitations of the fork() emulation. Note that the issues discussed here
|
---|
26 | are not applicable to platforms where a real fork() is available and Perl
|
---|
27 | has been configured to use it.
|
---|
28 |
|
---|
29 | =head1 DESCRIPTION
|
---|
30 |
|
---|
31 | The fork() emulation is implemented at the level of the Perl interpreter.
|
---|
32 | What this means in general is that running fork() will actually clone the
|
---|
33 | running interpreter and all its state, and run the cloned interpreter in
|
---|
34 | a separate thread, beginning execution in the new thread just after the
|
---|
35 | point where the fork() was called in the parent. We will refer to the
|
---|
36 | thread that implements this child "process" as the pseudo-process.
|
---|
37 |
|
---|
38 | To the Perl program that called fork(), all this is designed to be
|
---|
39 | transparent. The parent returns from the fork() with a pseudo-process
|
---|
40 | ID that can be subsequently used in any process manipulation functions;
|
---|
41 | the child returns from the fork() with a value of C<0> to signify that
|
---|
42 | it is the child pseudo-process.
|
---|
43 |
|
---|
44 | =head2 Behavior of other Perl features in forked pseudo-processes
|
---|
45 |
|
---|
46 | Most Perl features behave in a natural way within pseudo-processes.
|
---|
47 |
|
---|
48 | =over 8
|
---|
49 |
|
---|
50 | =item $$ or $PROCESS_ID
|
---|
51 |
|
---|
52 | This special variable is correctly set to the pseudo-process ID.
|
---|
53 | It can be used to identify pseudo-processes within a particular
|
---|
54 | session. Note that this value is subject to recycling if any
|
---|
55 | pseudo-processes are launched after others have been wait()-ed on.
|
---|
56 |
|
---|
57 | =item %ENV
|
---|
58 |
|
---|
59 | Each pseudo-process maintains its own virtual environment. Modifications
|
---|
60 | to %ENV affect the virtual environment, and are only visible within that
|
---|
61 | pseudo-process, and in any processes (or pseudo-processes) launched from
|
---|
62 | it.
|
---|
63 |
|
---|
64 | =item chdir() and all other builtins that accept filenames
|
---|
65 |
|
---|
66 | Each pseudo-process maintains its own virtual idea of the current directory.
|
---|
67 | Modifications to the current directory using chdir() are only visible within
|
---|
68 | that pseudo-process, and in any processes (or pseudo-processes) launched from
|
---|
69 | it. All file and directory accesses from the pseudo-process will correctly
|
---|
70 | map the virtual working directory to the real working directory appropriately.
|
---|
71 |
|
---|
72 | =item wait() and waitpid()
|
---|
73 |
|
---|
74 | wait() and waitpid() can be passed a pseudo-process ID returned by fork().
|
---|
75 | These calls will properly wait for the termination of the pseudo-process
|
---|
76 | and return its status.
|
---|
77 |
|
---|
78 | =item kill()
|
---|
79 |
|
---|
80 | kill() can be used to terminate a pseudo-process by passing it the ID returned
|
---|
81 | by fork(). This should not be used except under dire circumstances, because
|
---|
82 | the operating system may not guarantee integrity of the process resources
|
---|
83 | when a running thread is terminated. Note that using kill() on a
|
---|
84 | pseudo-process() may typically cause memory leaks, because the thread that
|
---|
85 | implements the pseudo-process does not get a chance to clean up its resources.
|
---|
86 |
|
---|
87 | =item exec()
|
---|
88 |
|
---|
89 | Calling exec() within a pseudo-process actually spawns the requested
|
---|
90 | executable in a separate process and waits for it to complete before
|
---|
91 | exiting with the same exit status as that process. This means that the
|
---|
92 | process ID reported within the running executable will be different from
|
---|
93 | what the earlier Perl fork() might have returned. Similarly, any process
|
---|
94 | manipulation functions applied to the ID returned by fork() will affect the
|
---|
95 | waiting pseudo-process that called exec(), not the real process it is
|
---|
96 | waiting for after the exec().
|
---|
97 |
|
---|
98 | =item exit()
|
---|
99 |
|
---|
100 | exit() always exits just the executing pseudo-process, after automatically
|
---|
101 | wait()-ing for any outstanding child pseudo-processes. Note that this means
|
---|
102 | that the process as a whole will not exit unless all running pseudo-processes
|
---|
103 | have exited.
|
---|
104 |
|
---|
105 | =item Open handles to files, directories and network sockets
|
---|
106 |
|
---|
107 | All open handles are dup()-ed in pseudo-processes, so that closing
|
---|
108 | any handles in one process does not affect the others. See below for
|
---|
109 | some limitations.
|
---|
110 |
|
---|
111 | =back
|
---|
112 |
|
---|
113 | =head2 Resource limits
|
---|
114 |
|
---|
115 | In the eyes of the operating system, pseudo-processes created via the fork()
|
---|
116 | emulation are simply threads in the same process. This means that any
|
---|
117 | process-level limits imposed by the operating system apply to all
|
---|
118 | pseudo-processes taken together. This includes any limits imposed by the
|
---|
119 | operating system on the number of open file, directory and socket handles,
|
---|
120 | limits on disk space usage, limits on memory size, limits on CPU utilization
|
---|
121 | etc.
|
---|
122 |
|
---|
123 | =head2 Killing the parent process
|
---|
124 |
|
---|
125 | If the parent process is killed (either using Perl's kill() builtin, or
|
---|
126 | using some external means) all the pseudo-processes are killed as well,
|
---|
127 | and the whole process exits.
|
---|
128 |
|
---|
129 | =head2 Lifetime of the parent process and pseudo-processes
|
---|
130 |
|
---|
131 | During the normal course of events, the parent process and every
|
---|
132 | pseudo-process started by it will wait for their respective pseudo-children
|
---|
133 | to complete before they exit. This means that the parent and every
|
---|
134 | pseudo-child created by it that is also a pseudo-parent will only exit
|
---|
135 | after their pseudo-children have exited.
|
---|
136 |
|
---|
137 | A way to mark a pseudo-processes as running detached from their parent (so
|
---|
138 | that the parent would not have to wait() for them if it doesn't want to)
|
---|
139 | will be provided in future.
|
---|
140 |
|
---|
141 | =head2 CAVEATS AND LIMITATIONS
|
---|
142 |
|
---|
143 | =over 8
|
---|
144 |
|
---|
145 | =item BEGIN blocks
|
---|
146 |
|
---|
147 | The fork() emulation will not work entirely correctly when called from
|
---|
148 | within a BEGIN block. The forked copy will run the contents of the
|
---|
149 | BEGIN block, but will not continue parsing the source stream after the
|
---|
150 | BEGIN block. For example, consider the following code:
|
---|
151 |
|
---|
152 | BEGIN {
|
---|
153 | fork and exit; # fork child and exit the parent
|
---|
154 | print "inner\n";
|
---|
155 | }
|
---|
156 | print "outer\n";
|
---|
157 |
|
---|
158 | This will print:
|
---|
159 |
|
---|
160 | inner
|
---|
161 |
|
---|
162 | rather than the expected:
|
---|
163 |
|
---|
164 | inner
|
---|
165 | outer
|
---|
166 |
|
---|
167 | This limitation arises from fundamental technical difficulties in
|
---|
168 | cloning and restarting the stacks used by the Perl parser in the
|
---|
169 | middle of a parse.
|
---|
170 |
|
---|
171 | =item Open filehandles
|
---|
172 |
|
---|
173 | Any filehandles open at the time of the fork() will be dup()-ed. Thus,
|
---|
174 | the files can be closed independently in the parent and child, but beware
|
---|
175 | that the dup()-ed handles will still share the same seek pointer. Changing
|
---|
176 | the seek position in the parent will change it in the child and vice-versa.
|
---|
177 | One can avoid this by opening files that need distinct seek pointers
|
---|
178 | separately in the child.
|
---|
179 |
|
---|
180 | =item Forking pipe open() not yet implemented
|
---|
181 |
|
---|
182 | The C<open(FOO, "|-")> and C<open(BAR, "-|")> constructs are not yet
|
---|
183 | implemented. This limitation can be easily worked around in new code
|
---|
184 | by creating a pipe explicitly. The following example shows how to
|
---|
185 | write to a forked child:
|
---|
186 |
|
---|
187 | # simulate open(FOO, "|-")
|
---|
188 | sub pipe_to_fork ($) {
|
---|
189 | my $parent = shift;
|
---|
190 | pipe my $child, $parent or die;
|
---|
191 | my $pid = fork();
|
---|
192 | die "fork() failed: $!" unless defined $pid;
|
---|
193 | if ($pid) {
|
---|
194 | close $child;
|
---|
195 | }
|
---|
196 | else {
|
---|
197 | close $parent;
|
---|
198 | open(STDIN, "<&=" . fileno($child)) or die;
|
---|
199 | }
|
---|
200 | $pid;
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (pipe_to_fork('FOO')) {
|
---|
204 | # parent
|
---|
205 | print FOO "pipe_to_fork\n";
|
---|
206 | close FOO;
|
---|
207 | }
|
---|
208 | else {
|
---|
209 | # child
|
---|
210 | while (<STDIN>) { print; }
|
---|
211 | exit(0);
|
---|
212 | }
|
---|
213 |
|
---|
214 | And this one reads from the child:
|
---|
215 |
|
---|
216 | # simulate open(FOO, "-|")
|
---|
217 | sub pipe_from_fork ($) {
|
---|
218 | my $parent = shift;
|
---|
219 | pipe $parent, my $child or die;
|
---|
220 | my $pid = fork();
|
---|
221 | die "fork() failed: $!" unless defined $pid;
|
---|
222 | if ($pid) {
|
---|
223 | close $child;
|
---|
224 | }
|
---|
225 | else {
|
---|
226 | close $parent;
|
---|
227 | open(STDOUT, ">&=" . fileno($child)) or die;
|
---|
228 | }
|
---|
229 | $pid;
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (pipe_from_fork('BAR')) {
|
---|
233 | # parent
|
---|
234 | while (<BAR>) { print; }
|
---|
235 | close BAR;
|
---|
236 | }
|
---|
237 | else {
|
---|
238 | # child
|
---|
239 | print "pipe_from_fork\n";
|
---|
240 | exit(0);
|
---|
241 | }
|
---|
242 |
|
---|
243 | Forking pipe open() constructs will be supported in future.
|
---|
244 |
|
---|
245 | =item Global state maintained by XSUBs
|
---|
246 |
|
---|
247 | External subroutines (XSUBs) that maintain their own global state may
|
---|
248 | not work correctly. Such XSUBs will either need to maintain locks to
|
---|
249 | protect simultaneous access to global data from different pseudo-processes,
|
---|
250 | or maintain all their state on the Perl symbol table, which is copied
|
---|
251 | naturally when fork() is called. A callback mechanism that provides
|
---|
252 | extensions an opportunity to clone their state will be provided in the
|
---|
253 | near future.
|
---|
254 |
|
---|
255 | =item Interpreter embedded in larger application
|
---|
256 |
|
---|
257 | The fork() emulation may not behave as expected when it is executed in an
|
---|
258 | application which embeds a Perl interpreter and calls Perl APIs that can
|
---|
259 | evaluate bits of Perl code. This stems from the fact that the emulation
|
---|
260 | only has knowledge about the Perl interpreter's own data structures and
|
---|
261 | knows nothing about the containing application's state. For example, any
|
---|
262 | state carried on the application's own call stack is out of reach.
|
---|
263 |
|
---|
264 | =item Thread-safety of extensions
|
---|
265 |
|
---|
266 | Since the fork() emulation runs code in multiple threads, extensions
|
---|
267 | calling into non-thread-safe libraries may not work reliably when
|
---|
268 | calling fork(). As Perl's threading support gradually becomes more
|
---|
269 | widely adopted even on platforms with a native fork(), such extensions
|
---|
270 | are expected to be fixed for thread-safety.
|
---|
271 |
|
---|
272 | =back
|
---|
273 |
|
---|
274 | =head1 BUGS
|
---|
275 |
|
---|
276 | =over 8
|
---|
277 |
|
---|
278 | =item *
|
---|
279 |
|
---|
280 | Having pseudo-process IDs be negative integers breaks down for the integer
|
---|
281 | C<-1> because the wait() and waitpid() functions treat this number as
|
---|
282 | being special. The tacit assumption in the current implementation is that
|
---|
283 | the system never allocates a thread ID of C<1> for user threads. A better
|
---|
284 | representation for pseudo-process IDs will be implemented in future.
|
---|
285 |
|
---|
286 | =item *
|
---|
287 |
|
---|
288 | In certain cases, the OS-level handles created by the pipe(), socket(),
|
---|
289 | and accept() operators are apparently not duplicated accurately in
|
---|
290 | pseudo-processes. This only happens in some situations, but where it
|
---|
291 | does happen, it may result in deadlocks between the read and write ends
|
---|
292 | of pipe handles, or inability to send or receive data across socket
|
---|
293 | handles.
|
---|
294 |
|
---|
295 | =item *
|
---|
296 |
|
---|
297 | This document may be incomplete in some respects.
|
---|
298 |
|
---|
299 | =back
|
---|
300 |
|
---|
301 | =head1 AUTHOR
|
---|
302 |
|
---|
303 | Support for concurrent interpreters and the fork() emulation was implemented
|
---|
304 | by ActiveState, with funding from Microsoft Corporation.
|
---|
305 |
|
---|
306 | This document is authored and maintained by Gurusamy Sarathy
|
---|
307 | E<lt>[email protected]<gt>.
|
---|
308 |
|
---|
309 | =head1 SEE ALSO
|
---|
310 |
|
---|
311 | L<perlfunc/"fork">, L<perlipc>
|
---|
312 |
|
---|
313 | =cut
|
---|