2015-01-09 488 views
0

有沒有一種方法可以查詢Linux進程表中的進程狀態,以便能夠證明進程在查詢時是運行還是阻塞執行?我的目標是從流程或程序的「外部」執行此操作,因爲我希望通過操作系統流程瞭解這一點,但任何想法都是值得歡迎的!如何在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'命令正在處理,我想顯示哪些進程阻止和whi ch狀態其他人都在。這個問題是打算成爲一個工具前進,學習狀態,因爲我學習python多處理,所以雖然我相信PID 14309阻塞和PID 14442是非阻塞的,雖然我可能有這個錯誤。這就是爲什麼我能夠看到或測試所顯示的所有PID對此有幫助。

感謝尊敬的用戶'ubuntu'和他們對此的迴應: Blocking and Non Blocking subprocess calls 提供了入門者代碼。

在這種情況下的操作系統是Ubuntu,但任何debian或操作系統評論將有所幫助。

回答

2

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

優秀,這顯示了我正在尋找的東西。我在下面添加了另一部分內容,因爲我相信它有助於理解。 – apollon 2015-01-10 13:14:46

0

這可能不是你正在尋找的東西,但你可以嘗試ps aux,它會給你STAT列。這不會明確說明阻塞或非阻塞,但某些狀態會爲您提供有關過程狀態當前狀態的更多信息。例如無論是正在運行還是處於殭屍狀態(即將被收回)。

0

最能說明問題的關鍵過程是在短節目阻塞是它的所有腦幹進程表:

這清楚地表明瞭我的追求,格外由「等待」強調狀態以及缺乏這是無阻塞的程序已終止所有腦幹的,而子過程住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/

通知書命令「蟒蛇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/
0

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