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

apo*_*lon 4 python linux process nonblocking blocking

有没有办法在 Linux 进程表中查询进程的状态,以便能够证明在执行查询时进程是正在运行还是被阻塞?我的目标是从进程或程序的“外部”执行此操作,因为我希望从操作系统进程的角度理解这一点,但欢迎提出任何想法!

这是python代码阻塞的过程:

import subprocess
proc = subprocess.call('ls -lRa /', shell=True)
Run Code Online (Sandbox Code Playgroud)

这是非阻塞进程的python代码:

import subprocess
proc = subprocess.Popen('ls -lRa /', shell=True)
Run Code Online (Sandbox Code Playgroud)

这是显示进程 ID 的“ps -ef”的输出:

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 /
Run Code Online (Sandbox Code Playgroud)

当这些“ls”命令正在处理时,我想显示哪些进程正在阻塞以及其他进程处于哪些状态。这个问题旨在成为一个向前发展的工具,用于在我学习使用 python 进行多处理时了解状态,所以虽然我相信 PID 14309 是阻塞的,而 PID 14442 是非阻塞的,尽管我可能有那个错误。这就是为什么能够查看或测试所有显示的 PID 对我有帮助的原因。

感谢尊敬的用户 'ubuntu' 和他们对此的回应: 阻塞和非阻塞子进程要求 提供启动代码。

在这种情况下,操作系统是 Ubuntu,但任何 debian 或操作系统注释都会有所帮助。

tde*_*ney 7

试试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
Run Code Online (Sandbox Code Playgroud)

这就是进程当前被阻塞的 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
Run Code Online (Sandbox Code Playgroud)