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

📄 ch16_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
><ACLASS="indexterm"NAME="ch16-idx-1000006189-0"></A> to replace the program in the child process with a new one. You don't always both <CODECLASS="literal">fork</CODE> and <CODECLASS="literal">exec</CODE> together, so having them as separate primitives is more expressive and powerful than if all you could do is run <CODECLASS="literal">system </CODE>. In practice, you're more apt to use <CODECLASS="literal">fork</CODE> by itself than <CODECLASS="literal">exec</CODE> by itself.</P><PCLASS="para">When a child process dies, its memory is returned to the operating system, but its entry in the process table isn't freed. This lets a parent check the exit status of its child processes. Processes that have died but haven't been removed from the process table are called <EMCLASS="emphasis">zombie</EM><ACLASS="indexterm"NAME="ch16-idx-1000006187-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006187-1"></A>s, and you should clean them up lest they fill the whole process table. Backticks and the <CODECLASS="literal">system</CODE> and <CODECLASS="literal">open</CODE> functions automatically take care of this, and will work on most non-Unix systems. You have more to worry about when you go beyond these simple portable functions and use low-level primitives to launch programs. One thing to worry about is signals.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-42227">Signals</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006190-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006190-1"></A>Your process is notified of the death of a child it created with a <EMCLASS="emphasis">signal</EM>. Signals are a kind of notification delivered by the operating system. They are used for errors (when the kernel says, "Hey, you can't touch that area of memory!") and for events (death of a child, expiration of a per-process timer, interrupt with Ctrl-C). If you're launching processes manually, you normally arrange for a subroutine of your choosing to be called whenever one of your children exits.</P><PCLASS="para">Each process has a default disposition for each possible signal. You may install your own handler or otherwise change the disposition of most signals. Only SIGKILL and SIGSTOP cannot be changed. The rest you can ignore, trap, or block.</P><PCLASS="para">Briefly, here's a rundown of the more important signals:</P><DLCLASS="variablelist"><DTCLASS="term">SIGINT</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006200-0"></A>is normally triggered by Ctrl-C. This requests that a process interrupt what it's doing. Simple programs like filters usually just die, but more important ones like shells, editors, or FTP programs usually use SIGINT to stop long-running operations so you can tell them to do something else.</P></DD><DTCLASS="term">SIGQUIT</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006202-0"></A>is also normally generated by a terminal, usually Ctrl-\. Its default behavior is to generate a core dump.</P></DD><DTCLASS="term">SIGTERM</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006207-0"></A>is sent by the <EMCLASS="emphasis">kill</EM> shell command when no signal name is explicitly given. Think of it as a polite request for a process to die.</P></DD><DTCLASS="term">SIGUSR1 and SIGUSR2</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006212-0"></A>are never caused by system events, so user applications can safely use them for their own purposes.</P></DD><DTCLASS="term">SIGPIPE</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006217-0"></A>is sent by the kernel when your process tries to write to a pipe or socket when the process on the other end has closed its connection, usually because it no longer exists.</P></DD><DTCLASS="term">SIGALRM</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006222-0"></A>is sent when the timer set by the <CODECLASS="literal">alarm</CODE> function expires, as described in <ACLASS="xref"HREF="ch16_22.htm"TITLE="Timing Out an Operation">Recipe 16.21</A>.</P></DD><DTCLASS="term">SIGHUP</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006227-0"></A>is sent to a process when its controlling terminal gets a hang-up (e.g., the modem lost its carrier), but it also often indicates that a program should restart or reread its configuration.</P></DD><DTCLASS="term">SIGCHLD</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006232-0"></A>is probably the most important signal when it comes to low-level systems programming. The system sends your process a SIGCHLD when one of its child processes stops running&nbsp;- or, more likely, when that child exits. See <ACLASS="xref"HREF="ch16_20.htm"TITLE="Avoiding Zombie Processes">Recipe 16.19</A> for more on SIGCHLD.</P></DD></DL><PCLASS="para">Signal names are a convenience for humans. Each signal has an associated number that the operating system uses instead of names. Although we talk about SIGCHLD, your operating system only knows it as a number, like 20 (these numbers vary across operating systems). Perl translates between signal names and numbers for you, so you can think in terms of signal names.</P><PCLASS="para">Recipes <ACLASS="xref"HREF="ch16_16.htm"TITLE="Installing a Signal Handler">Recipe 16.15</A>, <ACLASS="xref"HREF="ch16_18.htm"TITLE="Writing a Signal Handler">Recipe 16.17</A>, <ACLASS="xref"HREF="ch16_22.htm"TITLE="Timing Out an Operation">Recipe 16.21</A>, <ACLASS="xref"HREF="ch16_19.htm"TITLE="Catching Ctrl-C">Recipe 16.18</A>, and <ACLASS="xref"HREF="ch16_21.htm"TITLE="Blocking Signals">Recipe 16.20</A> run the full gamut of signal handling.<ACLASS="indexterm"NAME="ch16-idx-1000006192-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006192-1"></A></P></DIV></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="ch15_20.htm"TITLE="15.19. Program: tkshufflepod"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.19. Program: tkshufflepod"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_02.htm"TITLE="16.1. Gathering Output from a Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.1. Gathering Output from a Program"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.19. Program: tkshufflepod</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.1. Gathering Output from a Program</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 + -