📄 ch01_28.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.28 Some Gotchas with Background Processing </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:31:37Z"><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="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_27.htm"TITLE="1.27 How Background Processing Works "><LINKREL="next"HREF="ch01_29.htm"TITLE="1.29 When Is a File Not a File? "></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="ch01_27.htm"TITLE="1.27 How Background Processing Works "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.27 How Background Processing Works "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_29.htm"TITLE="1.29 When Is a File Not a File? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.29 When Is a File Not a File? "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7730">1.28 Some Gotchas with Background Processing </A></H2><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2378"></A>If you're using the Bourne shell, you have to watch out forputting a series of commands separated by<SPANCLASS="link">semicolons (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>into thebackground. The Bourne shell puts only the last command on theline into the background, but waits for the first.</P><PCLASS="para">An easy way to test this is with the following command line, whichwaits for 15 seconds, then does an <EMCLASS="emphasis">ls</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>sleep 15; ls &</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In the Bourne shell, you won't get your prompt back until the<SPANCLASS="link"><EMCLASS="emphasis">sleep</EM> (<ACLASS="linkend"HREF="ch40_02.htm"TITLE="Waiting a Little While: sleep ">40.2</A>)</SPAN>command has finished.</P><PCLASS="para">The proper way to put a series of Bourne shell commands intothe background is to group them with parentheses:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch13_07.htm"TITLE="13.7 The () Subshell Operators ">( )</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>(sleep 15; ls)&</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">This may strike you as a defect, but in fact, it's a sign ofthe greater precision of Bourne shell syntax, which makes itsomewhat exasperating for interactive use, but much better forprogramming.</P></LI><LICLASS="listitem"><PCLASS="para">It doesn't make any sense to run an interactive program such asan editor in the background. For example, if you type this from the C shell:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>vi &</B></CODE>[1] 3071</PRE></BLOCKQUOTE></P><PCLASS="para">you'll get a message like the following:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">[1] + Stopped (tty output) vi</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">vi</EM> can be active only in the foreground. However, it does makesense to have <EMCLASS="emphasis">vi</EM><SPANCLASS="link">stopped (<ACLASS="linkend"HREF="ch12_08.htm"TITLE="Job Control in a Nutshell ">12.8</A>)</SPAN>in the background.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2409"></A>If you are running <EMCLASS="emphasis">vi</EM> or any other interactive program, you can quickly getback to the shell by typing CTRL-z to stop the program.The shell will take control of your terminal and print another shell prompt.</P><PCLASS="para"><SPANCLASS="link">Stopping <EMCLASS="emphasis">vi</EM> (<ACLASS="linkend"HREF="ch12_04.htm"TITLE="Job Control and autowrite: Real Time Savers! ">12.4</A>)</SPAN>is more efficient than using its<SPANCLASS="link">shell escape mechanism (<ACLASS="linkend"HREF="ch30_26.htm"TITLE="Shell Escapes: Running One UNIX Command While Using Another">30.26</A>)</SPAN>,since it lets you go back to your original shell rather thanstarting a new one. Simply type <CODECLASS="literal">fg</CODE> to get back to where youwere in editing.</P></LI><LICLASS="listitem"><PCLASS="para">We have shared a system with new users who were overenthusiasticusers of background processes, rather like the man who loved lovingso much he sought many lovers. Because each background process iscompeting for the same resources, running many of themcan be a drain on the system. This means that everything takes longerfor everyone. We used to have people who thought that if they ranthree<SPANCLASS="link"><EMCLASS="emphasis">troff</EM> (<ACLASS="linkend"HREF="ch43_13.htm"TITLE="The Text Formatters nroff, troff, ditroff, ... ">43.13</A>)</SPAN>processes at once, they'd get their three files formatted faster thanif they did them one after another. Boy, were they mistaken.[4]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[4] In the old days, UNIX systems gave all processes to a singleCPU. Modern UNIX systems can have multiple CPUs. On thesesystems, you may do several jobs almost as quickly as one.</P></BLOCKQUOTE></LI><LICLASS="listitem"><PCLASS="para">If you use the Bourne shell,any background processes you have running will normallybe terminated when you log out. To avoid this, use the<SPANCLASS="link"><EMCLASS="emphasis">nohup</EM> (<ACLASS="linkend"HREF="ch38_18.htm"TITLE="nohup ">38.18</A>)</SPAN><ACLASS="indexterm"NAME="AUTOID-2427"></A>command.</P></LI><LICLASS="listitem"><PCLASS="para">Not all processes are created equal. UNIX maintains a queueof processes ordered by priority. Foreground processes, suchas a user typing a command at a prompt, often receive higher priority thanbackground processes. However, youmay want to run background processes at an even lower priority, byusing<ACLASS="indexterm"NAME="AUTOID-2432"></A><SPANCLASS="link"><EMCLASS="emphasis">nice</EM> (<ACLASS="linkend"HREF="ch39_09.htm"TITLE='Know When to Be "nice" to OTher Users...and WhenNot to'>39.9</A>)</SPAN>.This is a relatively painless way of being kind to other users - and makingyour foreground job run faster - though it will make your background tasks takea little longer.</P></LI></OL><ACLASS="indexterm"NAME="AUTOID-2437"></A><ACLASS="indexterm"NAME="AUTOID-2438"></A><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN>, <SPANCLASS="authorinitials">DD</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="ch01_27.htm"TITLE="1.27 How Background Processing Works "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.27 How Background Processing Works "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="ch01_29.htm"TITLE="1.29 When Is a File Not a File? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.29 When Is a File Not a File? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.27 How Background Processing Works </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">1.29 When Is a File Not a File? </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 + -