📄 ch38_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 38] 38.2 fork and exec </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:49:29Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-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="ch38_01.htm"TITLE="38. Starting, Stopping, and Killing Processes"><LINKREL="prev"HREF="ch38_01.htm"TITLE="38.1 What's in This Chapter "><LINKREL="next"HREF="ch38_03.htm"TITLE="38.3 Managing Processes: Overall Concepts "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_01.htm"TITLE="38.1 What's in This Chapter "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.1 What's in This Chapter "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 38<BR>Starting, Stopping, and Killing Processes</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_03.htm"TITLE="38.3 Managing Processes: Overall Concepts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.3 Managing Processes: Overall Concepts "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4880">38.2 fork and exec </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-42164"></A><ACLASS="indexterm"NAME="AUTOID-42166"></A><ACLASS="indexterm"NAME="AUTOID-42168"></A><ACLASS="indexterm"NAME="AUTOID-42170"></A><ACLASS="indexterm"NAME="AUTOID-42172"></A><ACLASS="indexterm"NAME="AUTOID-42175"></A>We've already discussed <EMCLASS="emphasis">fork</EM> and <EMCLASS="emphasis">exec</EM> way back in article<ACLASS="xref"HREF="ch01_11.htm"TITLE="How the Shell Executes Other Commands ">1.11</A>,but the concept comes up so often in this chapter that we thought weought to have a closer cross reference.</P><PCLASS="para">Put simply, <EMCLASS="emphasis">fork</EM> and <EMCLASS="emphasis">exec</EM> are the UNIX system calls (requestsfor operating system services) that UNIX programs use to create newprocesses.When you start up a UNIX system, it starts with only oneprocess, a program called <EMCLASS="emphasis">init</EM>.<ACLASS="indexterm"NAME="AUTOID-42185"></A></P><PCLASS="para">How does <EMCLASS="emphasis">init</EM> magically turn into the hundreds or perhaps eventhousands of processes that make up a working UNIX system?That's where <EMCLASS="emphasis">fork</EM> and <EMCLASS="emphasis">exec</EM> come in.</P><PCLASS="para">One process spawns another ("spawn" is another term you should getused to seeing) either by replacing itself when it's done - an<EMCLASS="emphasis">exec</EM> - or if it needs to stay around, by making a copy of itself - a<EMCLASS="emphasis">fork</EM>.<ACLASS="indexterm"NAME="AUTOID-42194"></A><ACLASS="indexterm"NAME="AUTOID-42196"></A>In the latter case, the forked copy commits politesuicide by <EMCLASS="emphasis">exec</EM>ing the desired second program.</P><PCLASS="para">A good example of this whole sequence can be seen in the waya UNIX system's login procedure for terminals (non-<SPANCLASS="link">network (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>logins) works.<ACLASS="indexterm"NAME="AUTOID-42201"></A>The <EMCLASS="emphasis">init</EM> process spawns a series of <EMCLASS="emphasis">getty</EM>processes, each of which monitors a serial port (a <EMCLASS="emphasis">tty</EM>) looking foractivity.It's the <EMCLASS="emphasis">getty</EM> program that actually puts up thefirst <CODECLASS="literal">login:</CODE> prompt.</P><PCLASS="para">Once someone actually types a login name, <EMCLASS="emphasis">getty</EM>'s job is done;it <EMCLASS="emphasis">exec</EM>s the <EMCLASS="emphasis">login</EM> command. <EMCLASS="emphasis">login</EM> prompts for apassword (if the account has one) and, if the password is okay,<EMCLASS="emphasis">exec</EM>s the login shell. Whenever you start another program,the shell <EMCLASS="emphasis">fork</EM>s itself, and the copy <EMCLASS="emphasis">exec</EM>s whateverprogram you asked to run.</P><PCLASS="para">That's why <SPANCLASS="link">some commands are built-in to the shell (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>.There's overhead involved in starting a new process. What's more,because<SPANCLASS="link">a child process can't affect its parent's environment (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>,some commands don't make sense as separate processes. For example,<EMCLASS="emphasis">cd</EM> must be built in, or it couldn't change the working directoryfor the current shell.</P><PCLASS="para">There's an <EMCLASS="emphasis">exec</EM> command that you can type at a shell prompt; seearticle<ACLASS="xref"HREF="ch45_07.htm"TITLE="The exec Command ">45.7</A>.Watch out, though, it will replace your shell with whatever commandyou <EMCLASS="emphasis">exec</EM>, with no going back. This is useful only if you wantto replace your shell with some other interactive command interpreterwith similar powers (as in article<ACLASS="xref"HREF="ch22_22.htm"TITLE="The su Command Isn't Just for the Superuser ">22.22</A>),or if you'll be ready to log out when the command you <EMCLASS="emphasis">exec</EM>finishes.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_01.htm"TITLE="38.1 What's in This Chapter "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.1 What's in This Chapter "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_03.htm"TITLE="38.3 Managing Processes: Overall Concepts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.3 Managing Processes: Overall Concepts "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">38.1 What's in This Chapter </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">38.3 Managing Processes: Overall Concepts </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -