简体   繁体   English

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

[英]How to show process state (blocking, non-blocking) in Linux

Is there a way to query the state of processes in a Linux process table to be able to demonstrate if a process is running or blocked at the time the query is executed? 有没有一种方法可以查询Linux进程表中进程的状态,从而能够证明执行查询时进程是正在运行还是被阻塞? My goal is to do this from 'outside' the process or program, as I hope to understand this in terms of the OS process, but any thought are welcome! 我的目标是从流程或程序的“外部”进行此操作,因为我希望从OS流程的角度来理解这一点,但是欢迎任何想法!

Here is the python code blocked process: 这是python代码被阻止的过程:

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

Here is the python code for a non-blocked process: 这是非阻塞进程的python代码:

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

Here is the output of the 'ps -ef' showing the process ids: 这是显示进程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 /

While these 'ls' commands are processing, I'd like to show which processes are blocking and which states the others are in. The question is intended to be a tool going forward, for learning about states as I learn multiprocessing with python, so while I believe PID 14309 to be blocking and PID 14442 to be non-blocking, although I may have that wrong. 在处理这些“ ls”命令时,我想显示哪些进程正在阻塞,其他进程处于哪些状态。该问题旨在作为一种工具,用于在我学习python的多处理过程时了解状态。尽管我可能认为有错误,但我认为PID 14309处于阻塞状态,而PID 14442处于非阻塞状态。 That is why it would be helpful for me to be able to see or test this for all of the PIDs shown. 这就是为什么能够对所示的所有PID进行查看或测试对我有帮助的原因。

Thanks to the venerable user 'ubuntu' and their response to this: Blocking and Non Blocking subprocess calls for providing the starter code. 感谢久负盛名的用户'ubuntu'以及他们对此的回应: 阻止和非阻止子流程要求提供启动代码。

The OS in this case is Ubuntu, but any debian or OS comment would be helpful. 在这种情况下,操作系统是Ubuntu,但是任何debian或OS注释都将有所帮助。

Try ps -weo pid,stat,wchan:32,args . 尝试ps -weo pid,stat,wchan:32,args You'll get output like: 您将得到如下输出:

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

Thats the pid, state flags (see below), where the process is currently blocked and the command line. 就是pid,状态标志(请参见下文),当前阻止进程和命令行的位置。 Flags are: 标志是:

       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

This may not be exactly what you're looking for, but you can try ps aux , which will give you the STAT column. 这可能并不是您想要的,但是您可以尝试使用ps aux ,它将为您提供STAT列。 This won't explicitly say blocking or non-blocking, but certain states will provide you with more information about what the process state current is. 这不会明确表示阻塞或非阻塞,但是某些状态将为您提供有关当前进程状态的更多信息。 eg whether it's running or is in zombie state (about to be reclaimed). 例如,它正在运行还是处于僵尸状态(即将收回)。

The most telling key to a process being blocking in a short program is it's existance in the process table: 流程在短程序中被阻塞的最有说服力的关键是它在流程表中的存在:

This clearly shows what I was seeking, particulary emphasized by the 'wait' state and by the lack of existance of the program which was non-blocking which has terminated while the subprocesses live onl 这清楚地显示了我正在寻找的内容,特别是“等待”状态和缺少程序的存在所强调,该程序是非阻塞的,在子进程只运行时该程序已终止

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 /

notice the lack of a PID for the command 'python popen_nb.py' as it's already finished. 请注意,命令“ python popen_nb.py”缺少PID,因为它已经完成。 However both the terminal windows I have open are spewing results from an ls on my drive... 但是我打开的两个终端窗口都在驱动器上显示ls的结果...

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

Use ps -elf will ouput the process state to you. 使用ps -elf将输出过程状态给您。 In Centos 6.5 , the second column is process state: 在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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM