📄 perlfork.1
字号:
.IP "\fIexec()\fR" 8.IX Item "exec()"Calling \fIexec()\fR within a pseudo-process actually spawns the requestedexecutable in a separate process and waits for it to complete beforeexiting with the same exit status as that process. This means that theprocess \s-1ID\s0 reported within the running executable will be different fromwhat the earlier Perl \fIfork()\fR might have returned. Similarly, any processmanipulation functions applied to the \s-1ID\s0 returned by \fIfork()\fR will affect thewaiting pseudo-process that called \fIexec()\fR, not the real process it iswaiting for after the \fIexec()\fR..SpWhen \fIexec()\fR is called inside a pseudo-process then \s-1DESTROY\s0 methods and\&\s-1END\s0 blocks will still be called after the external process returns..IP "\fIexit()\fR" 8.IX Item "exit()"\&\fIexit()\fR always exits just the executing pseudo-process, after automatically\&\fIwait()\fR\-ing for any outstanding child pseudo-processes. Note that this meansthat the process as a whole will not exit unless all running pseudo-processeshave exited. See below for some limitations with open filehandles..IP "Open handles to files, directories and network sockets" 8.IX Item "Open handles to files, directories and network sockets"All open handles are \fIdup()\fR\-ed in pseudo-processes, so that closingany handles in one process does not affect the others. See below forsome limitations..Sh "Resource limits".IX Subsection "Resource limits"In the eyes of the operating system, pseudo-processes created via the \fIfork()\fRemulation are simply threads in the same process. This means that anyprocess-level limits imposed by the operating system apply to allpseudo-processes taken together. This includes any limits imposed by theoperating system on the number of open file, directory and socket handles,limits on disk space usage, limits on memory size, limits on \s-1CPU\s0 utilizationetc..Sh "Killing the parent process".IX Subsection "Killing the parent process"If the parent process is killed (either using Perl's \fIkill()\fR builtin, orusing some external means) all the pseudo-processes are killed as well,and the whole process exits..Sh "Lifetime of the parent process and pseudo-processes".IX Subsection "Lifetime of the parent process and pseudo-processes"During the normal course of events, the parent process and everypseudo-process started by it will wait for their respective pseudo-childrento complete before they exit. This means that the parent and everypseudo-child created by it that is also a pseudo-parent will only exitafter their pseudo-children have exited..PPA way to mark a pseudo-processes as running detached from their parent (sothat the parent would not have to \fIwait()\fR for them if it doesn't want to)will be provided in future..Sh "\s-1CAVEATS\s0 \s-1AND\s0 \s-1LIMITATIONS\s0".IX Subsection "CAVEATS AND LIMITATIONS".IP "\s-1BEGIN\s0 blocks" 8.IX Item "BEGIN blocks"The \fIfork()\fR emulation will not work entirely correctly when called fromwithin a \s-1BEGIN\s0 block. The forked copy will run the contents of the\&\s-1BEGIN\s0 block, but will not continue parsing the source stream after the\&\s-1BEGIN\s0 block. For example, consider the following code:.Sp.Vb 5\& BEGIN {\& fork and exit; # fork child and exit the parent\& print "inner\en";\& }\& print "outer\en";.Ve.SpThis will print:.Sp.Vb 1\& inner.Ve.Sprather than the expected:.Sp.Vb 2\& inner\& outer.Ve.SpThis limitation arises from fundamental technical difficulties incloning and restarting the stacks used by the Perl parser in themiddle of a parse..IP "Open filehandles" 8.IX Item "Open filehandles"Any filehandles open at the time of the \fIfork()\fR will be \fIdup()\fR\-ed. Thus,the files can be closed independently in the parent and child, but bewarethat the \fIdup()\fR\-ed handles will still share the same seek pointer. Changingthe seek position in the parent will change it in the child and vice-versa.One can avoid this by opening files that need distinct seek pointersseparately in the child..SpOn some operating systems, notably Solaris and Unixware, calling \f(CW\*(C`exit()\*(C'\fRfrom a child process will flush and close open filehandles in the parent,thereby corrupting the filehandles. On these systems, calling \f(CW\*(C`_exit()\*(C'\fRis suggested instead. \f(CW\*(C`_exit()\*(C'\fR is available in Perl through the \&\f(CW\*(C`POSIX\*(C'\fR module. Please consult your systems manpages for more informationon this..IP "Forking pipe \fIopen()\fR not yet implemented" 8.IX Item "Forking pipe open() not yet implemented"The \f(CW\*(C`open(FOO, "|\-")\*(C'\fR and \f(CW\*(C`open(BAR, "\-|")\*(C'\fR constructs are not yetimplemented. This limitation can be easily worked around in new codeby creating a pipe explicitly. The following example shows how towrite to a forked child:.Sp.Vb 10\& # simulate open(FOO, "|\-")\& sub pipe_to_fork ($) {\& my $parent = shift;\& pipe my $child, $parent or die;\& my $pid = fork();\& die "fork() failed: $!" unless defined $pid;\& if ($pid) {\& close $child;\& }\& else {\& close $parent;\& open(STDIN, "<&=" . fileno($child)) or die;\& }\& $pid;\& }\&\& if (pipe_to_fork(\*(AqFOO\*(Aq)) {\& # parent\& print FOO "pipe_to_fork\en";\& close FOO;\& }\& else {\& # child\& while (<STDIN>) { print; }\& exit(0);\& }.Ve.SpAnd this one reads from the child:.Sp.Vb 10\& # simulate open(FOO, "\-|")\& sub pipe_from_fork ($) {\& my $parent = shift;\& pipe $parent, my $child or die;\& my $pid = fork();\& die "fork() failed: $!" unless defined $pid;\& if ($pid) {\& close $child;\& }\& else {\& close $parent;\& open(STDOUT, ">&=" . fileno($child)) or die;\& }\& $pid;\& }\&\& if (pipe_from_fork(\*(AqBAR\*(Aq)) {\& # parent\& while (<BAR>) { print; }\& close BAR;\& }\& else {\& # child\& print "pipe_from_fork\en";\& exit(0);\& }.Ve.SpForking pipe \fIopen()\fR constructs will be supported in future..IP "Global state maintained by XSUBs" 8.IX Item "Global state maintained by XSUBs"External subroutines (XSUBs) that maintain their own global state maynot work correctly. Such XSUBs will either need to maintain locks toprotect simultaneous access to global data from different pseudo-processes,or maintain all their state on the Perl symbol table, which is copiednaturally when \fIfork()\fR is called. A callback mechanism that providesextensions an opportunity to clone their state will be provided in thenear future..IP "Interpreter embedded in larger application" 8.IX Item "Interpreter embedded in larger application"The \fIfork()\fR emulation may not behave as expected when it is executed in anapplication which embeds a Perl interpreter and calls Perl APIs that canevaluate bits of Perl code. This stems from the fact that the emulationonly has knowledge about the Perl interpreter's own data structures andknows nothing about the containing application's state. For example, anystate carried on the application's own call stack is out of reach..IP "Thread-safety of extensions" 8.IX Item "Thread-safety of extensions"Since the \fIfork()\fR emulation runs code in multiple threads, extensionscalling into non-thread-safe libraries may not work reliably whencalling \fIfork()\fR. As Perl's threading support gradually becomes morewidely adopted even on platforms with a native \fIfork()\fR, such extensionsare expected to be fixed for thread-safety..SH "BUGS".IX Header "BUGS".IP "\(bu" 8Having pseudo-process IDs be negative integers breaks down for the integer\&\f(CW\*(C`\-1\*(C'\fR because the \fIwait()\fR and \fIwaitpid()\fR functions treat this number asbeing special. The tacit assumption in the current implementation is thatthe system never allocates a thread \s-1ID\s0 of \f(CW1\fR for user threads. A betterrepresentation for pseudo-process IDs will be implemented in future..IP "\(bu" 8In certain cases, the OS-level handles created by the \fIpipe()\fR, \fIsocket()\fR,and \fIaccept()\fR operators are apparently not duplicated accurately inpseudo-processes. This only happens in some situations, but where itdoes happen, it may result in deadlocks between the read and write endsof pipe handles, or inability to send or receive data across sockethandles..IP "\(bu" 8This document may be incomplete in some respects..SH "AUTHOR".IX Header "AUTHOR"Support for concurrent interpreters and the \fIfork()\fR emulation was implementedby ActiveState, with funding from Microsoft Corporation..PPThis document is authored and maintained by Gurusamy Sarathy<gsar@activestate.com>..SH "SEE ALSO".IX Header "SEE ALSO"\&\*(L"fork\*(R" in perlfunc, perlipc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -