如何在Linux中显示进程状态(阻塞,非阻塞)

6
有没有一种方法可以查询Linux进程表中进程的状态,以便在执行查询时演示进程是否正在运行或被阻塞?我的目标是从进程或程序的“外部”完成这个操作,因为我希望从操作系统进程的角度来理解这个问题,但任何想法都受欢迎!
以下是Python代码,用于阻止进程:
import subprocess
proc = subprocess.call('ls -lRa /', shell=True)

以下是非阻塞进程的Python代码:

import subprocess
proc = subprocess.Popen('ls -lRa /', shell=True)

以下是“ps -ef”命令输出的进程ID列表:
UID        PID  PPID  C STIME TTY          TIME CMD
user1    14308  4145  0 15:30 pts/2    00:00:00 python call_b.py
user1    14309 14308  0 15:30 pts/2    00:00:00 /bin/sh -c ls -lRa /
user1    14310 14309 15 15:30 pts/2    00:00:30 ls -lRa /
root     14313     2  0 15:31 ?        00:00:00 [kworker/2:0]
user1    14318  2476  0 15:32 pts/4    00:00:00 -bash
user1    14442     1  0 15:33 pts/4    00:00:00 /bin/sh -c ls -lRa /
user1    14443 14442  6 15:33 pts/4    00:00:01 ls -lRa /

当这些'ls'命令正在处理时,我想展示哪些进程正在阻塞,以及其他进程的状态。这个问题旨在成为一个工具,帮助我学习使用Python进行多进程编程时了解状态,因此我认为PID 14309是阻塞的,PID 14442是非阻塞的,尽管我可能错了。这就是为什么能够查看或测试所有显示的PID对我很有帮助。
感谢受人尊敬的用户“ubuntu”及其对此的回复: Blocking and Non Blocking subprocess calls 提供了起始代码。
在这种情况下,操作系统是Ubuntu,但任何debian或操作系统的注释都将有所帮助。
4个回答

9
尝试使用ps -weo pid,stat,wchan:32,args 命令。你将得到以下输出:
28325 S<l  poll_schedule_timeout            /usr/bin/pulseaudio --start --log-target=syslog
28328 Sl   poll_schedule_timeout            /usr/bin/krunner
28344 Sl   poll_schedule_timeout            /usr/bin/kmix -session 014f10adfdf000141320876500000291010026_1419380700_54458

这是进程的pid、状态标志(见下文)、进程当前所阻塞的位置和命令行。标志如下:

       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

       <    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

太棒了,这正是我所寻找的。我在下面添加了另一部分,因为我认为它有助于理解。 - apollon

1

使用 ps -elf 命令可以输出进程状态。在Centos 6.5中,第二列是进程状态:

F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 -  4808 poll_s 09:50 ?        00:00:01 /sbin/init
1 S root         2     0  0  80   0 -     0 kthrea 09:50 ?        00:00:00 [kthreadd]
1 S root         3     2  0 -40   - -     0 migrat 09:50 ?        00:00:00 [migration/0]
1 S root         4     2  0  80   0 -     0 ksofti 09:50 ?        00:00:00 [ksoftirqd/0]
1 S root         5     2  0 -40   - -     0 cpu_st 09:50 ?        00:00:00 [migration/0]

0

这可能不是您要寻找的确切答案,但您可以尝试使用ps aux命令,它将为您提供STAT列。这不会明确地说明阻塞或非阻塞,但某些状态将为您提供有关进程状态的更多信息。例如,它是否正在运行或处于僵尸状态(即将被回收)。


0

在短程序中,判断进程是否阻塞的最明显的关键是它是否存在于进程表中:

这清楚地展示了我所寻找的内容,特别是通过“等待”状态和非阻塞程序已经终止而子进程仍在运行的缺乏存在感。

blocking:
  PID STAT WCHAN                            COMMAND
18714 S+   wait                             python call_b.py       <=see blocking process
18715 S+   wait                             /bin/sh -c ls -lRa /
18716 D+   sleep_on_buffer                  ls -lRa /

注意命令“python popen_nb.py”缺少PID,因为它已经完成。但是,我打开的两个终端窗口都在从我的驱动器上进行ls操作并输出结果...

non-blocking:
  PID STAT WCHAN                            COMMAND
18869 S    wait                             /bin/sh -c ls -lRa /
18870 D    vfs_readdir                      ls -lRa /

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接