📄 ch39_09.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 39] 39.9 Know When to Be "nice" to OTher Users...and WhenNot to</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:50:20Z"><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="ch39_01.htm"TITLE="39. Time and Performance"><LINKREL="prev"HREF="ch39_08.htm"TITLE="39.8 A Big Environment Can Slow You Down "><LINKREL="next"HREF="ch39_10.htm"TITLE="39.10 A nice Gotcha "></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="ch39_08.htm"TITLE="39.8 A Big Environment Can Slow You Down "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 39.8 A Big Environment Can Slow You Down "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 39<BR>Time and Performance</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch39_10.htm"TITLE="39.10 A nice Gotcha "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 39.10 A nice Gotcha "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0068">39.9 Know When to Be "nice" to OTher Users...and WhenNot to</A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="nice">nice</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-44001"></A><ACLASS="indexterm"NAME="AUTOID-44003"></A><ACLASS="indexterm"NAME="AUTOID-44006"></A>The <EMCLASS="emphasis">nice</EM> command modifies the scheduling priority of time-sharingprocesses (for BSD and pre-V.4 releases of System V, all processes).The GNU version is on the CD-ROM (the disc's installsystem will only install <EMCLASS="emphasis">nice</EM> if your system has the appropriatefacilities).</TD></TR></TABLE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44013"></A><ACLASS="indexterm"NAME="AUTOID-44016"></A><ACLASS="indexterm"NAME="AUTOID-44019"></A><ACLASS="indexterm"NAME="AUTOID-44021"></A>If you're not familiar with UNIX, you will find its definition ofpriority confusing - it's the opposite of what you would expect. A process with ahigh <EMCLASS="emphasis">nice</EM> number runs at low priority, getting relatively little ofthe processor's attention; similarly, jobs with a low <EMCLASS="emphasis">nice</EM> numberrun at high priority. This is why the <EMCLASS="emphasis">nice</EM> number isusually called <EMCLASS="emphasis">niceness</EM>: a job with a lot of niceness is very kindto the other users of your system (i.e., it runs at low priority), whilea job with little niceness will hog the CPU. The term "niceness" isawkward, like the priority system itself. Unfortunately, it's the onlyterm that is both accurate (<EMCLASS="emphasis">nice</EM> numbers are used to computepriorities but are not the priorities themselves) and avoids horrible circumlocutions ("increasing the priority means lowering the priority...").</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44029"></A>Many supposedly experienced users claim that<EMCLASS="emphasis">nice</EM> has virtually no effect. Don't listen to them. As ageneral rule, reducing the priority of an I/O-bound job (a job that's waitingfor I/O a lot of the time) won't change things very much. The system rewards jobs that spend most oftheir time waiting for I/O by increasing their priority. But reducingthe priority of a CPU-bound process can have a significant effect.Compilations, batch typesetting programs (<EMCLASS="emphasis">troff</EM>, TeX, etc.),applications that do a lot of math, and similar programs are good candidates for <EMCLASS="emphasis">nice</EM>. On a moderately loaded system, I havefound that <EMCLASS="emphasis">nice</EM> typically makes a CPU-intensive job roughly 30percent slower and consequently frees that much time for higherpriority jobs. You can often significantly improve keyboard response by running CPU-intensive jobs at low priority.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44037"></A><ACLASS="indexterm"NAME="AUTOID-44039"></A>Note that System V Release 4 has a much more complex priority system, includingreal-time priorities. Priorities are managed with the <EMCLASS="emphasis">priocntl</EM> command. The older <EMCLASS="emphasis">nice</EM> command is available for compatibility.Other UNIX implementations (including HP and Concurrent) support real-time scheduling. These implementations have their own tools for managing the scheduler.</P><PCLASS="para">The <EMCLASS="emphasis">nice</EM> command sets a job's niceness, which is used to computeits priority. It may be one of the most non-uniform commands in theuniverse. There are four versions, each slightly different from the others.BSD UNIX has one <EMCLASS="emphasis">nice</EM> that is built into the C shell,and another standalone version can be used by other shells.System V also has one <EMCLASS="emphasis">nice</EM> that is built into the C shelland a separate standalone version.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44048"></A>Under BSD UNIX, you must also know about <SPANCLASS="link">the <EMCLASS="emphasis">renice</EM>(8) command (<ACLASS="linkend"HREF="ch39_11.htm"TITLE="Changing a Job's Priority Under BSD UNIX ">39.11</A>)</SPAN>;this lets you change the niceness of a job after it isrunning. Under System V, you can't modify a job's niceness once ithas started, so there is no equivalent.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> Think carefully before you <EMCLASS="emphasis">nice</EM> an interactive job like a texteditor.See article<ACLASS="xref"HREF="ch39_10.htm"TITLE="A nice Gotcha ">39.10</A>.</P></BLOCKQUOTE><PCLASS="para">We'll tackle the different variations of <EMCLASS="emphasis">nice</EM> in order.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-68-SECT-1.0.1">39.9.1 BSD C Shell nice </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44061"></A>Under BSD UNIX, <EMCLASS="emphasis">nice</EM> numbers run from -20 to 20. The -20 designationcorresponds to the highest priority; 20 corresponds to the lowest.By default, UNIX assigns the <EMCLASS="emphasis">nice</EM> number 0 to user-executed jobs.The lowest <EMCLASS="emphasis">nice</EM> numbers (-20 to -17) are unofficially reserved forsystem processes. Assigning a user's job to these <EMCLASS="emphasis">nice</EM> numbers cancause problems. Users can always request a higher <EMCLASS="emphasis">nice</EM> number (i.e., a lower priority) for their jobs.Only the<SPANCLASS="link">superuser (<ACLASS="linkend"HREF="ch01_24.htm"TITLE="The Superuser (Root) ">1.24</A>)</SPAN>can raise ajob's priority.</P><PCLASS="para">To submit a job at a greater niceness, precede it with the modifier<EMCLASS="emphasis">nice</EM>. For example, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>nice awk -f proc.awk datafile > awk.out</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">runs an <EMCLASS="emphasis">awk</EM> command at low priority. By default, <EMCLASS="emphasis">csh</EM> versionof <EMCLASS="emphasis">nice</EM> willsubmit this job with a <EMCLASS="emphasis">nice</EM> level of 4. To submit a job with anarbitrary <EMCLASS="emphasis">nice</EM> number, use <EMCLASS="emphasis">nice</EM> one of these ways:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>nice +</B></CODE><CODECLASS="replaceable"><I>n</I></CODE><CODECLASS="userinput"><B> command</B></CODE>% <CODECLASS="userinput"><B>nice -</B></CODE><CODECLASS="replaceable"><I>n</I></CODE><CODECLASS="userinput"><B> command</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">where <EMCLASS="emphasis">n</EM> is an integer between 0 and 20. The <EMCLASS="emphasis">+n</EM> designationrequests a positive <EMCLASS="emphasis">nice</EM> number (low priority); <EMCLASS="emphasis">-n</EM> request anegative <EMCLASS="emphasis">nice</EM> number. Only a superuser may request a negative<EMCLASS="emphasis">nice</EM> number.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-68-SECT-1.0.2">39.9.2 BSD Standalone nice </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44099"></A>The standalone version of <EMCLASS="emphasis">nice</EM> differs from C shell <EMCLASS="emphasis">nice</EM> in that it is a separate program, not a command built in to the C shell.You can therefore use the standalone version in any situation: within
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -