📄 unx19.htm
字号:
<P>On BSD-based systems, the renice command takes arguments in this manner:
<BR></P>
<PRE>renice priority [ [-p] pid ... ] [ -g pgrp ... ] [ -u userid ... ]</PRE>
<P>The priority is the new nice value desired for the processes to be changed. The -p option (the default) allows a list of process identifiers; you should get these from ps or by saving the PID of each background task you start. The -g option allows a
list of process groups; if you are using a shell that does job control you should get this from the PID of each background task you start or by using ps and using the PID of the process that has a PPID that is your shell's PID. The -u option outputs a list
of user IDs; unless you have appropriate privileges (usually only if you are root), you will be able to change only your own processes. If you want to make all of your current processes nicer, you can use renice -u <I>yourusername</I>. Remember that this
will affect your login shell! This means that any command you start after renice will have lower priority.
<BR></P>
<P>Here is an example of using renice on a single process. You start a long job (called longjob) and then realize you have an important job (called impjob) to run. After you start impjob, you can do a ps to see that longjob is PID 27662. Then you run
renice 20 27662 to make longjob have a lower priority. If you immediately run ps l (try ps -l on a SYSV system that has renice), you will see that longjob has a higher nice value (see the NI column). If you wait a bit and do another ps l, you should notice
that impjob is getting more CPU time (see the TIME column).
<BR></P>
<PRE>$ longjob &
27662
$ impjob &
28687
$ ps l
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
240801 S 343 24076 29195 0 60 20 4231 88 268 pts/4 0:00 -sh
240001 R 343 26398 24076 4 62 20 4e52 108 204 pts/4 0:00 ps l
241001 R 343 27662 24076 52 86 20 49d0 32 40 pts/4 0:03 longjob
241001 R 343 28687 24076 52 86 20 256b 32 40 pts/4 0:00 impjob
$ renice 20 27662
27662: old priority 0, new priority 20
$ ps l
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
240001 R 343 18017 24076 3 61 20 60b8 108 204 pts/4 0:00 ps l
240801 S 343 24076 29195 0 60 20 4231 88 268 pts/4 0:00 -sh
241001 R 343 27662 24076 32 96 40 49d0 32 40 pts/4 0:09 longjob
241001 R 343 28687 24076 52 86 20 256b 32 40 pts/4 0:07 impjob
$ # Wait a bit
$ ps l
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
240801 S 343 24076 29195 0 60 20 4231 88 268 pts/4 0:00 -sh
241001 R 343 27662 24076 74 117 40 49d0 32 40 pts/4 0:31 longjob
241001 R 343 28687 24076 115 117 20 256b 32 40 pts/4 0:41 impjob
240001 R 343 29821 24076 4 62 20 4ff2 108 204 pts/4 0:00 ps l
$ </PRE>
<P>Some jobs you run may start multiple processes, but renice -p will affect only one of them. One way to get around this is to use ps to find all of the processes and list each one to renice -p. If you are using a job control shell (for example, Korn
shell or C shell), you may be able to use renice -g. In the following example, longjob spawns several sub-processes to help do more work (see the output of the first ps l). Notice that if you use renice -p you affect only the parent process's nice value
(see the output of the second ps l). If you are using a shell that does job control, your background process should have been put in its own process group with a process group ID the same as its process ID. Try renice 20 -g <I>PID</I> and see if it works.
Notice in the output of the third ps l that all of the children of longjob have had their nice values changed.
<BR></P>
<PRE>$ longjob &
[1] 27823
$ ps l
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
1001 R 343 21938 27823 27 77 24 328e 56 20 pts/5 0:01 longjob
1001 R 343 26545 27823 26 77 24 601a 48 20 pts/5 0:01 longjob
201001 R 343 27823 27973 26 77 24 1647 56 20 pts/5 0:01 longjob
200801 S 343 27973 24078 0 60 20 6838 104 384 pts/5 0:00 -ksh
1001 R 343 28336 27823 26 77 24 7f1e 40 20 pts/5 0:01 longjob
200001 R 343 29877 27973 4 62 20 4ff2 108 204 pts/5 0:00 ps l
$ renice 20 -p 27823
27823: old priority 4, new priority 20
$ ps l
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
1001 R 343 21938 27823 24 76 24 328e 56 20 pts/5 0:04 longjob
1001 R 343 26545 27823 24 76 24 601a 48 20 pts/5 0:04 longjob
201001 R 343 27823 27973 11 85 40 1647 56 20 pts/5 0:04 longjob
200801 S 343 27973 24078 0 60 20 6838 104 384 pts/5 0:00 -ksh
1001 R 343 28336 27823 24 76 24 7f1e 40 20 pts/5 0:04 longjob
200001 R 343 29699 27973 4 62 20 4ff2 108 204 pts/5 0:00 ps l
$ renice 20 -g 27823
27823: old priority 4, new priority 20
$ ps l
F S UID PID PPID C PRI NI ADDR SZ RSS WCHAN TTY TIME CMD
1001 R 343 21938 27823 39 99 40 328e 56 20 pts/5 0:06 longjob
1001 R 343 26545 27823 38 99 40 601a 48 20 pts/5 0:06 longjob
201001 R 343 27823 27973 38 99 40 1647 56 20 pts/5 0:05 longjob
200801 S 343 27973 24078 0 60 20 6838 104 384 pts/5 0:00 -ksh
1001 R 343 28336 27823 38 99 40 7f1e 40 20 pts/5 0:06 longjob
200001 R 343 29719 27973 4 62 20 705d 108 204 pts/5 0:00 ps l
$ </PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I31" NAME="I31">
<FONT SIZE=4><B>Job Control and Process Groups</B>
<BR></FONT></A></CENTER></H3>
<P>Job control is a BSD UNIX addition that is used by some shells. Both C shell and Korn shell support job control. In order to support job control, these shells use the concept of process groups. Each time you enter a command or pipeline from the command
line, your shell creates a process group. The process group is simply the collection of all the processes that are executed as a result of that command. For simple commands, this could be a single process. For pipelines, the process group could contain
many processes. Either way, the shell keeps track of the processes as one unit by identifying a process group ID. This ID will be the PID of one of the processes in the group.
<BR></P>
<P>If you run a process group in the background or suspend its execution, it is referred to as a job. A small integer value, the job number, is associated with this process group. The shell prints out a message with these two identifiers at the time when
you perform the background operation. A process group and a job are almost the same thing. The one distinction you might care about is that every command line results in a process group (and therefore a process group identifier); a job identifier is
assigned only when a process group is suspended or put into the background.
<BR></P>
<P>Given process groups and job IDs, the shells have added new commands that operate on the job (or process group) as a whole. Further, existing commands (such as kill) are modified to take advantage of this concept. The two shells (C shell and Korn shell)
have very minor differences from one another, but for the most part the job control commands in each are the same.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I32" NAME="I32">
<FONT SIZE=3><B>Using the </B><B><I>jobs</I></B><B> Command</B>
<BR></FONT></A></CENTER></H4>
<P>The jobs command will show you the list of all of your shell's jobs that are either suspended or executing in the background. The list of jobs will look similar to this:
<BR></P>
<PRE>[1] Stopped vi mydoc.txt
[2] - Running fin_analysis < fin_database > fin_report &
[3] + Stopped (tty output) summararize_log &</PRE>
<P>Each line corresponds to a single process group, and the integer at the start is its job number. You can use the job number as an argument to the kill command by prefixing the job number with a percent (%) sign. To send a signal to the process vi
mydoc.txt, you could enter kill %1. Since you did not specify the signal you wanted to send to the process, the default signal, TERM, is sent. This notation is just a convenience for you since you can do the same thing via kill and the PID. The real power
of job control comes with the ability to manipulate jobs between the foreground and the background.
<BR></P>
<P>The shell also keeps the concept of current and previous jobs. On the output of the jobs command you will notice a + next to the current job and a - next to the previous job. If you have more than two jobs, the remaining jobs have no particular
distinction. Again, this notation is mainly a convenience for you. In some job control commands, if you do not specify a job (or PID) number, the current job is taken by default. Keep in mind that your current job is different from your foreground process
group. A job is either suspended or in the background.
<BR></P>
<P>The following are various ways to reference a job:
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>%n</P>
<TD>
<P>Where n is the job number reported by jobs</P>
<TR>
<TD>
<P>%+</P>
<TD>
<P>Your current job</P>
<TR>
<TD>
<P>%%</P>
<TD>
<P>Your current job</P>
<TR>
<TD>
<P>%-</P>
<TD>
<P>Your previous job</P>
<TR>
<TD>
<P>%string</P>
<TD>
<P>Job whose command line begins with string</P>
<TR>
<TD>
<P>%?string</P>
<TD>
<P>Job whose command line contains string</P></TABLE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I33" NAME="I33">
<FONT SIZE=3><B>Putting Jobs in the Foreground</B>
<BR></FONT></A></CENTER></H4>
<P>After executing a process group in the background, you may decide for some reason that you would like it to execute in the foreground. With non-job control shells, after executing a command line in the background (via the & symbol), it stays in the
background until it completes or is terminated (for example, if you send a terminate signal to it via kill). With a job control shell, the fg command will move the specified job into the foreground. The fg command will take either a job number preceded by
a percent (%) sign or a PID as an argument. If neither is given, the current job is taken as the default.
<BR></P>
<P>The result of the fg command is that the specified job executes as your foreground process. Remember that you can have only one foreground process at a time. To move the vi mydoc.txt job into the foreground, you could enter fg %1.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I34" NAME="I34">
<FONT SIZE=3><B>Suspending Process Groups</B>
<BR></FONT></A></CENTER></H4>
<P>To suspend an executing process group, you need to send a suspend signal to the process. There are two ways to do this: (1) use the suspend control character on your foreground process, or (2) send a suspend signal via the kill command.
<BR></P>
<P>The suspend control character, commonly Ctrl+Z, is used to send a suspend signal to the foreground process. Your shell may be configured with a different suspend control character, so be sure to find out your own configuration by running the stty -a
command. (Refer to the section "Working on the System" for information on control characters.) After you have executed a command in the foreground, you simply press Ctrl+Z (or whatever your suspend control character is) to suspend the running
process. The result is that the process is suspended from execution. When this happens, your shell prints a message giving the job number and process group ID for that job. You can subsequently use the fg or bg commands to manipulate this process.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I35" NAME="I35">
<FONT SIZE=3><B>Putting Jobs in the Background</B>
<BR></FONT></A></CENTER></H4>
<P>The bg command puts the specified jobs into the background and resumes their execution. The common way to use this command is following a suspend control character. After a job is put in the background, it will continue executing until it completes (or
attempts input or output from the terminal). You manipulate it via fg or kill.
<BR></P>
<P>An example may help you see the power of these commands when used together:
<BR></P>
<PRE>$ long_job\
^Z[1] + Stopped long_job
$ important_job 1
$ jobs
[1] + Stopped sleep 400
$ bg
[1] long_job&
$ important_job 2
$ kill -STOP %1
[1] + Stopped (signal) long_job
$ important_job 3
$ fg %1
long_job</PRE>
<P>If you don't have a long_job, try using sleep 100. If you don't have an important_job, try using echo. This example shows how you can use job control to move jobs between the foreground and background and suspend, then later resume, jobs that might be
taking computer resources that you need.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I36" NAME="I36">
<FONT SIZE=3><B>Using </B><B><I>wait</I></B><B> to Wait for Background Jobs</B>
<BR></FONT></A></CENTER></H4>
<P>The wait command built into most shells (including all the shells discussed in this book) will wait for completion of all background processes or a specific background process. Usually, wait is used in scripts, but occasionally you may want to use it
interactively to wait for a particularly important background job or to pause until all of your current background jobs complete so you will not load the system with your next job. The command wait will wait for all background jobs. The command wait
<I>pid</I> will wait for a particular PID. If you are using a job control shell, you can use a job identifier instead of a PID:
<BR></P>
<PRE>$ job1 &
[1] 20233
$ job2 &
[2] 20234
$ job3 &
[3] 20235
$ job4 %
[4] 20237
$ wait %1
$ wait 20234
$ wait
[4] + Done job4 &
[3] + Done job3 &
$ jobs
$</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I37" NAME="I37">
<FONT SIZE=3><B>Using </B><B><I>csh notify</I></B><B> to Learn About Changes Sooner</B>
<BR></FONT></A></CENTER></H4>
<P>Most interactive use of wait in csh can be replaced by notify. The notify command tells csh not to wait until issuing a new prompt before telling you about the completion of all or some background jobs. The command
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -