⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch16_20.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 16.19. Avoiding Zombie Processes (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:12Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"><LINKREL="prev"HREF="ch16_19.htm"TITLE="16.18. Catching Ctrl-C"><LINKREL="next"HREF="ch16_21.htm"TITLE="16.20. Blocking Signals"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_19.htm"TITLE="16.18. Catching Ctrl-C"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.18. Catching Ctrl-C"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_21.htm"TITLE="16.20. Blocking Signals"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.20. Blocking Signals"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-15019">16.19. Avoiding Zombie Processes</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005696">Problem<ACLASS="indexterm"NAME="ch16-idx-1000006421-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006421-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006421-2"></A></A></H3><PCLASS="para">Your program forks children, but the dead children accumulate, fill up your process table, and aggravate your system administrator.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005699">Solution</A></H3><PCLASS="para">If you don't need to record the children that have terminated, use:</P><PRECLASS="programlisting">$SIG{CHLD} = 'IGNORE';</PRE><PCLASS="para">To keep better track of deceased children, install a <ACLASS="indexterm"NAME="ch16-idx-1000006430-0"></A>SIGCHLD handler to call <CODECLASS="literal">waitpid</CODE>:</P><PRECLASS="programlisting">use POSIX &quot;:sys_wait_h&quot;;$SIG{CHLD} = \&amp;REAPER;sub REAPER {    my $stiff;    while (($stiff = waitpid(-1, &amp;WNOHANG)) &gt; 0) {        # do something with $stiff if you want    }    $SIG{CHLD} = \&amp;REAPER;                  # install *after* calling waitpid}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005714">Discussion</A></H3><PCLASS="para">When a process exits, the system keeps it in the process table so the parent can check its status&nbsp;- whether it terminated normally or abnormally. Fetching a child's status (thereby freeing it to drop from the system altogether) is rather grimly called <ICLASS="firstterm">reaping</I> dead children. (This entire recipe is full of ways to harvest your dead children. If this makes you queasy, we understand.) It involves a call to <CODECLASS="literal">wait</CODE> or <CODECLASS="literal">waitpid </CODE>. Some Perl functions (piped <CODECLASS="literal">open</CODE>s, <CODECLASS="literal">system </CODE>, and backticks) will automatically reap the children they make, but you must explicitly wait when you use <CODECLASS="literal">fork</CODE> to manually start another process.</P><PCLASS="para">To avoid accumulating dead children, simply tell the system that you're not interested in them by setting <CODECLASS="literal">$SIG{CHLD}</CODE> to <CODECLASS="literal">&quot;IGNORE&quot;</CODE>. If you want to know which children die and when, you'll need to use <CODECLASS="literal">waitpid</CODE>.</P><PCLASS="para">The <CODECLASS="literal">waitpid</CODE><ACLASS="indexterm"NAME="ch16-idx-1000006431-0"></A> function reaps a single process. Its first argument is the process to wait for&nbsp;- use <CODECLASS="literal">-1</CODE> to mean any process&nbsp;- and its second argument is a set of flags. We use the WNOHANG flag to make <CODECLASS="literal">waitpid</CODE> immediately return <CODECLASS="literal">0</CODE> if there are no dead children. A flag value of <CODECLASS="literal">0</CODE> is supported everywhere, indicating a blocking wait. Call <CODECLASS="literal">waitpid</CODE> from a SIGCHLD handler, as we do in the Solution, to reap the children as soon as they die.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006432-0"></A>The <CODECLASS="literal">wait</CODE> function also reaps children, but it does not have a non-blocking option. If you inadvertently call it when there are running child processes but none have exited, your program will pause until there is a dead child.</P><PCLASS="para">Because the kernel keeps track of undelivered signals using a bit vector, one bit per signal, if two children die before your process is scheduled, you will get only a single SIGCHLD. You must always loop when you reap in a SIGCHLD handler, and so you can't use <CODECLASS="literal">wait</CODE>.</P><PCLASS="para">Both <CODECLASS="literal">wait</CODE> and <CODECLASS="literal">waitpid</CODE> return the process ID that they just reaped and set <CODECLASS="literal">$?</CODE> to the wait status of the defunct process. This status is actually two 8-bit values in one 16-bit number. The high byte is the exit value of the process. The low 7 bits represent the number of the signal that killed the process, with the 8th bit indicating whether a core dump occurred. Here's one way to isolate those values:</P><PRECLASS="programlisting">$exit_value  = $? &gt;&gt; 8;$signal_num  = $? &amp; 127;$dumped_core = $? &amp; 128;</PRE><PCLASS="para">The standard POSIX module has macros to interrogate status values: WIFEXITED, WEXITSTATUS, WIFSIGNALLED, and WTERMSIG. Oddly enough, POSIX doesn't have a macro to test whether the process core dumped.</P><PCLASS="para">Beware of two things when using SIGCHLD. First, the system doesn't just send a SIGCHLD when a child exits; it also sends one when the child stops. A process can stop for many reasons&nbsp;- waiting to be foregrounded so it can do terminal I/O, being sent a SIGSTOP (it will wait for the SIGCONT before continuing), or being suspended from its terminal. You need to check the status with the <CODECLASS="literal">WIFEXITED</CODE>[<ACLASS="footnote"HREF="#ch16-pgfId-1000005731">1</A>] function from the POSIX module to make sure you're dealing with a process that really died, and isn't just suspended.</P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch16-pgfId-1000005731">[1]</A> Not <CODECLASS="literal">SPOUSEXITED</CODE>, even on a PC.</P></DIV></BLOCKQUOTE><PRECLASS="programlisting">use POSIX qw(:signal_h :errno_h :sys_wait_h);$SIG{CHLD} = \&amp;REAPER;sub REAPER {    my $pid;    $pid = waitpid(-1, &amp;WNOHANG);    if ($pid == -1) {        # no child waiting.  Ignore it.    } elsif (WIFEXITED($?)) {        print &quot;Process $pid exited.\n&quot;;    } else {        print &quot;False alarm on $pid.\n&quot;;    }    $SIG{CHLD} = \&amp;REAPER;          # in case of unreliable signals}</PRE><PCLASS="para">The second trap with SIGCHLD is related to Perl, not the operating system. Because <CODECLASS="literal">system </CODE>, <CODECLASS="literal">open</CODE>, and backticks all fork subprocesses and the operating system sends your process a SIGCHLD whenever any of its subprocesses exit, you could get called for something you weren't expecting. The built-in operations all wait for the child themselves, so sometimes the SIGCHLD will arrive before the <CODECLASS="literal">close</CODE> on the filehandle blocks to reap it. If the signal handler gets to it first, the zombie won't be there for the normal close. This makes <CODECLASS="literal">close</CODE> return false and set <CODECLASS="literal">$!</CODE> to <CODECLASS="literal">&quot;No</CODE> <CODECLASS="literal">child</CODE> <CODECLASS="literal">processes&quot;</CODE>. Then, if the <CODECLASS="literal">close</CODE> gets to the dead child first, <CODECLASS="literal">waitpid</CODE> will return <CODECLASS="literal">0</CODE>.</P><PCLASS="para">Most systems support a non-blocking <CODECLASS="literal">waitpid </CODE>. Use Perl's standard Config.pm module to find out:</P><PRECLASS="programlisting">use Config;$has_nonblocking = $Config{d_waitpid} eq &quot;define&quot; ||                   $Config{d_wait4}   eq &quot;define&quot;;</PRE><PCLASS="para">System V defines SIGCLD, which has the same signal number as SIGCHLD but subtly different semantics. Use SIGCHLD to avoid confusion.<ACLASS="indexterm"NAME="ch16-idx-1000006423-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006423-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006423-2"></A><ACLASS="indexterm"NAME="ch16-idx-1000006423-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000008386">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch06_02.htm#PERL2-CH-6-SECT-2.1">"Signals"</A> sections in <ACLASS="olink"HREF="../prog/ch06_01.htm">Chapter 6</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlipc </I>(1); the <ACLASS="olink"HREF="../prog/ch03_189.htm"><CODECLASS="literal">wait</CODE></A> and <ACLASS="olink"HREF="../prog/ch03_190.htm"><CODECLASS="literal">waitpid</CODE></A> functions in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlfunc </I>(1); the documentation for the standard POSIX module, in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; your system's <ICLASS="filename">sigaction </I>(2), <ICLASS="filename">signal</I> (3), and <ICLASS="filename">kill</I> (2) manpages (if you have them); <ACLASS="xref"HREF="ch16_18.htm"TITLE="Writing a Signal Handler">Recipe 16.17</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_19.htm"TITLE="16.18. Catching Ctrl-C"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.18. Catching Ctrl-C"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_21.htm"TITLE="16.20. Blocking Signals"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.20. Blocking Signals"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.18. Catching Ctrl-C</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">16.20. Blocking Signals</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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