⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx19.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
the arguments.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B>  The BSD ps command predates standard UNIX option processing. It does not take hyphens to introduce options. On systems where one ps acts like either SYSV or BSD (e.g., AIX ps), the absence of the 
hyphen is what makes it run in BSD mode.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Look at what happens when you enter ps l:

<BR></P>

<PRE>$ ps l

       F UID   PID  PPID CP PRI NI  SZ  RSS WCHAN    STAT TT  TIME COMMAND

20408020 343 22711 22631  0  25  0  48    0          TW   c0  0:00 rlogin brat

    8000 343 22712 22711  0   1  0  48    0 socket   TW   c0  0:00 rlogin brat

20000001 343 23122 22631 19  29  0 200  400          R    c0  0:00 ps l

$ </PRE>

<P>The F field gives a series of flags that tell you about the current state of the process. Check your system manuals for information on interpreting this field. The UID field tells the user ID that owns the process. Your login name should appear here. 
The PPID field tells the process identifier of the parent of the process; notice that the PPID of the second rlogin is the same as the PID of the other, its parent process. The CP is process utilization information used by the scheduler. The PRI field is 
the priority of the process; a lower number means more priority to the scheduler. See the section &quot;Prioritizing Processes&quot; for more information on the scheduler. The SZ field shows the process size. The RSS field shows the resident set size, 
which is the actual amount of computer memory occupied by the process. The WCHAN field tells what event, if any, the process is waiting for. Interpretation of the WCHAN field is specific to your system.

<BR></P>

<P>Look at what happens when you enter ps u:

<BR></P>

<PRE>$ ps u

USER       PID %CPU %MEM   SZ  RSS TT STAT START  TIME COMMAND

sartin   23127  0.0  1.6  200  416 c0 R    19:25   0:00 ps u

sartin   22712  0.0  0.0   48    0 c0 TW   18:40   0:00 rlogin brat

sartin   22711  0.0  0.0   48    0 c0 TW   18:40   0:00 rlogin brat

$</PRE>

<P>The %CPU and %MEM fields tell the percentage of CPU time and system memory the process is using. The START field tells when the process started.

<BR></P>

<PRE>$ ps v

  PID TT STAT  TIME SL RE PAGEIN SIZE  RSS   LIM %CPU %MEM COMMAND

23126 c0 R     0:00  0  0      0  200  420    xx  0.0  1.6 ps

$</PRE>

<P>The SL field tells how long the process has been sleeping, waiting for an event to occur. The RE field tells how long the process has been resident in memory. The PAGEIN field tells the number of disk input operations caused by the process, to read in 
pages that were not already resident in memory. The LIM field tells the soft limit on memory used.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I8" NAME="I8">

<FONT SIZE=3><B>Checking on Your Processes with </B><B><I>ps</I></B>

<BR></FONT></A></CENTER></H4>

<P>This section gives a few handy ways to examine the states of certain processes you might care about. Short examples are given using the SYSV and BSD versions of ps. 

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=3><B>Everything You Own</B>

<BR></FONT></A></CENTER></H5>

<P>Viewing all processes that you own can be useful in looking for jobs that you accidentally left running or to see everything you are doing so you can control it. On SYSV, you type ps -u <I>userid</I> to see everything owned by a particular user. Try ps 

-u $LOGNAME to see everything you own:

<BR></P>

<PRE>$ ps -u $LOGNAME

   PID TTY      TIME COMMAND

  18743 ttys0    0:01 ksh

  19250 ttys0    0:00 ps

$ </PRE>

<P>On BSD, the default is for ps to show everything you own:

<BR></P>

<PRE>$ ps l

       F UID   PID  PPID CP PRI NI  SZ  RSS WCHAN    STAT TT  TIME COMMAND

20088201 343   835   834  1  15  0  32  176 kernelma S    p0  0:00 -ksh TERM=vt

20000001 343   861   835 25  31  0 204  440          R    p0  0:00 ps l

20088001 343   857   856  0   3  0  32  344 Heapbase S    p1  0:00 -ksh HOME=/t

$ </PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=3><B>Specific Processes</B>

<BR></FONT></A></CENTER></H5>

<P>Looking at the current status of a particular process can be useful to track the progress (or lack thereof) of a single command you have running. On SYSV you type ps -p<I>PID</I> ...  to see a specific process:

<BR></P>

<PRE>$ ps -p19057

   PID TTY      TIME COMMAND

  19057 ttys3    0:00 ksh

$ </PRE>

<P>On BSD, if the last argument to ps is a number, it is used as a PID:

<BR></P>

<PRE>$ ps l22712

       F UID   PID  PPID CP PRI NI  SZ  RSS WCHAN    STAT TT  TIME COMMAND

    8000 343 22712 22711  0   1  0  48    0 socket   TW   c0  0:00 rlogin brat

$</PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I11" NAME="I11">

<FONT SIZE=3><B>Specific Process Groups</B>

<BR></FONT></A></CENTER></H5>

<P>Looking at the status of a process group (See the section &quot;Job Control and Process Groups.&quot;) can be useful in tracking a particular job you run. On SYSV you can use ps -g<I>PGID</I> to see a particular process group:

<BR></P>

<PRE>$ ps -lg19080

  F S   UID   PID  PPID  C PRI NI     ADDR   SZ    WCHAN TTY      TIME COMD

  1 S   343 19080 19057  0 158 24   710340   51   39f040 ttys3    0:58 fin_analysis

  1 S   343 19100 19080  0 168 24   71f2c0   87 7ffe6000 ttys3    2:16 fin_marketval

$ </PRE>

<P>On BSD, there is no standard way to see a particular process group, but the output of ps j gives much useful information:

<BR></P>

<PRE>$ ps j

 PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND

  834   835   835   835 p0   904   SOE   198  0:00 -ksh TERM=vt100 HOME=/u/sart

  835   880   880   835 p0   904   TWE   198  0:00 vi

  835   881   881   835 p0   904   TWE   198  0:00 vi t1.sh

  835   896   896   835 p0   904   IWE   198  0:00 ksh t2.sh _ /usr/local/bin/k

  896   897   896   835 p0   904   IWE   198  0:00 task_a

  896   898   896   835 p0   904   IWE   198  0:00 task_b

  835   904   904   835 p0   904    RE   198  0:00 ps j

$</PRE>

<P>Note the PGID field for PIDs 896&#151;898, which are all part of one shell script. Note the TPGID field, which is the same for all processes and identifies the current owner of the terminal.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I12" NAME="I12">

<FONT SIZE=3><B>Specific Terminal</B>

<BR></FONT></A></CENTER></H5>

<P>Looking at the status of a particular terminal can be a useful way to filter processes started from a particular login, either from a terminal or over the network. On SYSV use ps -t <I>termid</I> to see processes running from a particular terminal or 
pseudo terminal. (See your system documentation to determine the correct values for <I>termid</I>.)

<BR></P>

<PRE>$ ps -fts3

     UID   PID  PPID  C    STIME TTY      TIME COMMAND

    root 19056   136  0 19:21:00 ttys3    0:00 rlogind

  sartin 19080 19057  0 19:23:53 ttys3    1:01 fin_analysis

  sartin 19057 19056  0 19:21:01 ttys3    0:00 -ksh

  sartin 19100 19080  0 19:33:53 ttys3    3:43 fin_marketval

  sartin 19082 19057  0 19:23:58 ttys3    0:00 vi 19unxor.adj

$ </PRE>

<P>On BSD use ps t <I>termid</I> to see processes running from a particular terminal or pseudo terminal (See your system documentation to determine the correct values for <I>termid</I>.):

<BR></P>

<PRE>$ ps utp5

USER       PID  %CPU %MEM   SZ  RSS TT STAT  TIME COMMAND

sartin    2058   0.0  0.9  286      p5 R     0:00 -sh (sh)

sartin    2060   0.0  2.7   53      p5 R     0:00 vi 19unxor.adj

$ </PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I13" NAME="I13">

<FONT SIZE=3><B>Specific User</B>

<BR></FONT></A></CENTER></H5>

<P>Looking at processes run by a particular user can be useful for the system administrator to track what is being run by others and to deal with &quot;runaway&quot; processes. On SYSV enter ps -u <I>userid</I> to see everything owned by a particular user:



<BR></P>

<PRE>$ ps -fusartin

     UID   PID  PPID  C    STIME TTY      TIME COMMAND

  sartin 18743 18735  0  Mar 12  ttys0    0:31 collect_stats

  sartin 19065 19057  1 19:21:04 ttys3    0:00 vi 19unxor.adj

  sartin 19057 19056  0 19:21:01 ttys3    0:00 -ksh

  sartin 18735 18734  0  Mar 12  ttys0    0:00 -ksh

  sartin 19066 18743  8 19:21:12 ttys0    0:00 ps -fusartin

$ </PRE>

<P>On BSD, there is no simple, standard way to see processes owned by a particular user other than yourself.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I14" NAME="I14">

<FONT SIZE=3><B>Checking on a Process with </B><B><I>time</I></B>

<BR></FONT></A></CENTER></H4>

<P>The time command prints out the real, system, and user time spent by a command (in ksh, the built-in time command will time a pipeline as well). The real time is the amount of clock time it took from starting the command until it completed. This will 
include time spent waiting for input, output, or other events. The user time is the amount of CPU time used by the code of the process. The system time is the amount of time the UNIX kernel spent doing things for the process. The time command prints real, 

user, and sys times on separate lines (BSD time may print them all on one line). Both csh and ksh have built-in versions of time that have slightly different output formats. The csh built-in time command prints user time, system time, clock time, percent 
usage, and some I/O statistics all on one line. The ksh time built-in time command prints real, user, and sys time on separate lines, but uses a slightly different format for the times than does time:

<BR></P>

<PRE>% time ./doio

9.470u 0.160s 0:09.56 100.7% 0+99k 0+0io 0pf+0w

% ksh

$ time ./doio

real    0m9.73s

user    0m9.63s

sys     0m0.10s

$ sh

$ time ./doio

real        9.8

user        9.5

sys         0.1

$ </PRE>

<H3 ALIGN="CENTER">

<CENTER><A ID="I15" NAME="I15">

<FONT SIZE=4><B>Background and Foreground Processes</B>

<BR></FONT></A></CENTER></H3>

<P>So far, you have seen examples and descriptions of a user typing a command, watching as it executes, possibly interacting during its execution, and eventually completing. This is the default way your interactive shell executes processes. Using only this 

order of events means your shell executes a single process at a time. This single process is running in the foreground. Shells are able to keep track of more than one process at a time. In this type of environment, one process at most can be in the 
foreground; all the other processes are running in the backgound. This allows you to do multiple things at once from a single screen or window. You can think of the foreground and the background as two separate places where your interactive shell keeps 
processes. The foreground holds a single process, and you may interact with this process. The background holds many processes, but you cannot interact with these processes.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I16" NAME="I16">

<FONT SIZE=3><B>Foreground Processing</B>

<BR></FONT></A></CENTER></H4>

<P>Running a process in the foreground is very common&#151;it is the default way your shell executes a process. If you want to write a letter using the vi editor, you enter the command vi <I>letter</I> and type away. After you enter the vi command, your 
shell starts the vi process in the foreground so you can write your letter. In order for you to enter information interactively, your process must be in the foreground. When you exit the editor, you are terminating the process. After your foreground 
process terminates, but not before, the shell prompts you for the next command.

<BR></P>

<P>This mode of execution is necessary for all processes that need your interactions. It would be impossible for the computer to write the letter you want without your input. Mind reading is not currently a means of input, so you commonly type, use your 
mouse, and even sometimes speak the words. But not all processes need your input&#151;they are designed to be able to get all the necessary input via other ways. They may be designed to get input from the computer system, from other processes, or from the 

file system.

<BR></P>

<P>Still, such processes may be designed to give you information. Status information could be reported periodically, and usually the process results are displayed at a certain point. If you wish to see this information as it is reported, the process must 
be running in the foreground.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I17" NAME="I17">

<FONT SIZE=3><B>Where Is the Background and Why Should You Go There?</B>

<BR></FONT></A></CENTER></H4>

<P>Sometimes a program you run doesn't need you to enter any information or view any results. If this is the case, there is no reason you need to wait for it to complete before doing something else. UNIX shells provide a way for you to execute more than 
one process at a time from a single terminal. The way you do this is to run one or more processes in the background. The background is where your shell keeps all processes other than the one you are interacting with (your foreground process). You cannot 
give input to a process via standard input while it is in the background&#151;you can give input via standard input only to a process in the foreground.

<BR></P>

<P>The most common reason to put a process in the background is to allow you to do something else interactively without waiting for the process to complete. For example, you may need to run a calculation program that goes through a very large database, 
computing a complicated financial analysis of your data and then printing a report; this may take several minutes (or hours). You don't need to input any data because your database has all the necessary information. You don't need to see the report on your 

screen since it is so big you would rather have it saved in a file and/or printed on your laser printer. So when you execute this program, you specify that the input should come from your database (redirection of standard input) and the report should be 
sent to a file (redirection of standard output). At the end of the command you add the special background symbol, &amp;. This symbol tells your shell to execute the given command in the background. Refer to the following example scenario.

<BR></P>

<PRE>$ fin_analysis &lt; fin_database &gt; fin_report &amp;

[1]   123

$ date

Sat Mar 12 13:25:17 CST 1994

$ tetris

$ date

Sat Mar 12 15:44:21 CST 1994

[1] +  Done             fin_analysis &lt; fin_database &gt; fin_report &amp;

$ </PRE>

<P>After starting your program on its way (in the background), the shell prints a prompt and awaits your next command. You may continue doing work (executing commands) while the calculation program runs in the background. When the background process 
terminates (all your calculations are complete), your shell may print a termination message on your screen, followed by a prompt.

<BR></P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -