最近遇到pipe_wait問題,父進程調用子進程時,子進程阻塞,cat /proc/$child/wchan輸出pipe_wait,進程阻塞在pipe_wait不執行,轉載文章對此問題分析很透徹。
問題背景
如果要在Java中調用shell腳本時,可以使用Runtime.exec或ProcessBuilder.start。它們都會返回一個Process對象,通過這個Process可以對獲取腳本執行的輸出,然后在Java中進行相應處理。例如,下面的代碼:
通常,安全編碼規范中都會指出:使用Process.waitfor的時候,可能導致進程阻塞,甚至死鎖。 那么這句應該怎么理解呢?用個實際的例子說明下。
問題描述
使用Java代碼調用shell腳本,執行后會發現Java進程和Shell進程都會掛起,無法結束。
Java代碼 processtest.java
- try
- {
- Process process = Runtime.getRuntime().exec(cmd);
- System.out.println("start run cmd=" + cmd);
- process.waitFor();
- System.out.println("finish run cmd=" + cmd);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
被調用的Shell腳本doecho.sh
- #!/bin/bash
- for((i=0; ;i++))
- do
- echo -n "0123456789"
- echo $i >> count.log
- done
掛起原因
- 主進程中調用Runtime.exec會創建一個子進程,用於執行shell腳本。子進程創建后會和主進程分別獨立運行。
- 因為主進程需要等待腳本執行完成,然后對腳本返回值或輸出進行處理,所以這里主進程調用Process.waitfor等待子進程完成。
- 通過shell腳本可以看出:子進程執行過程就是不斷的打印信息。主進程中可以通過Process.getInputStream和Process.getErrorStream獲取並處理。
- 這時候子進程不斷向主進程發生數據,而主進程調用Process.waitfor后已掛起。當前子進程和主進程之間的緩沖區塞滿后,子進程不能繼續寫數據,然后也會掛起。
- 這樣子進程等待主進程讀取數據,主進程等待子進程結束,兩個進程相互等待,最終導致死鎖。
解決方法
基於上述分析,只要主進程在waitfor之前,能不斷處理緩沖區中的數據就可以。因為,我們可以再waitfor之前,單獨啟兩個額外的線程,分別用於處理InputStream和ErrorStream就可以。實例代碼如下:
JDK上的說明
By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.
從JDK的說明中可以看出兩點:
- 如果系統中標准輸入輸出流使用的bufffer大小有限,所有讀寫時可能會出現阻塞或死鎖。------這點上面已分析
- 子進程的標准I/O已經被重定向到了父進程。父進程可以通過對應的接口獲取到子進程的I/O。------I/O是如何重定向的?
背后的故事
要回答上面的問題可以從系統的層面嘗試分析。
首先通過ps命令可以看到,在Linux上多出了兩個進程:一個Java進程、一個shell進程,且shell是java的子進程。
然后,可以看到shell進程的狀態顯示為pipe_w。我剛開始以為pipe_w表示pipe_write。進一步查看/proc/pid/wchan 發現pipe_w其實表示為pipe_wait。通常/proc/pid/wchan表示一個內存地址或進程正在執行的方法名稱。因此,這似乎表明該進程 在操作pipe時發生了等待,從而被掛起。我們知道pipe是IPC的一種,通常用於父子進程之間通信。這樣我們可以猜測:可能是父子進程之間通過 pipe通信的時候出現了阻塞。
另外,觀察父子進程的fd信息,即/proc/pid/fd。可以看到子進程的0/1/2(即:stdin/stdout/stderr)分別被重定向到了三個pipe文件;父親進程中對應的也有對着三個pipe文件的引用。
綜上所述,這個過程應該是這樣的:子進程不斷向pipe中寫數據,而父進程一直不讀取pipe中的數據,導致pipe被塞滿,子進程無法繼續寫入,所以出現pipe_wait的狀態。那么pipe到底有多大呢?
測試pipe的大小
因為我已經在doecho.sh的腳步中記錄了打印了字符數,查看count.log就可以知道子進程最終發送了多少數據。在子進程掛起 了,count.log的數據一致保持在6543不變。故,當前子進程向pipe中寫入6543*10=65430bytes時,出現進程掛起。 65536-65430=106byte即距離64K差了106bytes。
換另外的測試方式,每次寫入1k,記錄總共可以寫入多少。進程代碼如test_pipe_size.sh所示。測試結果為64K。兩次結果相差了106byte,那個這個pipe到底多大?
Linux上pipe分析
最直接的方式就是看源碼。Pipe的實現代碼主要在linux/fs/pipe.c中,我們主要看pipe_wait方法。
參考資料
Java 中的進程與線程
https://www.ibm.com/developerworks/cn/java/j-lo-processthread/
When Runtime.exec() won't
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=3
Linux進程間通信之管道(pipe)、命名管道(FIFO)與信號(Signal)
http://www.cnblogs.com/biyeymyhjob/archive/2012/11/03/2751593.html
buffering in standard streams
http://www.pixelbeat.org/programming/stdio_buffering/
Todd.log - a place to keep my thoughts onprogramming
http://www.cnblogs.com/weidagang2046/p/io-redirection.html
linux cross reference
http://lxr.free-electrons.com/source/fs/pipe.c#L103
How big is the pipe buffer
http://unix.stackexchange.com/questions/11946/how-big-is-the-pipe-buffer