📄 384-386.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=384-386//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="381-384.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="386-388.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Linux identifies and keeps track of processes by assigning a process ID number (PID) to each process.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Starting Multiple Processes</FONT></H4>
<P>You’ve already seen that your login shell is always running. Whenever you enter a command, you start at least one new process while the login shell continues to run. If you enter the following command, for example, the file named report.txt is sent to the <TT>lp</TT> program:</P>
<!-- CODE SNIP //-->
<PRE>
lp report.txt
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>• <B>See</B> “Understanding Shells,” <B>p. 339</B><HR></FONT>
</BLOCKQUOTE>
<P>When the <TT>lp</TT> program completes its task, the shell prompt reappears. However, before the shell prompt reappeared, the login shell and the <TT>lp</TT> command were running; you have initiated multiple processes in that case. The shell waited until the <TT>lp</TT> command finished before putting the shell prompt back onscreen.</P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Starting a Background Process</FONT></H4>
<P>You can run a process as a background job by giving the command to start a process and placing an ampersand (&) after the command. For example, if you enter the command <TT><B>lp report.txt &</B></TT>, the shell responds immediately with a number—the PID for that process. The shell prompt reappears without waiting for the process to complete. The following is a sample of what you would see:</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>lp report.txt &</B>
3146
$
</PRE>
<!-- END CODE SNIP //-->
<P>In this example, 3146 is the PID of the process started by the <TT>lp</TT> command.</P>
<P>Regardless of whether you run the <TT>lp</TT> command in the background, the process associated with <TT>lp</TT> is started from the current shell. The <TT>lp</TT> process is a child process of the current shell. This example points to a common relationship between processes—that of parent and child. Your current shell is the parent process, and the running <TT>lp</TT> process is a child process. Usually, a parent process waits for one or more of its child processes to complete before it continues. If you want the parent to continue without waiting for the child to finish, attach the ampersand (&) to the command that <I>spawns</I>, or initiates, the child process. You can continue with other work or commands while the child runs.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>If you’re working from a character terminal or a remote login, your current shell is usually your login shell. However, if you’re using a virtual terminal or a terminal window from a GUI, a separate shell is associated with each session.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Using Pipes to Start Multiple Processes</FONT></H4>
<P>Another way to start multiple processes is to use one or more pipes on a command line. To print a long listing of the 10 most recently modified files in your current directory, enter this command:
</P>
<!-- CODE SNIP //-->
<PRE>
<B>ls -lt | head | lp</B>
</PRE>
<!-- END CODE SNIP //-->
<P>This command starts three processes simultaneously, and they’re all children of the current shell. A pipe works this way: Commands on either side of the vertical bar (|) begin at the same time. Neither is the parent of the other; they’re both children of the process that was running when they were created. In this sense, you can think of commands on either side of the pipe symbol as sibling processes.
</P>
<P>Some programs are written so that they themselves spawn several processes. One example is the <TT>ispell</TT> command, which lists the words in a document that Linux can’t find in a system dictionary. The <TT>ispell</TT> command spawns some child processes. Suppose you enter this:</P>
<!-- CODE SNIP //-->
<PRE>
<B>ispell final.rept > final.errs &</B>
</PRE>
<!-- END CODE SNIP //-->
<P>You’ll see the following results displayed:
</P>
<!-- CODE SNIP //-->
<PRE>
1286
$
</PRE>
<!-- END CODE SNIP //-->
<P>Here, 1286 is the PID of the <TT>ispell</TT> process; the <TT>$</TT> prompt indicates that the shell is ready to handle another command from you. Even though <TT>ispell</TT> may spawn some children and wait for them to complete, you don’t have to wait. In this example, the current shell is the parent of <TT>ispell</TT>, and <TT>ispell</TT>’s children can be thought of as grandchildren of the login shell. Although a parent can wait for its children, a grandparent doesn’t.</P>
<P>All these examples show how it’s possible for users to start multiple processes. You can wait until child processes are finished before continuing or not. If you continue without waiting for child processes to complete, you make the children background processes. The following section looks at some Linux commands you can use to schedule processes to run at specified times or at a lower relative priority.</P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Using the Scheduling Commands</FONT></H3>
<P>The Linux environment provides many ways to handle command execution. Linux lets you create lists of commands and specify when they’re to be run. The <TT>at</TT> command, for example, takes a list of commands typed at the keyboard or from a file and runs them at the time specified by the command. The <TT>batch</TT> command is similar to the <TT>at</TT> command, except that <TT>batch</TT> runs commands when the system finds time for them rather than allow the user to specify a particular time. The <TT>cron</TT> command allows for commands to be run periodically, and the <TT>crontab</TT> command allows users to edit the files used by <TT>cron</TT>.</P>
<P>All scheduling commands are useful for running tasks at times when the system isn’t too busy. They’re also good for executing scripts to external services—such as database queries—at times when it’s least expensive to do so.</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Running Commands at Specified Times with <I>at</I>
</FONT></H4>
<P>To schedule one or more commands for a specified time, use the <TT>at</TT> command. With this command, you can specify a time, a date, or both. The command expects two or more arguments. At a minimum, you specify the time you want the command(s) executed and the command(s) you want to execute.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="381-384.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="386-388.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 + -