[問題] 短暫成為殭屍行程是正常的嗎?[已解決]

看板C_and_CPP作者 (みなさん、こんにちは)時間8年前 (2017/10/18 01:58), 8年前編輯推噓5(507)
留言12則, 4人參與, 8年前最新討論串1/1
開發平台(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

10/18 08:31, 8年前 , 1F
應該這樣說: 殭屍行程的目的就是為了等父行程來收屍
10/18 08:31, 1F

10/18 08:31, 8年前 , 2F
所以在父行程還有餘力收屍時殭屍行程自然不會消失
10/18 08:31, 2F

10/18 09:41, 8年前 , 3F
zombie 不是 parent 沒用 wait 去取 child 遺留的 exit
10/18 09:41, 3F

10/18 09:42, 8年前 , 4F
code 導致的嗎?跟你 child 有沒有提早結束無關吧
10/18 09:42, 4F

10/18 09:50, 8年前 , 5F
嗯嗯 我在父行程加了 sleep 讓子行程結束早於等待
10/18 09:50, 5F

10/18 13:05, 8年前 , 6F
你應該用wait或join吧 sleep不是很好
10/18 13:05, 6F

10/18 16:29, 8年前 , 7F
他有 wait 啊, 只是故意晚 wait 而已
10/18 16:29, 7F

10/18 16:31, 8年前 , 8F
說起來, 其實就是為了防止這篇講的這種事才會有 SIGCHLD
10/18 16:31, 8F

10/18 16:33, 8年前 , 9F
如果在 fork 前有先設定好 SIGCHLD 一來就 waitpid 清掉
10/18 16:33, 9F

10/18 16:33, 8年前 , 10F
那就不會留著殭屍行程了
10/18 16:33, 10F

10/18 16:34, 8年前 , 11F
這跟父行程的 main thread 在做什麼可以不相關
10/18 16:34, 11F

10/18 21:15, 8年前 , 12F
那個 signal 小心使用就是。畢竟所有的 fork 都有效
10/18 21:15, 12F
文章代碼(AID): #1PvaJ7kb (C_and_CPP)