簡體   English   中英

如何在Linux中顯示進程狀態(阻塞,非阻塞)

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

有沒有一種方法可以查詢Linux進程表中進程的狀態,從而能夠證明執行查詢時進程是正在運行還是被阻塞? 我的目標是從流程或程序的“外部”進行此操作,因為我希望從OS流程的角度來理解這一點,但是歡迎任何想法!

這是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'以及他們對此的回應: 阻止和非阻止子流程要求提供啟動代碼。

在這種情況下,操作系統是Ubuntu,但是任何debian或OS注釋都將有所幫助。

嘗試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

這可能並不是您想要的,但是您可以嘗試使用ps aux ,它將為您提供STAT列。 這不會明確表示阻塞或非阻塞,但是某些狀態將為您提供有關當前進程狀態的更多信息。 例如,它正在運行還是處於僵屍狀態(即將收回)。

流程在短程序中被阻塞的最有說服力的關鍵是它在流程表中的存在:

這清楚地顯示了我正在尋找的內容,特別是“等待”狀態和缺少程序的存在所強調,該程序是非阻塞的,在子進程只運行時該程序已終止

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 /

使用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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM