📄 ch16_19.htm
字号:
<HTML><HEAD><TITLE>Recipe 16.18. Catching Ctrl-C (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: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_18.htm"TITLE="16.17. Writing a Signal Handler"><LINKREL="next"HREF="ch16_20.htm"TITLE="16.19. Avoiding Zombie Processes"></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_18.htm"TITLE="16.17. Writing a Signal Handler"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.17. Writing a Signal Handler"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_20.htm"TITLE="16.19. Avoiding Zombie Processes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.19. Avoiding Zombie Processes"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-12862">16.18. Catching Ctrl-C</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005651">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006414-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006414-1"></A>You want to intercept Ctrl-C, which would otherwise kill your whole program. You'd like to ignore it or run your own function when the signal is received.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005654">Solution</A></H3><PCLASS="para">Set a handler for <ACLASS="indexterm"NAME="ch16-idx-1000006415-0"></A>SIGINT. Set it to <CODECLASS="literal">"IGNORE"</CODE> to make Ctrl-C have no effect:</P><PRECLASS="programlisting">$SIG{INT} = 'IGNORE';</PRE><PCLASS="para">Or, set it to a subroutine of your own devising to respond to Ctrl-C:</P><PRECLASS="programlisting">$SIG{INT} = \&tsktsk;sub tsktsk { $SIG{INT} = \&tsktsk; # See ``Writing A Signal Handler'' warn "\aThe long habit of living indisposeth us for dying.\n";}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005665">Discussion</A></H3><PCLASS="para">Ctrl-C isn't directly affecting your program. The terminal driver processing your keystrokes recognizes the Ctrl-C combination (or whatever you've set your terminal to recognize as the interrupt character), and sends a SIGINT to every process in the foreground process group (<ICLASS="firstterm">foreground job</I>) for that terminal. The foreground job normally comprises all programs started from the shell on a single command line, plus any other programs run by those programs. See the section on "<ACLASS="xref"HREF="ch16_01.htm#ch16-42227"TITLE="Signals">"Signals</A>" in the Introduction to this chapter for details.</P><PCLASS="para">The interrupt character isn't the only special character interpreted by your terminal driver. Type <CODECLASS="literal">stty</CODE> <CODECLASS="literal">-a</CODE> to find out your current settings:</P><PRECLASS="programlisting">% stty -a<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>speed 9600 baud; 38 rows; 80 columns;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> -extproc</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> brkint -inpck -ignpar -parmrk</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>oflags: opost onlcr oxtabs</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> -dtrflow -mdmbuf</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef;></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> eol2 = <undef; erase = ^H; intr = ^C; kill = ^U; lnext = ^V;></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> min = 1; quit = ^\; reprint = ^R; start = ^Q; status = <undef;></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> stop = ^S; susp = ^Z; time = 0; werase = ^W;</I></CODE></B></CODE></PRE><PCLASS="para">The last section, <CODECLASS="literal">cchars:</CODE>, is the list of special characters. <ACLASS="xref"HREF="ch15_09.htm"TITLE="Using POSIX termios">Recipe 15.8</A> shows you how to change these from your script without calling the <EMCLASS="emphasis">stty</EM> program.<ACLASS="indexterm"NAME="ch16-idx-1000006417-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006417-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005690">See Also</A></H3><PCLASS="para">Your system's <ICLASS="filename">stty </I>(1) manpage (if you have one); <ACLASS="xref"HREF="ch15_09.htm"TITLE="Using POSIX termios">Recipe 15.8</A>; <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_18.htm"TITLE="16.17. Writing a Signal Handler"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.17. Writing a Signal Handler"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_20.htm"TITLE="16.19. Avoiding Zombie Processes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.19. Avoiding Zombie Processes"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.17. Writing a Signal Handler</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.19. Avoiding Zombie Processes</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 + -