gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:45 25 4
gpt4 key购买 nike

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

这里是python代码阻塞的过程:

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

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

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

这是显示进程 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 /

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

感谢尊敬的用户“ubuntu”及其对此的回应: Blocking and Non Blocking subprocess calls用于提供入门代码。

本例中的操作系统是 Ubuntu,但任何 debian 或操作系统评论都会有所帮助。

最佳答案

尝试 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

关于python - 如何在 Linux 中显示进程状态(阻塞、非阻塞),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27869182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com