redhat server 2.6.x进程学习笔记

概要:进程概念;ps,pstree,top,kill,进程状态;man ps;

[root@server1 /]#
[root@server1 /]# ps
  PID TTY          TIME CMD
4973 pts/0    00:00:00 bash
6326 pts/0    00:00:00 man
6329 pts/0    00:00:00 sh
6330 pts/0    00:00:00 sh
6335 pts/0    00:00:00 less
6381 pts/0    00:00:00 ps
[root@server1 /]# man ps

ps - report a snapshot of the current processes.

ps [options]

DESCRIPTION
ps displays information about a selection of the active processes. If you want a repetitive update of theselection and the displayed information, use top(1) instead.

This version of ps accepts several kinds of options:
1   UNIX-style options, which may be grouped and must be preceded by a dash.

Technorati 标签:


2   BSD-style options, which may be grouped and must not be used with a dash.
GNU-style long options, which are preceded by two dashes.

 

By default, ps selects all processes with the same effective user ID (euid=EUID) as the current user and associated with the same terminal as the invoker(非故意的; 偶然的). It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated(累积) CPU time in [dd-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). Output is unsorted by default.

To see every process on the system using standard syntax:
  ps -e
   ps -ef
   ps -eF
   ps -ely

To see every process on the system using BSD syntax:
   ps ax
   ps axu

To print a process tree:
   ps -ejH
   ps axjf

To get info about threads:
   ps -eLf
   ps axms

To get security info:
   ps -eo euser,ruser,suser,fuser,f,comm,label
   ps axZ
   ps –eM

To see every process running as root (real & effective ID) in user format:
   ps -U root -u root u

This ps works by reading the virtual files in /proc. This ps does not need to be setuid kmem or have any privileges to run. Do not give this ps any special permissions.

This ps needs access to namelist data for proper WCHAN display. For kernels prior to 2.6, the System.map file must be installed.

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is
unlikely to add up to exactly 100%.

PROCESS STATE CODES
Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display
to describe the state of a process.
D    Uninterruptible sleep (usually IO)
R    Running or runnable (on run queue)
S    Interruptible sleep (waiting for an event to complete)
T    Stopped, either by a job control signal or because it is being traced.
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    Defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:
<    high-priority (not nice to other users)
N    low-priority (nice to other users)
L    has pages locked into memory (for real-time and custom IO)
s    is a session leader
l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+    is in the foreground process group

SEE ALSO
top(1), pgrep(1), pstree(1), proc(5).

[root@server1 /]#
[root@server1 /]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1   2036   640 ?        Ss   Dec04   0:00 init [5]                            
root         2  0.0  0.0      0     0 ?        S    Dec04   0:00 [migration/0]
root         3  0.0  0.0      0     0 ?       SN (?)  Dec04   0:00 [ksoftirqd/0]
…….
root      3425  0.0  0.1   1628   440 tty3     Ss+ (?) Dec04   0:00 /sbin/mingetty tty3
root      3428  0.0  0.1   1624   432 tty4     Ss+  Dec04   0:00 /sbin/mingetty tty4
root      3431  0.0  0.1   1624   436 tty5     Ss+  Dec04   0:00 /sbin/mingetty tty5
root      3432  0.0  0.1   1628   440 tty6     Ss+  Dec04   0:00 /sbin/mingetty tty6
…….

root      6523  0.0  0.1   4128   804 pts/0    T    00:38   0:00 /usr/bin/less -is
root      6827  0.0  0.2   4344  1060 pts/0    R+(?)   00:48   0:00 ps aux

[root@server1 /]# ps -A
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    2 ?        00:00:00 migration/0
    3 ?        00:00:00 ksoftirqd/0
……

6523 pts/0    00:00:00 less
6898 pts/0    00:00:00 ps
[root@server1 /]#

[root@server1 /]# top

top - 00:52:32 up  1:23,  2 users,  load average: 0.00, 0.01, 0.00
Tasks: 121 total,   1 running, 108 sleeping,  12 stopped,   0 zombie(?)
Cpu(s):  8.6%us,  7.3%sy,  0.0%ni, 84.1%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    409944k total,   402212k used,     7732k free,    20192k buffers
Swap:  1044184k total,        0k used,  1044184k free,   270036k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                        
3885 root      15   0 23152  10m 5268 S 10.0  2.6   0:32.94 Xorg                                                           
4956 root      15   0 40876  13m 9180 S  4.3  3.3   0:08.54 gnome-terminal                                                  
   

….

[root@server1 /]# pstree
init─┬─NetworkManager───{NetworkManager}
     ├─NetworkManagerD
     ├─acpid
     ├─atd
     ├─auditd─┬─python
     │        └─{auditd}
     ├─automount───4*[{automount}]
     ├─avahi-daemon───avahi-daemon
     ├─bonobo-activati───{bonobo-activati}
     ├─bt-applet
     ├─clock-applet
     ├─crond

[root@server1 /]# man kill

kill - terminate a process

       kill [ -s signal | -p ] [ -a ] [ -- ] pid ...
       kill -l [ signal ]

      The  command  kill sends the specified signal to the specified process or process group.  If no signal is
       specified, the TERM signal is sent.  The TERM signal will kill processes which do not catch this  signal.
       For  other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

       Most modern shells have a builtin kill function, with a usage rather  similar  to  that  of  the  command
       described here. The ‘-a’ and ‘-p’ options, and the possibility to specify pids by command name is a local
       extension.

-l     Print a list of signal names.  These are found in /usr/include/linux/signal.h

[root@server1 /]# kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
[root@server1 /]#