天天看点

Linux 进程相关

【进程状态】

R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态)

S (TASK_INTERRUPTIBLE),可中断的睡眠状态, 可处理signal

D (TASK_UNINTERRUPTIBLE),不可中断的睡眠状态, 可处理signal, 有延迟

T (TASK_STOPPED or TASK_TRACED),暂停状态或跟踪状态, 不可处理signal, 因为根本没有时间片运行代码

Z (TASK_DEAD - EXIT_ZOMBIE),退出状态,进程成为僵尸进程。不可被kill, 即不响应任务信号, 无法用SIGKILL杀死

man ps 相关内容:

<code>PROCESS STATE CODES</code>

<code>       </code><code>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:</code>

<code>               </code><code>D    uninterruptible sleep (usually IO)</code>

<code>               </code><code>R    running or runnable (on run queue)</code>

<code>               </code><code>S    interruptible sleep (waiting for an event to complete)</code>

<code>               </code><code>T    stopped by job control signal</code>

<code>               </code><code>t    stopped by debugger during the tracing</code>

<code>               </code><code>W    paging (not valid since the 2.6.xx kernel)</code>

<code>               </code><code>X    dead (should never be seen)</code>

<code>               </code><code>Z    defunct ("zombie") process, terminated but not reaped by its parent</code>

<code>       </code><code>For BSD formats and when the stat keyword is used, additional characters may be displayed:</code>

<code>               </code><code>&lt;    high-priority (not nice to other users)</code>

<code>               </code><code>N    low-priority (nice to other users)</code>

<code>               </code><code>L    has pages locked into memory (for real-time and custom IO)</code>

<code>               </code><code>s    is a session leader</code>

<code>               </code><code>l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)</code>

<code>               </code><code>+    is in the foreground process group</code>

【孤儿进程与僵尸进程】

1、在UNIX术语中,一个已经终止、但是其父进程尚未对其进行善后处理(获取终止子进程的有关信息、释放它仍占用的资源)的进程被称为僵尸进程。(APUE 第三版 第八章 进程控制)

2、由init收养的进程不会变成僵尸进程。(APUE 第三版 第八章 进程控制)

3、一个其父进程已终止的进程称为孤儿进程(orphan process), 这种进程由 init 进程“收养”。(APUE 第三版 第九章 进程关系)

  所以,孤儿进程是父进程已死自己还没死的进程;僵尸进程是自己已死,父进程没死,却没有对它做善后处理和安排的进程;孤儿进程由于会被 init 进程收养,最终不会变成僵尸进程。

  编程时避免僵尸进程的方法是使用 SIG_IGN 信号或 fork 两次。

【杀死 Z 进程】

  处理僵尸进程的方法是杀死其父进程,让僵尸进程被 init 进程领养善后。

【杀死 T 进程】

  T 进程一般是需要交互的后台进程,可以通过 fg 命令将其调到前台后处理。

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1659636如需转载请自行联系原作者

RQSLT

继续阅读