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

📄 ch16_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><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:43:36Z"><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="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch15_20.htm"TITLE="15.19. Program: tkshufflepod"><LINKREL="next"HREF="ch16_02.htm"TITLE="16.1. Gathering Output from a Program"></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="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"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></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></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch16-31304">16. Process Management and Communication</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch16-39961"TITLE="16.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch16_02.htm"TITLE="16.1. Gathering Output from a Program">Gathering Output from a Program</A><BR><ACLASS="sect1"HREF="ch16_03.htm"TITLE="16.2. Running Another Program">Running Another Program</A><BR><ACLASS="sect1"HREF="ch16_04.htm"TITLE="16.3. Replacing the Current Program with a Different One">Replacing the Current Program with a Different One</A><BR><ACLASS="sect1"HREF="ch16_05.htm"TITLE="16.4. Reading or Writing to Another Program">Reading or Writing to Another Program</A><BR><ACLASS="sect1"HREF="ch16_06.htm"TITLE="16.5. Filtering Your Own Output">Filtering Your Own Output</A><BR><ACLASS="sect1"HREF="ch16_07.htm"TITLE="16.6. Preprocessing Input">Preprocessing Input</A><BR><ACLASS="sect1"HREF="ch16_08.htm"TITLE="16.7. Reading STDERR from a Program">Reading STDERR from a Program</A><BR><ACLASS="sect1"HREF="ch16_09.htm"TITLE="16.8. Controlling Input and Output of Another Program">Controlling Input and Output of Another Program</A><BR><ACLASS="sect1"HREF="ch16_10.htm"TITLE="16.9. Controlling the Input, Output, and Error of Another Program">Controlling the Input, Output, and Error of Another Program</A><BR><ACLASS="sect1"HREF="ch16_11.htm"TITLE="16.10. Communicating Between Related Processes">Communicating Between Related Processes</A><BR><ACLASS="sect1"HREF="ch16_12.htm"TITLE="16.11. Making a Process Look Like a File with Named Pipes">Making a Process Look Like a File with Named Pipes</A><BR><ACLASS="sect1"HREF="ch16_13.htm"TITLE="16.12. Sharing Variables in Different Processes">Sharing Variables in Different Processes</A><BR><ACLASS="sect1"HREF="ch16_14.htm"TITLE="16.13. Listing Available Signals">Listing Available Signals</A><BR><ACLASS="sect1"HREF="ch16_15.htm"TITLE="16.14. Sending a Signal">Sending a Signal</A><BR><ACLASS="sect1"HREF="ch16_16.htm"TITLE="16.15. Installing a Signal Handler">Installing a Signal Handler</A><BR><ACLASS="sect1"HREF="ch16_17.htm"TITLE="16.16. Temporarily Overriding a Signal Handler">Temporarily Overriding a Signal Handler</A><BR><ACLASS="sect1"HREF="ch16_18.htm"TITLE="16.17. Writing a Signal Handler">Writing a Signal Handler</A><BR><ACLASS="sect1"HREF="ch16_19.htm"TITLE="16.18. Catching Ctrl-C">Catching Ctrl-C</A><BR><ACLASS="sect1"HREF="ch16_20.htm"TITLE="16.19. Avoiding Zombie Processes">Avoiding Zombie Processes</A><BR><ACLASS="sect1"HREF="ch16_21.htm"TITLE="16.20. Blocking Signals">Blocking Signals</A><BR><ACLASS="sect1"HREF="ch16_22.htm"TITLE="16.21. Timing Out an Operation">Timing Out an Operation</A><BR><ACLASS="sect1"HREF="ch16_23.htm"TITLE="16.22. Program: sigrand">Program: sigrand</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>It is quite a three-pipe problem, and I beg that you won't speak to me for fifty minutes.</I></P><PCLASS="attribution"ALIGN="right">-&nbsp;Sherlock Holmes <CITECLASS="citetitle">The Red-Headed League </CITE></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-39961">16.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006183-0"></A>Perl may be many things to many people, but to most of us it is the glue that connects diverse components. This chapter is about launching commands and connecting separate processes together. It's about managing their creation, communication, and ultimate demise. It's about systems programming.</P><PCLASS="para">When it comes to systems programming, Perl, as usual, makes easy things easy and hard things possible. If you want to use it as you would the shell, Perl is happy to assist you. If you want to roll up your sleeves for low-level hacking like a hardcore C programmer, you can do that, too.</P><PCLASS="para">Because Perl lets you get so close to the system, portability issues can sneak in. This chapter is the most Unix-centric chapter of the book. It will be tremendously useful to those on Unix systems, but only of limited use to others. We deal with features that aren't as universal as strings and numbers and basic arithmetic. Most basic operations work more or less the same everywhere. But if you're not using some kind of Unix or other POSIX conformant system, most of the interesting features in this chapter may work differently for you&nbsp;- or not at all. Check the documentation that came with your Perl port if you aren't sure.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-chap16_process_1">Process Creation</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006185-0"></A>In this chapter, we cover the proper care and feeding of your own child processes. Sometimes this means launching a stand-alone command and letting it have its own way with the world (using <CODECLASS="literal">system</CODE>). Other times it means keeping a tight rein on your child, feeding it carefully filtered input or taking hold of its output stream (backticks and piped <CODECLASS="literal">open</CODE>s). Without even starting a new process, you can use <CODECLASS="literal">exec</CODE> to replace your current program with a completely different program.</P><PCLASS="para">We first show how to use the most portable and commonly used operations for managing processes: backticks, <CODECLASS="literal">system</CODE> , <CODECLASS="literal">open </CODE>, and the manipulation of the <CODECLASS="literal">%SIG</CODE> hash. Those are the easy things, but we don't stop there. We also show what to do when the simple approach isn't good enough.</P><PCLASS="para">For example, you might want to interrupt your program while it's running a different program. Maybe you need to process a child program's standard error separately from its standard output. Perhaps you need to control both the input and output of a program simultaneously. When you tire of having just one thread of control and begin to take advantage of multitasking, you'll want to learn how to split your current program into several, simultaneously running processes that all talk to each other.</P><PCLASS="para">For tasks like these, you have to drop back to the underlying system calls: <CODECLASS="literal">pipe</CODE>, <CODECLASS="literal">fork</CODE> , and <CODECLASS="literal">exec</CODE>. The <CODECLASS="literal">pipe</CODE><ACLASS="indexterm"NAME="ch16-idx-1000006186-0"></A> function creates two connected filehandles, a reader and writer, whereby anything written to the writer can be read from the reader. The <CODECLASS="literal">fork</CODE><ACLASS="indexterm"NAME="ch16-idx-1000006188-0"></A> function is the basis of multitasking, but unfortunately it has not been supported on all non-Unix systems. It clones off a duplicate process identical in virtually every aspect to its parent, including variable settings and open files. The most noticable changes are the process ID and parent process ID. New programs are started by forking, then using <CODECLASS="literal">exec</CODE

⌨️ 快捷键说明

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