perlfork.html

来自「perl教程」· HTML 代码 · 共 406 行 · 第 1/2 页

HTML
406
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlfork - Perl's fork emulation</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlfork - Perl's fork emulation</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#behavior_of_other_perl_features_in_forked_pseudoprocesses">Behavior of other Perl features in forked pseudo-processes</a></li>
		<li><a href="#resource_limits">Resource limits</a></li>
		<li><a href="#killing_the_parent_process">Killing the parent process</a></li>
		<li><a href="#lifetime_of_the_parent_process_and_pseudoprocesses">Lifetime of the parent process and pseudo-processes</a></li>
		<li><a href="#caveats_and_limitations">CAVEATS AND LIMITATIONS</a></li>
	</ul>

	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlfork - Perl's <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> emulation</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    NOTE:  As of the 5.8.0 release, fork() emulation has considerably
    matured.  However, there are still a few known bugs and differences
    from real fork() that might affect you.  See the &quot;BUGS&quot; and
    &quot;CAVEATS AND LIMITATIONS&quot; sections below.</pre>
<p>Perl provides a <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> keyword that corresponds to the Unix system call
of the same name.  On most Unix-like platforms where the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> system
call is available, Perl's <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> simply calls it.</p>
<p>On some platforms such as Windows where the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> system call is not
available, Perl can be built to emulate <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> at the interpreter level.
While the emulation is designed to be as compatible as possible with the
real <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> at the level of the Perl program, there are certain
important differences that stem from the fact that all the pseudo child
&quot;processes&quot; created this way live in the same real process as far as the
operating system is concerned.</p>
<p>This document provides a general overview of the capabilities and
limitations of the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> emulation.  Note that the issues discussed here
are not applicable to platforms where a real <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> is available and Perl
has been configured to use it.</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> emulation is implemented at the level of the Perl interpreter.
What this means in general is that running <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> will actually clone the
running interpreter and all its state, and run the cloned interpreter in
a separate thread, beginning execution in the new thread just after the
point where the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> was called in the parent.  We will refer to the
thread that implements this child &quot;process&quot; as the pseudo-process.</p>
<p>To the Perl program that called fork(), all this is designed to be
transparent.  The parent returns from the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> with a pseudo-process
ID that can be subsequently used in any process manipulation functions;
the child returns from the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> with a value of <code>0</code> to signify that
it is the child pseudo-process.</p>
<p>
</p>
<h2><a name="behavior_of_other_perl_features_in_forked_pseudoprocesses">Behavior of other Perl features in forked pseudo-processes</a></h2>
<p>Most Perl features behave in a natural way within pseudo-processes.</p>
<dl>
<dt><strong><a name="item___">$$ or $PROCESS_ID</a></strong>

<dd>
<p>This special variable is correctly set to the pseudo-process ID.
It can be used to identify pseudo-processes within a particular
session.  Note that this value is subject to recycling if any
pseudo-processes are launched after others have been wait()-ed on.</p>
</dd>
</li>
<dt><strong><a name="item__env">%ENV</a></strong>

<dd>
<p>Each pseudo-process maintains its own virtual environment.  Modifications
to %ENV affect the virtual environment, and are only visible within that
pseudo-process, and in any processes (or pseudo-processes) launched from
it.</p>
</dd>
</li>
<dt><strong><a name="item_chdir"><code>chdir()</code> and all other builtins that accept filenames</a></strong>

<dd>
<p>Each pseudo-process maintains its own virtual idea of the current directory.
Modifications to the current directory using <a href="#item_chdir"><code>chdir()</code></a> are only visible within
that pseudo-process, and in any processes (or pseudo-processes) launched from
it.  All file and directory accesses from the pseudo-process will correctly
map the virtual working directory to the real working directory appropriately.</p>
</dd>
</li>
<dt><strong><a name="item_wait"><code>wait()</code> and <a href="../../lib/Pod/perlfunc.html#item_waitpid"><code>waitpid()</code></a></a></strong>

<dd>
<p><a href="#item_wait"><code>wait()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_waitpid"><code>waitpid()</code></a> can be passed a pseudo-process ID returned by fork().
These calls will properly wait for the termination of the pseudo-process
and return its status.</p>
</dd>
</li>
<dt><strong><a name="item_kill"><code>kill()</code></a></strong>

<dd>
<p><a href="#item_kill"><code>kill()</code></a> can be used to terminate a pseudo-process by passing it the ID returned
by fork().  This should not be used except under dire circumstances, because
the operating system may not guarantee integrity of the process resources
when a running thread is terminated.  Note that using <a href="#item_kill"><code>kill()</code></a> on a
pseudo-process() may typically cause memory leaks, because the thread that
implements the pseudo-process does not get a chance to clean up its resources.</p>
</dd>
</li>
<dt><strong><a name="item_exec"><code>exec()</code></a></strong>

<dd>
<p>Calling <a href="#item_exec"><code>exec()</code></a> within a pseudo-process actually spawns the requested
executable in a separate process and waits for it to complete before
exiting with the same exit status as that process.  This means that the
process ID reported within the running executable will be different from
what the earlier Perl <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> might have returned.  Similarly, any process
manipulation functions applied to the ID returned by <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> will affect the
waiting pseudo-process that called exec(), not the real process it is
waiting for after the exec().</p>
</dd>
</li>
<dt><strong><a name="item_exit"><code>exit()</code></a></strong>

<dd>
<p><a href="#item_exit"><code>exit()</code></a> always exits just the executing pseudo-process, after automatically
wait()-ing for any outstanding child pseudo-processes.  Note that this means
that the process as a whole will not exit unless all running pseudo-processes
have exited.</p>
</dd>
</li>
<dt><strong><a name="item_open_handles_to_files_2c_directories_and_network_s">Open handles to files, directories and network sockets</a></strong>

<dd>
<p>All open handles are dup()-ed in pseudo-processes, so that closing
any handles in one process does not affect the others.  See below for
some limitations.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="resource_limits">Resource limits</a></h2>
<p>In the eyes of the operating system, pseudo-processes created via the <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a>
emulation are simply threads in the same process.  This means that any
process-level limits imposed by the operating system apply to all
pseudo-processes taken together.  This includes any limits imposed by the
operating system on the number of open file, directory and socket handles,
limits on disk space usage, limits on memory size, limits on CPU utilization
etc.</p>
<p>
</p>
<h2><a name="killing_the_parent_process">Killing the parent process</a></h2>
<p>If the parent process is killed (either using Perl's <a href="#item_kill"><code>kill()</code></a> builtin, or
using some external means) all the pseudo-processes are killed as well,
and the whole process exits.</p>
<p>
</p>
<h2><a name="lifetime_of_the_parent_process_and_pseudoprocesses">Lifetime of the parent process and pseudo-processes</a></h2>
<p>During the normal course of events, the parent process and every
pseudo-process started by it will wait for their respective pseudo-children
to complete before they exit.  This means that the parent and every
pseudo-child created by it that is also a pseudo-parent will only exit
after their pseudo-children have exited.</p>
<p>A way to mark a pseudo-processes as running detached from their parent (so
that the parent would not have to <a href="#item_wait"><code>wait()</code></a> for them if it doesn't want to)
will be provided in future.</p>
<p>
</p>
<h2><a name="caveats_and_limitations">CAVEATS AND LIMITATIONS</a></h2>
<dl>
<dt><strong><a name="item_begin_blocks">BEGIN blocks</a></strong>

<dd>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?