我正在嘗試呼叫 cmake 并將輸出重定向到管道。
重現:
git clone https://github.com/avast/retdec(似乎每個 CMake 專案,gradle 專案也不起作用)mkdir build&&cd build- 添加檔案
test.hs:
import System.Posix.IO
import System.Process
import System.Exit
import System.IO
main :: IO ()
main = do
(code, content) <- createProcessRedirected "cmake" ["..", "-DCMAKE_INSTALL_PREFIX=/opt/root/"]
case code of
ExitFailure _ -> do
putStrLn ("OOPS" content)
return ()
_ -> do
putStrLn "After cmake"
(code2, content2) <- createProcessRedirected "make" ["install", "-j8"]
case code2 of
ExitFailure _ -> do
putStrLn ("OOPS" content2)
return ()
_ -> do
putStrLn "SUCCESS"
return ()
-- Based on https://stackoverflow.com/a/14027387
createProcessRedirected :: String -> [String] -> IO (ExitCode, String)
createProcessRedirected command args = do
(p_r, p_w) <- System.Posix.IO.createPipe
h_r <- fdToHandle p_r
h_w <- fdToHandle p_w
(_, _, _, h_proc) <- createProcess (proc command args) {
std_out = UseHandle h_w,
std_err = UseHandle h_w,
create_group = False,
delegate_ctlc = True}
ret_code <- waitForProcess h_proc
content <- hGetContents h_r
return (ret_code, content)
只需編譯此檔案 ( ghc test.hs -Wall -Wextra)
如果我./test現在嘗試執行,它將構建一段時間(我可以看到 c /make 行程是如何產生的),但在某一時刻它會簡單地停止。然后我有這個行程樹:
test
-> make install -j8
-> /usr/bin/cmake -P cmake_install.cmake
如果我附加 gdb 并運行bt(前 6 行):
#0 0x00007f4ac7f8c7f7 in __GI___libc_write (fd=1, buf=0x5607747dd2f0, nbytes=60) at ../sysdeps/unix/sysv/linux/write.c:26
#1 0x00007f4ac7f1068d in _IO_new_file_write (f=0x7f4ac80865a0 <_IO_2_1_stdout_>, data=0x5607747dd2f0, n=60) at fileops.c:1181
#2 0x00007f4ac7f0fa06 in new_do_write (fp=0x7f4ac80865a0 <_IO_2_1_stdout_>, data=0x5607747dd2f0 "-- Up-to-date: /opt/root/include/retdec/llvm/IR/OptBisect.h\ns.h\nexYAML.h\n.h\nnager.h\n\nayer.h\n.h\n\nt execute permission?\nif(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n set(CMAKE_INSTALL_SO_NO_EXE \"0\")\nendif()"..., to_do=to_do@entry=60) at /usr/src/debug/glibc-2.34-11.fc35.x86_64/libio/libioP.h:947
#3 0x00007f4ac7f11729 in _IO_new_do_write (to_do=60, data=<optimized out>, fp=<optimized out>) at fileops.c:423
#4 _IO_new_do_write (fp=<optimized out>, data=<optimized out>, to_do=60) at fileops.c:423
#5 0x00007f4ac7f0f828 in _IO_new_file_sync (fp=0x7f4ac80865a0 <_IO_2_1_stdout_>) at fileops.c:799
然后我嘗試繼續運行它,10 秒后我再次中斷,我得到完全相同的回溯。
但是如果我注釋掉這兩行:
std_out = UseHandle h_w,
std_err = UseHandle h_w,
有用。
我究竟做錯了什么?
uj5u.com熱心網友回復:
管道的緩沖區大小不是無限的。您正在創建一個死鎖,其中子行程掛起,因為它正在嘗試寫入已滿的緩沖區,并且您的父行程在子行程完成之前不會嘗試從緩沖區讀取任何內容。要解決此問題,您需要在子行程仍在運行時使用另一個執行緒從緩沖區讀取。最簡單的方法是使用readProcess或 類似的函式來代替createProcess. 如果這不能給你足夠的靈活性來做你想做的事,那么你需要自己創建和管理另一個執行緒,你可以通過查看如何readProcess實作來了解如何做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/398726.html
上一篇:【ZooKeeper】上班摸魚時間——一文了解 ZAB 協議
下一篇:寫入檔案的好習慣
