📄 ch16_15.htm
字号:
<HTML><HEAD><TITLE>Recipe 16.14. Sending a Signal (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:08Z"><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_14.htm"TITLE="16.13. Listing Available Signals"><LINKREL="next"HREF="ch16_16.htm"TITLE="16.15. Installing a Signal Handler"></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_14.htm"TITLE="16.13. Listing Available Signals"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.13. Listing Available Signals"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_16.htm"TITLE="16.15. Installing a Signal Handler"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.15. Installing a Signal Handler"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-96840">16.14. Sending a Signal</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005374">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006375-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006375-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006375-2"></A>You want to send a signal to a process. This could be sent to your own process or to another on the same system. For instance, you caught SIGINT and want to pass it on to your children.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005377">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">kill</CODE> to send a signal by name or number to the process IDs listed in the remaining arguments:</P><PRECLASS="programlisting">kill 9 => $pid; # send $pid a signal 9kill -1 => $pgrp; # send whole job a signal 1kill USR1 => $$; # send myself a SIGUSR1kill HUP => @pids; # send a SIGHUP to processes in @pids</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005384">Discussion</A></H3><PCLASS="para">Perl's <CODECLASS="literal">kill</CODE> function is an interface to the system call of the same name. The first argument is the signal to send, identified by number or by name; subsequent arguments are process IDs to send the signal to. It returns the count of processes successfully signaled. You can only send signals to processes running under the same real or saved UID as your real or effective UID - unless you're the superuser.</P><PCLASS="para">If the signal number is negative, Perl interprets remaining arguments as <ACLASS="indexterm"NAME="ch16-idx-1000008952-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000008952-1"></A>process group IDs and sends that signal to all those groups' processes using the <ICLASS="filename">killpg </I>(2) system call.</P><PCLASS="para">A process group is essentially a job. It's how the operating system ties related processes together. For example, when you use your shell to pipe one command into another, you've started two processes, but only one job. When you use Ctrl-C to interrupt the current job, or Ctrl-Z to suspend it, this sends the appropriate signals to the entire job, which may be more than one process.</P><PCLASS="para"><CODECLASS="literal">kill</CODE> can also check whether a process is alive. Sending the special pseudo-signal number 0 checks whether it's legal for you to send a signal to the process - without actually sending one. If it returns true, the process is still alive. If it returns false, the process has either changed its effective UID (in which case <CODECLASS="literal">$!</CODE> will be set to EPERM) or no longer exists (and <CODECLASS="literal">$!</CODE> is ESRCH). Zombie processes (as described in <ACLASS="xref"HREF="ch16_20.htm"TITLE="Avoiding Zombie Processes">Recipe 16.19</A>) also report back as ESRCH.</P><PRECLASS="programlisting">use POSIX qw(:errno_h);if (kill 0 => $minion) { print "$minion is alive!\n";} elsif ($! == EPERM) { # changed uid print "$minion has escaped my control!\n";} elsif ($! == ESRCH) { print "$minion is deceased.\n"; # or zombied} else { warn "Odd; I couldn't check on the status of $minion: $!\n";}<ACLASS="indexterm"NAME="ch16-idx-1000006377-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006377-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006377-2"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005404">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); your system's <ICLASS="filename">sigaction </I>(2), <ICLASS="filename">signal </I>(3), and <ICLASS="filename">kill </I>(2) manpages (if you have them); the <ACLASS="olink"HREF="../prog/ch03_080.htm"><CODECLASS="literal">kill</CODE></A> function 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 <ICLASS="filename">perlfunc </I>(1)</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_14.htm"TITLE="16.13. Listing Available Signals"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.13. Listing Available Signals"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_16.htm"TITLE="16.15. Installing a Signal Handler"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.15. Installing a Signal Handler"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.13. Listing Available Signals</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.15. Installing a Signal Handler</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 © 2002</a> O'Reilly & 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 + -