📄 397-400.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Managing Multiple Processes</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=19//-->
<!--PAGES=397-400//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="394-397.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="400-402.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>In this example, the <TT>-t</TT> option is used to restrict the listing to the processes associated with terminals tty01 and tty02. Terminal tty02 is running the shell command (PID 32) and using <TT>vi</TT> to edit the calendar (PID 235). The cumulative time for each process is also listed. If you’re using shells from a graphical interface (the <TT>xterm</TT> command), use device names pts001, pts002, and so on with the <TT>-t</TT> option to see the processes from those sessions.</P>
<P>Sometimes a process is marked as <TT><defunct></TT>, which means that the process has terminated and its parent process has been notified, but the parent hasn’t acknowledged that the process is “dead.” A process like that is called a <I>zombie process</I>. It’s possible that the parent is busy with something else and the zombie will soon disappear. If you see a number of defunct processes or ones that linger for some time, this is a sign of some difficulty with the operating system.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Because a zombie process has no parent, you can’t kill the zombie. The only way to get rid of a zombie process is to reboot your machine.<HR></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Controlling Multiple Processes</FONT></H3>
<P>Linux gives you the power to run several processes concurrently. It also allows a user or an administrator to have control over running processes. This control is advantageous when you need to do the following:
</P>
<DL>
<DD><B>•</B> Initiate a process that continues after its parent quits running (use the <TT>nohup</TT> command)
<DD><B>•</B> Schedule a process with a priority different than other processes (use the <TT>nice</TT> command)
<DD><B>•</B> Terminate or stop a process (use the <TT>kill</TT> command)
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Using <I>nohup</I> with Background Processes
</FONT></H4>
<P>Normally, the children of a process terminate when the parent dies or terminates. This means that when you start a background process, it terminates when you log out. To have a process continue after you log out, use the <TT>nohup</TT> command. Put <TT>nohup</TT> at the beginning of a command line:</P>
<!-- CODE SNIP //-->
<PRE>
nohup sort sales.dat &
</PRE>
<!-- END CODE SNIP //-->
<P>This sample command tells the <TT>sort</TT> command to ignore the fact that you log out of the system; it should run until the process completes. In this way, you can initiate a process that can run for days or even weeks. What’s more, you don’t have to be logged in as it runs. Naturally, you want to make sure that the job you initiate behaves nicely—that is, it eventually terminates and doesn’t create an excessive amount of output.</P>
<P>When you use <TT>nohup</TT>, the command sends all the output and error messages of a command that normally appear onscreen to a file named nohup.out. Consider the following example:</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>nohup sort sales.dat &</B>
1252
Sending output to nohup.out
$
</PRE>
<!-- END CODE SNIP //-->
<P>The sorted file and any error messages are placed in the file nohup.out. Now consider this example:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>nohup sort sales.dat > sales.srt &</B>
1257
Sending output to nohup.out
$
</PRE>
<!-- END CODE SNIP //-->
<P>Any error messages are placed in the nohup.out file, but the sorted sales.dat file is placed in sales.srt.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>When you use <TT>nohup</TT> with a pipeline, you must use <TT>nohup</TT> with each command in the pipeline:
<!-- CODE SNIP //-->
<PRE>
nohup sort sales.dat | nohup mailx -s“Sorted Sales Data” boss &
</PRE>
<!-- END CODE SNIP //-->
<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Scheduling the Priority of Commands with <I>nice</I>
</FONT></H4>
<P>Use the <TT>nice</TT> command to run a command at a specific scheduling priority. The <TT>nice</TT> command gives you some control over the priority of one job over another. If you don’t use <TT>nice</TT>, processes run at a set priority. You can lower the priority of a process with the <TT>nice</TT> command so that other processes can be scheduled to use the CPU more frequently than the <TT>nice</TT> job. The superuser (the person who can log in as the root user) can also raise the priority of a process.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>The commands <TT>nice –help</TT> and <TT>nice –version</TT> don’t work in the GNU implementation of <TT>nice</TT>.<HR></FONT>
</BLOCKQUOTE>
<P>The general form of the <TT>nice</TT> command is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
nice -<I>number command</I>
</PRE>
<!-- END CODE SNIP //-->
<P>The priority level is determined by the <I>number</I> argument (a higher number means a lower priority). The default is set to 10, and <I>number</I> is an offset to the default. If the <I>number</I> argument is present, the priority is incremented by that amount up to a limit of 20. If you enter the following command, the <TT>sort</TT> process starts with a priority of 10:</P>
<!-- CODE SNIP //-->
<PRE>
sort sales.dat > sales.srt &
</PRE>
<!-- END CODE SNIP //-->
<P>If you want to start another process—say, with the <TT>lp</TT> command—but give preference to the <TT>sort</TT> command, you can enter the following:</P>
<!-- CODE SNIP //-->
<PRE>
<B>nice -5 lp mail_list &</B>
</PRE>
<!-- END CODE SNIP //-->
<P>To give the <TT>lp</TT> command the lowest possible priority, enter this:</P>
<!-- CODE SNIP //-->
<PRE>
<B>nice -10 lp mail_list &</B>
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>The number flag above is preceded by the flag specifier -, which you shouldn’t confuse with the negative number sign.<HR></FONT>
</BLOCKQUOTE>
<P>Only superusers can increase the priority of a process. To do that, they use a negative number as the argument to <TT>nice</TT>. Remember—the lower the <TT>nice</TT> value, the higher the priority (up to a maximum priority of 20). To give a job “top priority,” a superuser initiates the job as follows:</P>
<!-- CODE SNIP //-->
<PRE>
nice –10 job &
</PRE>
<!-- END CODE SNIP //-->
<P>The ampersand (&) is optional; if job is interactive, you wouldn’t use the ampersand to place the process in the background.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Scheduling the Priority of Running Processes with <I>renice</I>
</FONT></H4>
<P>The <TT>renice</TT> command, available on some systems, allows you to modify the priority of a running process. Berkeley UNIX systems have the <TT>renice</TT> command; it’s also available in the /usr/ucb directory in Linux System V systems for compatibility with Berkeley systems. With <TT>renice</TT>, you can adjust priorities on commands as they execute. The format of <TT>renice</TT> is similar to that of <TT>nice</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
renice -<I>number PID</I>
</PRE>
<!-- END CODE SNIP //-->
<P>To change the priority on a running process, you must know its PID. To find the PID of all your processes, enter this command:
</P>
<!-- CODE SNIP //-->
<PRE>
<B>ps -e | grep</B> name
</PRE>
<!-- END CODE SNIP //-->
<P>In this command, <I>name</I> represents the name of the running process. The <TT>grep</TT> command filters out all processes that don’t contain the name of the process you’re looking for. If several processes of that name are running, you have to determine the one you want by looking at the time it started. If you want to affect all processes belonging to a certain group or a certain user, you can specify the GID or UID of the running processes to the <TT>renice</TT> command.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="394-397.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="400-402.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -