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

📄 ch16_11.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch16-idx-1000006353-0"></A>pipe3 - use forking open so parent can send to childuse IO::Handle;if ($pid = open(CHILD, &quot;|-&quot;)) {    CHILD-&gt;autoflush(1);    print CHILD &quot;Parent Pid $$ is sending this\n&quot;;    close(CHILD);} else {    die &quot;cannot fork: $!&quot; unless defined $pid;    chomp($line = &lt;STDIN&gt;);    print &quot;Child Pid $$ just read this: `$line'\n&quot;;    exit;}</PRE></DIV><PCLASS="para">Since the child already has STDIN set to the parent, the child could <CODECLASS="literal">exec</CODE> some other program that expects to read from standard input, such as <EMCLASS="emphasis">lpr</EM>. In fact, this is useful and commonly done.</P><PCLASS="para">If the child wants to write to the parent, it does something like what's shown in <ACLASS="xref"HREF="ch16_11.htm#ch16-42618"TITLE="pipe4">Example 16.6</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch16-42618">Example 16.6: pipe4</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch16-idx-1000006354-0"></A>pipe4 - use forking open so child can send to parentuse IO::Handle;if ($pid = open(CHILD, &quot;-|&quot;)) {    chomp($line = &lt;CHILD&gt;);    print &quot;Parent Pid $$ just read this: `$line'\n&quot;;    close(CHILD);} else {    die &quot;cannot fork: $!&quot; unless defined $pid;    STDOUT-&gt;autoflush(1);    print STDOUT &quot;Child Pid $$ is sending this\n&quot;;    exit;}</PRE></DIV><PCLASS="para">Again, since the child already has its STDOUT connected to the parent, this child could <CODECLASS="literal">exec</CODE> some other program to produce something interesting on its standard output. That output would be available to the parent as input from &lt;<CODECLASS="literal">CHILD&gt;</CODE>.</P><PCLASS="para">When using <CODECLASS="literal">open</CODE> this way, we don't have to manually call <CODECLASS="literal">waitpid</CODE> since we didn't do a manual fork. We do have to call <CODECLASS="literal">close</CODE>, though. In both cases, the <CODECLASS="literal">$?</CODE> variable will have the child's wait status in it (see <ACLASS="xref"HREF="ch16_20.htm"TITLE="Avoiding Zombie Processes">Recipe 16.19</A> to see how to interpret this status value).</P><PCLASS="para">The preceding examples were unidirectional. What if you want both processes talking to each other? Just make two calls to <CODECLASS="literal">pipe</CODE> before forking. You must be careful about who tells whom what and when, though, or you're apt to deadlock. (See <ACLASS="xref"HREF="ch16_11.htm#ch16-42071"TITLE="pipe5">Example 16.7</A>.)</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch16-42071">Example 16.7: pipe5</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch16-idx-1000006355-0"></A>pipe5 - bidirectional communication using two pipe pairs#         designed for the socketpair-challengeduse IO::Handle;pipe(PARENT_RDR, CHILD_WTR);pipe(CHILD_RDR,  PARENT_WTR);CHILD_WTR-&gt;autoflush(1);PARENT_WTR-&gt;autoflush(1);if ($pid = fork) {    close PARENT_RDR; close PARENT_WTR;    print CHILD_WTR &quot;Parent Pid $$ is sending this\n&quot;;    chomp($line = &lt;CHILD_RDR&gt;);    print &quot;Parent Pid $$ just read this: `$line'\n&quot;;    close CHILD_RDR; close CHILD_WTR;    waitpid($pid,0);} else {    die &quot;cannot fork: $!&quot; unless defined $pid;    close CHILD_RDR; close CHILD_WTR;    chomp($line = &lt;PARENT_RDR&gt;);    print &quot;Child Pid $$ just read this: `$line'\n&quot;;    print PARENT_WTR &quot;Child Pid $$ is sending this\n&quot;;    close PARENT_RDR; close PARENT_WTR;    exit;}</PRE></DIV><PCLASS="para">That's getting complicated. It just so happens that there's a special system call, shown in <ACLASS="xref"HREF="ch16_11.htm#ch16-38392"TITLE="pipe6">Example 16.8</A>, that makes the last example simpler. It's called <CODECLASS="literal">socketpair</CODE>, and it works like <CODECLASS="literal">pipe</CODE>, except that both handles can be used for reading and for writing.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch16-38392">Example 16.8: pipe6</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch16-idx-1000006356-0"></A>pipe6 - bidirectional communication using socketpair#   &quot;the best ones always go both ways&quot;use Socket;use IO::Handle;# We say AF_UNIX because although *_LOCAL is the# POSIX 1003.1g form of the constant, many machines# still don't have it.socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)    or  die &quot;socketpair: $!&quot;;CHILD-&gt;autoflush(1);PARENT-&gt;autoflush(1);if ($pid = fork) {    close PARENT;    print CHILD &quot;Parent Pid $$ is sending this\n&quot;;    chomp($line = &lt;CHILD&gt;);    print &quot;Parent Pid $$ just read this: `$line'\n&quot;;    close CHILD;    waitpid($pid,0);} else {    die &quot;cannot fork: $!&quot; unless defined $pid;    close CHILD;    chomp($line = &lt;PARENT&gt;);    print &quot;Child Pid $$ just read this: `$line'\n&quot;;    print PARENT &quot;Child Pid $$ is sending this\n&quot;;    close PARENT;    exit;}</PRE></DIV><PCLASS="para">In fact, some systems have historically implemented pipes as two half-closed ends of a socketpair. They essentially define <CODECLASS="literal">pipe(READER,</CODE> <CODECLASS="literal">WRITER)</CODE> this way:</P><PRECLASS="programlisting">socketpair(READER, WRITER, AF_UNIX, SOCK_STREAM, PF_UNSPEC);shutdown(READER, 1);        # no more writing for readershutdown(WRITER, 0);        # no more reading for writer</PRE><PCLASS="para">On Linux kernels before 2.0.34, the <ICLASS="filename">shutdown (2)</I> system call was broken. Instead of telling the reader not to write and the writer not to read, you had to tell the reader not to read and the writer not to write.<ACLASS="indexterm"NAME="ch16-idx-1000006347-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006347-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006347-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1950">See Also</A></H3><PCLASS="para"><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> or <ICLASS="filename">perlfunc </I>(1) for all functions used here; the documentation for the standard IPC::Open2 module; <EMCLASS="emphasis">Advanced Programming in the Unix Environment</EM>; <ACLASS="xref"HREF="ch16_09.htm"TITLE="Controlling Input and Output of Another Program">Recipe 16.8</A>; <ACLASS="xref"HREF="ch19_07.htm"TITLE="Executing Commands Without Shell Escapes">Recipe 19.6</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_10.htm"TITLE="16.9. Controlling the Input, Output, and Error of Another Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.9. Controlling the Input, Output, and Error of Another Program"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_12.htm"TITLE="16.11. Making a Process Look Like a File with Named Pipes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.11. Making a Process Look Like a File with Named Pipes"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.9. Controlling the Input, Output, and Error of Another Program</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.11. Making a Process Look Like a File with Named Pipes</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 + -