是否使用:process = Runtime.getRuntime().exec("logcat -d time");
或者:process = new ProcessBuilder()
. command("logcat","-d","time")
. redirectErrorStream(true)
. start();
我得到了同樣的結果: 它通常掛在 exec() 或者 start() 調用中,無論我怎麼做 ! 運行這裡線程的線程甚至不能用 Thread.interrupt() 中斷 ! 子進程絕對啟動,如果殺死上面的命令返回。
這些調用在第一次嘗試時可能會失敗,因此無法讀取它們的輸出 ! 我也可以使用一個簡單的"su -c殺死 xxx"命令行,同樣的結果 !
編輯:使用一些調試日誌在NDK項目中開始調試 java_lang_ProcessManager.cpp 文件 ! 這是我到目前為止發現的,在 fork() 之後,父對象執行以下操作:int result;
int count = read(statusIn, &result, sizeof(int));
close(statusIn);
儘管子進程不應該阻塞它: 這就是孩子做的( 如果開始 ): !//Make statusOut automatically close if execvp() succeeds.
fcntl(statusOut, F_SETFD, FD_CLOEXEC);
//Close remaining unwanted open fds.
closeNonStandardFds(statusOut, androidSystemPropertiesFd);
. . .
execvp(commands[0], commands);
//If we got here, execvp() failed or the working dir was invalid.
execFailed:
int error = errno;
write(statusOut, &error, sizeof(int));
close(statusOut);
exit(error);
孩子可能因 2個重複的原因而失敗: 1 - 子代碼沒有運行,但是父代碼認為它是 ! 2 - closeNonStandardFds ( statusOut,androidSystemPropertiesFd ) 上的子塊;
在任何情況下,父進程中的read(statusIn...) 都處於死鎖狀態 ! 還有一個子進程是死( 並且無法訪問,pid未知,沒有進程對象) !