1

Does this line of code kill and reap a child process without a kill operation?

waitpid(pid,&cs,0);

Or, must it be in this format to kill and reap a process?

kill(pid,SIGTERM);
waitpid(pid,&cs,0);
Yazgan
  • 151
  • 11
  • 5
    `waitpid` waits for a process that has voluntarily exited, or that has been terminated by a signal. But no, `waitpid` doesn't do any killing itself. – Steve Summit Mar 18 '18 at 09:06
  • @SteveSummit Then, if I waitpid a child in parent process without killing the child, should it wait forever? – Yazgan Mar 18 '18 at 09:12
  • 5
    Yes -- or until the child exits on its own. – Steve Summit Mar 18 '18 at 09:24
  • If you don't want to wait forever, you can pass `WNOHANG` to `waitpid`: `waitpid(pid, &cs, WNOHANG)`. See [`waitpid`](https://linux.die.net/man/2/waitpid) – Pablo Mar 18 '18 at 18:01
  • The thing is for some reason(probably I just forgot) I didn't kill the child process but just waited it(with 'waitpid' without 'WNOHANG') in my code. Still it doesn't go to infinite loop and when I check processes with 'ps aux | grep Z', I see no zombies. Weird. @SteveSummit – Yazgan Mar 19 '18 at 11:04

0 Answers0