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

📄 ch14_06.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Getting Down and Dirty with Fork (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch14_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">14.6. Getting Down and Dirty with Fork</h2><p>In addition to the high-level interfaces already described, Perlprovides nearly direct access to the low-level process managementsystem calls of Unix and some other systems. If you've neverdone this before,<a href="#FOOTNOTE-324">[324]</a> youwill probably want to skip this section. While it's a bit muchto cover all that stuff in a chapter like this, let's at leastlook at a quick reimplementation of this:</p><blockquote class="footnote"> <a name="FOOTNOTE-324" /><p>[324]Or you're not running on asystem that has support for forking. But the Perl developers areworking hard to add forking even on systems whose underlying processmodel is very different than the one in Unix.</p> </blockquote><blockquote><pre class="code">system "date";</pre></blockquote><p>Let's look at how that would be done using the low-level systemcalls:</p><blockquote><pre class="code">defined(my $pid = fork) or die "Cannot fork: $!";unless ($pid) {  # Child process is here  exec "date";  die "cannot exec date: $!";}# Parent process is herewaitpid($pid, 0);</pre></blockquote><p>Here, we've checked the return value from<a name="INDEX-985" /><tt class="literal">fork</tt>, which will be<tt class="literal">undef</tt> if it failed. Usually it will succeed,causing two separate processes to continue to the next line, but onlythe parent process has a nonzero value in <tt class="literal">$pid</tt>, soonly the child process executes the <tt class="literal">exec</tt>function. The parent process skips over that and executes the<tt class="literal">waitpid</tt><a name="INDEX-986" /> function, waiting for that particularchild to finish (if others finish in the meantime, they are ignored).If that all sounds like gobbledygook, just remember that you cancontinue to use the <tt class="literal">system</tt> function withoutbeing laughed at by your friends.</p><p>When you go to this extra trouble, you also have full control overarbitary pipe creation, rearranging filehandles, and noticing yourprocess ID and your parent's process ID (if knowable). Butagain, that's all a bit complicated for this chapter, so seethe details in the<em class="emphasis">perlipc</em><a name="INDEX-987" /> manpage (and in any good book onapplication programming on your system) for further information.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch14_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">14.5. Processes as Filehandles</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">14.7. Sending and Receiving Signals</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><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 + -