作者Hazukashiine (みなさん、こんにちは)
看板C_and_CPP
標題[問題] 短暫成為殭屍行程是正常的嗎?
時間Wed Oct 18 01:58:15 2017
開發平台(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. -- Wikipedia, Zombie process, Sec 2
--
Lorem ipsum dolor sit amet, cons
ectetuer adipiscing elit. Aenean co
mmodo ligula eget dolor. Aenean mas
sa. Cum sociis natoque penatibus et
magnis dis parturient montes, nasce
tur ridiculus mus. Donec quam felis
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.68.52
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1508263111.A.BA5.html
※ 編輯: Hazukashiine (140.113.68.52), 10/18/2017 02:44:06
推 LPH66: 應該這樣說: 殭屍行程的目的就是為了等父行程來收屍 10/18 08:31
→ LPH66: 所以在父行程還有餘力收屍時殭屍行程自然不會消失 10/18 08:31
推 Bencrie: zombie 不是 parent 沒用 wait 去取 child 遺留的 exit 10/18 09:41
→ Bencrie: code 導致的嗎?跟你 child 有沒有提早結束無關吧 10/18 09:42
→ Hazukashiine: 嗯嗯 我在父行程加了 sleep 讓子行程結束早於等待 10/18 09:50
推 Darkautism: 你應該用wait或join吧 sleep不是很好 10/18 13:05
推 LPH66: 他有 wait 啊, 只是故意晚 wait 而已 10/18 16:29
→ LPH66: 說起來, 其實就是為了防止這篇講的這種事才會有 SIGCHLD 10/18 16:31
→ LPH66: 如果在 fork 前有先設定好 SIGCHLD 一來就 waitpid 清掉 10/18 16:33
→ LPH66: 那就不會留著殭屍行程了 10/18 16:33
→ LPH66: 這跟父行程的 main thread 在做什麼可以不相關 10/18 16:34
推 Bencrie: 那個 signal 小心使用就是。畢竟所有的 fork 都有效 10/18 21:15