📄 609-612.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Processes</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=34//-->
<!--PAGES=609-612//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch33/607-608.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="612-615.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 34<BR>Processes
</FONT></H2>
<P><I>by Tim Parker</I></P>
<DL>
<DT><B>In This Chapter</B>
<DT>• What you need to know about processes
<DT>• Using the ps command
<DT>• Using kill
</DL>
<P>Everything that runs on a Linux system is a process—every user task, every system daemon—everything is a process. Knowing how to manage the processes running on your Linux system is an important (indeed even critical) aspect of system administration. This chapter looks at processes in some detail. In the course of discussing processes, we won’t bother with the mechanics behind how processes are allocated or how the Linux kernel manages to time-slice all the processes to run a multitasking operating system. Instead, we’ll look at the nitty-gritty aspects of process control that you need in order to keep your system running smoothly.
</P>
<P>You may come across the terms <I>process</I> and <I>job</I> used when dealing with multitasking operating systems. For most purposes, both terms are correct. However, a <I>job</I> is usually a process started by a shell (and may involve many processes), while a <I>process</I> is a single entity that is executing. To be correct, we’ll use the term process throughout.</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">What You Need to Know About Processes</FONT></H3>
<P>A formal definition of a process is that it is a single program running in its own virtual address space. This means that everything running under Linux is a process. This is compared to a job, which may involve several commands executing in a series. Alternatively, a single command line issued at the shell prompt may involve more than one process, especially when pipes or redirection is involved. For example, the following command will start three processes, one for each command:
</P>
<!-- CODE SNIP //-->
<PRE>
nroff -man ps.1 | grep kill | more
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Types of Processes</FONT></H4>
<P>There are several types of processes involved with the Linux operating system. Each has its own special features and attributes. The processes involved with Linux are as follows:
</P>
<DL>
<DD><B>•</B> Interactive processes: A process initiated from (and controlled by) a shell. Interactive processes may be in foreground or background.
<DD><B>•</B> Batch processes: Processes that are not associated with a terminal but are submitted to a queue to be executed sequentially.
<DD><B>•</B> Daemon processes: Processes usually initiated when Linux boots and that run in the background until required.
</DL>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Using the ps Command</FONT></H3>
<P>The easiest method of finding out which processes are running on your system is to use the <TT>ps</TT> (process status) command. The <TT>ps</TT> command has a number of options and arguments, although most system administrators use only a couple of common command line formats. We can start by looking at the basic usage of the <TT>ps</TT> command, and then examine some of the useful options.</P>
<P>The <TT>ps</TT> command is available to all system users, as well as <TT>root</TT>, although the output changes a little depending on whether you are logged in as <TT>root</TT> when you issue the command.</P>
<P>When you are logged in as a normal system user (in other words, any login but <TT>root</TT>) and issue the <TT>ps</TT> command on the command line by itself, it displays information about every process you are running. For example, you might see the following output when you issue the command:</P>
<!-- CODE SNIP //-->
<PRE>
$ ps
PID TTY STAT TIME COMMAND
41 v01 S 0:00 -bash
134 v01 R 0:00 ps
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">ps Command Output</FONT></H4>
<P>The output of the <TT>ps</TT> command is always organized in columns. The first column is labeled PID, which means “Process ID” number. Every process on the system has to have a unique identifier so Linux can tell which processes it is working with. Linux handles processes by assigning a unique number to each process, called the process ID number (or PID). PIDs start at zero when the system is booted and increment by one for each process run, up to some system-determined number (such as 65,564) at which point it starts numbering from zero again, ignoring those that are still active. Usually, the lowest number processes are the system kernel and daemons, which start when Linux boots and remain active as long as Linux is running. When you are working with processes (such as terminating them), you must use the PID.</P>
<P>The <TT>TTY</TT> column in the <TT>ps</TT> command output shows you which terminal the process was started from. If you are logged in as a user, this will usually be your terminal or console window. If you are running on multiple console windows, you will see all the processes you started in every window displayed.</P>
<P>The <TT>STAT</TT> column in the <TT>ps</TT> command output shows you the current status of the process. The two most common entries in the status column are <TT>S</TT> for “sleeping” and <TT>R</TT> for “running.” A <I>running</I> process is one that is currently executing on the CPU. A <I>sleeping</I> process is one that is not currently active. Processes may switch between sleeping and running many times every second.</P>
<P>The <TT>TIME</TT> column shows the total amount of system (CPU) time used by the process so far. These numbers tend to be very small for most processes because they require only a short time to complete. The numbers under the TIME column are a total of the CPU time, not the amount of time the process has been alive.</P>
<P>Finally, the <TT>COMMAND</TT> column contains the name of the command line you are running. This is usually the command line you used, although some commands start up other processes. These are called “child” processes, and they show up in the <TT>ps</TT> output as if you had entered them as commands.</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Login Shells</FONT></H4>
<P>As a general convention, a login shell has a hyphen placed before its name (such as <TT>-bash</TT> in the preceding output) to help you distinguish the startup shell from any shells you may have started afterward. Any other shells that appear in the output do not have the hyphen in front of the name, as the following example shows:</P>
<!-- CODE SNIP //-->
<PRE>
$ ps
PID TTY STAT TIME COMMAND
46 v01 S 0:01 -bash
75 v01 S 0:00 pdksh
96 v01 R 0:00 bash
123 v01 R 0:00 ps
</PRE>
<!-- END CODE SNIP //-->
<P>This output shows that the user’s startup shell is <TT>bash</TT> (PID 46), and that he or she started up the Korn shell (<TT>pdksh</TT>, PID 75) and another Bourne shell (<TT>bash</TT>, PID 96) afterward.</P>
<P>Notice in the preceding outputs that the command that actually shows you the process status, <TT>ps</TT>, appears on the output because it was running when you issued the command. The <TT>ps</TT> command always appears on the output.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch33/607-608.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="612-615.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 + -