您的位置:首页 > 运维架构 > Linux

linux上进程状态查询

2012-04-27 16:19 260 查看
From: http://www.cnblogs.com/dkblog/archive/2011/03/11/1980556.html
linux上进程有5种状态:

1. 运行(正在运行或在运行队列中等待)

2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)

3. 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生)

4. 僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放)

5. 停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行)

ps工具标识进程的5种状态码:

D 不可中断 uninterruptible sleep (usually IO)

R 运行 runnable (on run queue)

S 中断 sleeping

T 停止 traced or stopped

Z 僵死 a defunct ("zombie") process

注: 其它状态还包括W(无驻留页), <(高优先级进程), N(低优先级进程), L(内存锁页).

可以用下面命令查看进程状态

ps -aux

列出类似程序树的程序显示(显示进程下有哪些子进程)

ps -axjf

找出与 cron 与 syslog 这两个服务有关的 PID 号码

ps aux | egrep '(cron|syslog)'


也可以这样使用ps格式输出来查看进程状态:

ps -eo user,stat..,cmd

user 用户名

uid 用户号

pid 进程号

ppid 父进程号

size 内存大小, Kbytes字节.

vsize 总虚拟内存大小, bytes字节(包含code+data+stack)

share 总共享页数

nice 进程优先级(缺省为0, 最大为-20)

priority(pri) 内核调度优先级

pmem 进程分享的物理内存数的百分比

trs 程序执行代码驻留大小

rss 进程使用的总物理内存数, Kbytes字节

time 进程执行起到现在总的CPU暂用时间

stat 进程状态

cmd(args) 执行命令的简单格式

例子:

查看当前系统进程的uid,pid,stat,pri, 以uid号排序.

ps -eo pid,stat,pri,uid --sort uid

查看当前系统进程的user,pid,stat,rss,args, 以rss排序.

ps -eo user,pid,stat,rss,args --sort rss

在Linux下,还有一种方法检查某个进程是否存在:利用/proc文件系统. /proc/pid/stat里面有进程的状态,进程可执行文件名等.如果该文件不存在了,那进程肯定退出了.如果存在,可以检查状态和文件名是否正确.效率可能比PS还是高一些,因为/proc是虚拟文件系统,存在与内存中.

如何利用/proc文件系统

cat /proc/pid/status

这里pid是你的进程ID,看看输出结果,有一栏是State

你要利用/proc文件系统时,int fd = open( "/proc/pid/status", O_RDONLY );

这里pid是实际的进程的pid,如果open失败,刚进程显然不存在,然后读取该文件的内容,找出State

------------------------------------------------------------------------------------------------------------------------------

From: http://blog.chinaunix.net/space.php?uid=25085157&do=blog&id=332473
附:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息