開發平台(Platform): (Ex: Win10, Linux, ...)
Linux
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
gcc-6
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
短暫成為殭屍行程是正常的嗎?
餵入的資料(Input):
預期的正確結果(Expected Output):
即使子行程提早結束也不會變成殭屍行程
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0) { // child process
system("sleep 0");
_exit(EXIT_SUCCESS);
}
else if (pid > 0) { // parent process
system("sleep 3"); // 子行程變成殭屍行程
waitpid(pid, &ret, 0); // 順利回收子行程
}
補充說明(Supplement):
上述的實驗就算子行程提早結束了
waitpid 好像還是可以順利回收那個變成殭屍的子行程
這樣產生短暫的殭屍行程是正常的嗎 XDD (抓頭
謝謝
== 已解決 ==
這應該是正常的
Synchronously waiting for the specific child processes in a (specific) order
may leave zombies present longer than the above-mentioned "short period of
time". It is not necessarily a program bug.
作者:
LPH66 (-6.2598534e+18f)
2017-10-18 16:29:00他有 wait 啊, 只是故意晚 wait 而已說起來, 其實就是為了防止這篇講的這種事才會有 SIGCHLD如果在 fork 前有先設定好 SIGCHLD 一來就 waitpid 清掉那就不會留著殭屍行程了這跟父行程的 main thread 在做什麼可以不相關