perlipc.html
来自「perl教程」· HTML 代码 · 共 765 行 · 第 1/5 页
HTML
765 行
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> code </span><span class="operator">{</span>
<span class="keyword">print</span> <span class="string">"PID: $$\n"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"ARGV: @ARGV\n"</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$c</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
<span class="keyword">while</span> <span class="operator">(++</span><span class="variable">$c</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">sleep</span> <span class="number">2</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"$c\n"</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
<span class="comment">__END__
</span>
</pre>
<p>
</p>
<hr />
<h1><a name="named_pipes">Named Pipes</a></h1>
<p>A named pipe (often referred to as a FIFO) is an old Unix IPC
mechanism for processes communicating on the same machine. It works
just like a regular, connected anonymous pipes, except that the
processes rendezvous using a filename and don't have to be related.</p>
<p>To create a named pipe, use the <code>POSIX::mkfifo()</code> function.</p>
<pre>
<span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(mkfifo)</span><span class="operator">;</span>
<span class="variable">mkfifo</span><span class="operator">(</span><span class="variable">$path</span><span class="operator">,</span> <span class="number">0700</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"mkfifo $path failed: $!"</span><span class="operator">;</span>
</pre>
<p>You can also use the Unix command <code>mknod(1)</code> or on some
systems, mkfifo(1). These may not be in your normal path.</p>
<pre>
<span class="comment"># system return val is backwards, so && not ||</span>
<span class="comment">#</span>
<span class="variable">$ENV</span><span class="operator">{</span><span class="string">PATH</span><span class="operator">}</span> <span class="operator">.=</span> <span class="string">":/etc:/usr/etc"</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span> <span class="keyword">system</span><span class="operator">(</span><span class="string">'mknod'</span><span class="operator">,</span> <span class="variable">$path</span><span class="operator">,</span> <span class="string">'p'</span><span class="operator">)</span>
<span class="operator">&&</span> <span class="keyword">system</span><span class="operator">(</span><span class="string">'mkfifo'</span><span class="operator">,</span> <span class="variable">$path</span><span class="operator">)</span> <span class="operator">)</span>
<span class="operator">{</span>
<span class="keyword">die</span> <span class="string">"mk{nod,fifo} $path failed"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>A fifo is convenient when you want to connect a process to an unrelated
one. When you open a fifo, the program will block until there's something
on the other end.</p>
<p>For example, let's say you'd like to have your <em>.signature</em> file be a
named pipe that has a Perl program on the other end. Now every time any
program (like a mailer, news reader, finger program, etc.) tries to read
from that file, the reading program will block and your program will
supply the new signature. We'll use the pipe-checking file test <strong>-p</strong>
to find out whether anyone (or anything) has accidentally removed our fifo.</p>
<pre>
<span class="keyword">chdir</span><span class="operator">;</span> <span class="comment"># go home</span>
<span class="variable">$FIFO</span> <span class="operator">=</span> <span class="string">'.signature'</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">while</span> <span class="operator">(</span><span class="number">1</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">unless</span> <span class="operator">(</span><span class="keyword">-p</span> <span class="variable">$FIFO</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">unlink</span> <span class="variable">$FIFO</span><span class="operator">;</span>
<span class="keyword">require</span> <span class="variable">POSIX</span><span class="operator">;</span>
<span class="variable">POSIX::mkfifo</span><span class="operator">(</span><span class="variable">$FIFO</span><span class="operator">,</span> <span class="number">0700</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't mkfifo $FIFO: $!"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="comment"># next line blocks until there's a reader</span>
<span class="keyword">open</span> <span class="operator">(</span><span class="variable">FIFO</span><span class="operator">,</span> <span class="string">"> $FIFO"</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"can't write $FIFO: $!"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">FIFO</span> <span class="string">"John Smith (smith\@host.org)\n"</span><span class="operator">,</span> <span class="string">`fortune -s`</span><span class="operator">;</span>
<span class="keyword">close</span> <span class="variable">FIFO</span><span class="operator">;</span>
<span class="keyword">sleep</span> <span class="number">2</span><span class="operator">;</span> <span class="comment"># to avoid dup signals</span>
<span class="operator">}</span>
</pre>
<p>
</p>
<h2><a name="deferred_signals__safe_signals_">Deferred Signals (Safe Signals)</a></h2>
<p>In Perls before Perl 5.7.3 by installing Perl code to deal with
signals, you were exposing yourself to danger from two things. First,
few system library functions are re-entrant. If the signal interrupts
while Perl is executing one function (like <code>malloc(3)</code> or printf(3)),
and your signal handler then calls the same function again, you could
get unpredictable behavior--often, a core dump. Second, Perl isn't
itself re-entrant at the lowest levels. If the signal interrupts Perl
while Perl is changing its own internal data structures, similarly
unpredictable behaviour may result.</p>
<p>There were two things you could do, knowing this: be paranoid or be
pragmatic. The paranoid approach was to do as little as possible in your
signal handler. Set an existing integer variable that already has a
value, and return. This doesn't help you if you're in a slow system call,
which will just restart. That means you have to <a href="../../lib/Pod/perlfunc.html#item_die"><code>die</code></a> to <code>longjump(3)</code> out
of the handler. Even this is a little cavalier for the true paranoiac,
who avoids <a href="../../lib/Pod/perlfunc.html#item_die"><code>die</code></a> in a handler because the system <em>is</em> out to get you.
The pragmatic approach was to say "I know the risks, but prefer the
convenience", and to do anything you wanted in your signal handler,
and be prepared to clean up core dumps now and again.</p>
<p>In Perl 5.7.3 and later to avoid these problems signals are
"deferred"-- that is when the signal is delivered to the process by
the system (to the C code that implements Perl) a flag is set, and the
handler returns immediately. Then at strategic "safe" points in the
Perl interpreter (e.g. when it is about to execute a new opcode) the
flags are checked and the Perl level handler from %SIG is
executed. The "deferred" scheme allows much more flexibility in the
coding of signal handler as we know Perl interpreter is in a safe
state, and that we are not in a system library function when the
handler is called. However the implementation does differ from
previous Perls in the following ways:</p>
<dl>
<dt><strong><a name="item_long_running_opcodes">Long running opcodes</a></strong>
<dd>
<p>As Perl interpreter only looks at the signal flags when it about to
execute a new opcode if a signal arrives during a long running opcode
(e.g. a regular expression operation on a very large string) then
signal will not be seen until operation completes.</p>
</dd>
</li>
<dt><strong><a name="item_interrupting_io">Interrupting IO</a></strong>
<dd>
<p>When a signal is delivered (e.g. INT control-C) the operating system
breaks into IO operations like <a href="../../lib/Pod/perlfunc.html#item_read"><code>read</code></a> (used to implement Perls
<> operator). On older Perls the handler was called
immediately (and as <a href="../../lib/Pod/perlfunc.html#item_read"><code>read</code></a> is not "unsafe" this worked well). With
the "deferred" scheme the handler is not called immediately, and if
Perl is using system's <code>stdio</code> library that library may re-start the
<a href="../../lib/Pod/perlfunc.html#item_read"><code>read</code></a> without returning to Perl and giving it a chance to call the
%SIG handler. If this happens on your system the solution is to use
<a href="../../lib/Pod/perlrun.html#item__3aperlio"><code>:perlio</code></a> layer to do IO - at least on those handles which you want
to be able to break into with signals. (The <a href="../../lib/Pod/perlrun.html#item__3aperlio"><code>:perlio</code></a> layer checks
the signal flags and calls %SIG handlers before resuming IO operation.)</p>
</dd>
<dd>
<p>Note that the default in Perl 5.7.3 and later is to automatically use
the <a href="../../lib/Pod/perlrun.html#item__3aperlio"><code>:perlio</code></a> layer.</p>
</dd>
<dd>
<p>Note that some networking library functions like <a href="../../lib/Pod/perlfunc.html#item_gethostbyname"><code>gethostbyname()</code></a> are
known to have their own implementations of timeouts which may conflict
with your timeouts. If you are having problems with such functions,
you can try using the POSIX <code>sigaction()</code> function, which bypasses the
Perl safe signals (note that this means subjecting yourself to
possible memory corruption, as described above). Instead of setting
<code>$SIG{ALRM}</code>:</p>
</dd>
<dd>
<pre>
<span class="keyword">local</span> <span class="variable">$SIG</span><span class="operator">{</span><span class="string">ALRM</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">"alarm"</span> <span class="operator">};</span>
</pre>
</dd>
<dd>
<p>try something like the following:</p>
</dd>
<dd>
<pre>
<span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(SIGALRM)</span><span class="operator">;</span>
<span class="variable">POSIX::sigaction</span><span class="operator">(</span><span class="variable">SIGALRM</span><span class="operator">,</span>
<span class="variable">POSIX::SigAction</span><span class="operator">-></span><span class="variable">new</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">"alarm"</span> <span class="operator">}))</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?