perlipc.html

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

HTML
765 行
字号
          <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Error setting SIGALRM handler: $!\n"</span><span class="operator">;</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_restartable_system_calls">Restartable system calls</a></strong>

<dd>
<p>On systems that supported it, older versions of Perl used the
SA_RESTART flag when installing %SIG handlers.  This meant that
restartable system calls would continue rather than returning when
a signal arrived.  In order to deliver deferred signals promptly,
Perl 5.7.3 and later do <em>not</em> use SA_RESTART.  Consequently, 
restartable system calls can fail (with $! set to <code>EINTR</code>) in places
where they previously would have succeeded.</p>
</dd>
<dd>
<p>Note that the default <a href="../../lib/Pod/perlrun.html#item__3aperlio"><code>:perlio</code></a> layer will retry <a href="../../lib/Pod/perlfunc.html#item_read"><code>read</code></a>, <a href="../../lib/Pod/perlfunc.html#item_write"><code>write</code></a>
and <a href="../../lib/Pod/perlfunc.html#item_close"><code>close</code></a> as described above and that interrupted <a href="../../lib/Pod/perlfunc.html#item_wait"><code>wait</code></a> and 
<a href="../../lib/Pod/perlfunc.html#item_waitpid"><code>waitpid</code></a> calls will always be retried.</p>
</dd>
</li>
<dt><strong><a name="item_signals_as__22faults_22">Signals as &quot;faults&quot;</a></strong>

<dd>
<p>Certain signals e.g. SEGV, ILL, BUS are generated as a result of
virtual memory or other &quot;faults&quot;. These are normally fatal and there
is little a Perl-level handler can do with them. (In particular the
old signal scheme was particularly unsafe in such cases.)  However if
a %SIG handler is set the new scheme simply sets a flag and returns as
described above. This may cause the operating system to try the
offending machine instruction again and - as nothing has changed - it
will generate the signal again. The result of this is a rather odd
&quot;loop&quot;. In future Perl's signal mechanism may be changed to avoid this
- perhaps by simply disallowing %SIG handlers on signals of that
type. Until then the work-round is not to set a %SIG handler on those
signals. (Which signals they are is operating system dependent.)</p>
</dd>
</li>
<dt><strong><a name="item_signals_triggered_by_operating_system_state">Signals triggered by operating system state</a></strong>

<dd>
<p>On some operating systems certain signal handlers are supposed to &quot;do
something&quot; before returning. One example can be CHLD or CLD which
indicates a child process has completed. On some operating systems the
signal handler is expected to <a href="../../lib/Pod/perlfunc.html#item_wait"><code>wait</code></a> for the completed child
process. On such systems the deferred signal scheme will not work for
those signals (it does not do the <a href="../../lib/Pod/perlfunc.html#item_wait"><code>wait</code></a>). Again the failure will
look like a loop as the operating system will re-issue the signal as
there are un-waited-for completed child processes.</p>
</dd>
</li>
</dl>
<p>If you want the old signal behaviour back regardless of possible
memory corruption, set the environment variable <a href="../../lib/Pod/perlrun.html#item_perl_signals"><code>PERL_SIGNALS</code></a> to
<code>&quot;unsafe&quot;</code> (a new feature since Perl 5.8.1).</p>
<p>
</p>
<hr />
<h1><a name="using_open___for_ipc">Using <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> for IPC</a></h1>
<p>Perl's basic <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> statement can also be used for unidirectional
interprocess communication by either appending or prepending a pipe
symbol to the second argument to open().  Here's how to start
something up in a child process you intend to write to:</p>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">SPOOLER</span><span class="operator">,</span> <span class="string">"| cat -v | lpr -h 2&gt;/dev/null"</span><span class="operator">)</span>
                    <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"can't fork: $!"</span><span class="operator">;</span>
    <span class="keyword">local</span> <span class="variable">$SIG</span><span class="operator">{</span><span class="string">PIPE</span><span class="operator">}</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">die</span> <span class="string">"spooler pipe broke"</span> <span class="operator">};</span>
    <span class="keyword">print</span> <span class="variable">SPOOLER</span> <span class="string">"stuff\n"</span><span class="operator">;</span>
    <span class="keyword">close</span> <span class="variable">SPOOLER</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"bad spool: $! $?"</span><span class="operator">;</span>
</pre>
<p>And here's how to start up a child process you intend to read from:</p>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">STATUS</span><span class="operator">,</span> <span class="string">"netstat -an 2&gt;&amp;1 |"</span><span class="operator">)</span>
                    <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"can't fork: $!"</span><span class="operator">;</span>
    <span class="keyword">while</span> <span class="operator">(&lt;</span><span class="variable">STATUS</span><span class="operator">&gt;)</span> <span class="operator">{</span>
        <span class="keyword">next</span> <span class="keyword">if</span> <span class="regex">/^(tcp|udp)/</span><span class="operator">;</span>
        <span class="keyword">print</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="keyword">close</span> <span class="variable">STATUS</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"bad netstat: $! $?"</span><span class="operator">;</span>
</pre>
<p>If one can be sure that a particular program is a Perl script that is
expecting filenames in @ARGV, the clever programmer can write something
like this:</p>
<pre>
    % program f1 &quot;cmd1|&quot; - f2 &quot;cmd2|&quot; f3 &lt; tmpfile</pre>
<p>and irrespective of which shell it's called from, the Perl program will
read from the file <em>f1</em>, the process <em>cmd1</em>, standard input (<em>tmpfile</em>
in this case), the <em>f2</em> file, the <em>cmd2</em> command, and finally the <em>f3</em>
file.  Pretty nifty, eh?</p>
<p>You might notice that you could use backticks for much the
same effect as opening a pipe for reading:</p>
<pre>
    <span class="keyword">print</span> <span class="keyword">grep</span> <span class="operator">{</span> <span class="operator">!</span><span class="regex">/^(tcp|udp)/</span> <span class="operator">}</span> <span class="string">`netstat -an 2&gt;&amp;1`</span><span class="operator">;</span>
    <span class="keyword">die</span> <span class="string">"bad netstat"</span> <span class="keyword">if</span> <span class="variable">$?</span><span class="operator">;</span>
</pre>
<p>While this is true on the surface, it's much more efficient to process the
file one line or record at a time because then you don't have to read the
whole thing into memory at once.  It also gives you finer control of the
whole process, letting you to kill off the child process early if you'd
like.</p>
<p>Be careful to check both the <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> and the <a href="../../lib/Pod/perlfunc.html#item_close"><code>close()</code></a> return values.  If
you're <em>writing</em> to a pipe, you should also trap SIGPIPE.  Otherwise,
think of what happens when you start up a pipe to a command that doesn't
exist: the <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> will in all likelihood succeed (it only reflects the
fork()'s success), but then your output will fail--spectacularly.  Perl
can't know whether the command worked because your command is actually
running in a separate process whose <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec()</code></a> might have failed.  Therefore,
while readers of bogus commands return just a quick end of file, writers
to bogus command will trigger a signal they'd better be prepared to
handle.  Consider:</p>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">FH</span><span class="operator">,</span> <span class="string">"|bogus"</span><span class="operator">)</span>  <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't fork: $!"</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">FH</span> <span class="string">"bang\n"</span>   <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't write: $!"</span><span class="operator">;</span>
    <span class="keyword">close</span> <span class="variable">FH</span>            <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't close: $!"</span><span class="operator">;</span>
</pre>
<p>That won't blow up until the close, and it will blow up with a SIGPIPE.
To catch it, you could use this:</p>
<pre>
    <span class="variable">$SIG</span><span class="operator">{</span><span class="string">PIPE</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">'IGNORE'</span><span class="operator">;</span>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">FH</span><span class="operator">,</span> <span class="string">"|bogus"</span><span class="operator">)</span>  <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't fork: $!"</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">FH</span> <span class="string">"bang\n"</span>   <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't write: $!"</span><span class="operator">;</span>
    <span class="keyword">close</span> <span class="variable">FH</span>            <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't close: status=$?"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="filehandles">Filehandles</a></h2>
<p>Both the main process and any child processes it forks share the same
STDIN, STDOUT, and STDERR filehandles.  If both processes try to access
them at once, strange things can happen.  You may also want to close
or reopen the filehandles for the child.  You can get around this by
opening your pipe with open(), but on some systems this means that the
child process cannot outlive the parent.</p>
<p>
</p>
<h2><a name="background_processes">Background Processes</a></h2>
<p>You can run a command in the background with:</p>
<pre>
    <span class="keyword">system</span><span class="operator">(</span><span class="string">"cmd &amp;"</span><span class="operator">);</span>
</pre>
<p>The command's STDOUT and STDERR (and possibly STDIN, depending on your
shell) will be the same as the parent's.  You won't need to catch
SIGCHLD because of the double-fork taking place (see below for more
details).</p>
<p>
</p>
<h2><a name="complete_dissociation_of_child_from_parent">Complete Dissociation of Child from Parent</a></h2>
<p>In some cases (starting server processes, for instance) you'll want to
completely dissociate the child process from the parent.  This is
often called daemonization.  A well behaved daemon will also <a href="../../lib/Pod/perlfunc.html#item_chdir"><code>chdir()</code></a>
to the root directory (so it doesn't prevent unmounting the filesystem
containing the directory from which it was launched) and redirect its
standard file descriptors from and to <em>/dev/null</em> (so that random
output doesn't wind up on the user's terminal).</p>

⌨️ 快捷键说明

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