How to check if a process with pid X is a zombie?

I got the PID of the process, and I need to check if it is a zombie using POSIX system calls in C. How do I do this?

The problem I am facing is that I have a process, and it forks on many children, the children do exec all, and sometimes I want to do exec in the background, so I can not wait () for children go in the background. I could wait () periodically (once in my main loop), but I need to know what processes are zombies, so my father’s process does not hang, waiting for children who will not be over soon.

If you're interested, I create a unix shell, and in the nature of the shell there are processes that behave asynchronously.

+3
source share
5 answers

You cannot check if a zombie process is with pure POSIX calls - unless you are a parent process and reap it with the waitlast name . Therefore, you need to find a good place to wait for the baby.

One option is to install a handler SIGCHLDand do it there waitpid(0, &status, WNOHANG). Be sure to loop until he no longer finds any processes - if two children die in a short period of time, you can get only one SIGCHLD.

- fork, fork(), ( A) fork, ( B) exec. . wait() ing A. A , B, . , , B, , , .

+5

.. .. - ( - ), - , ? , , pid - , waitpid(2) WNOHANG ? , , - wait .

+3

, Unix; : SIGINT/SIGQUIT

+1

- waitpid(2) WNOHANG, . SIGCHLD, , , - , .

.

+1

ps, , , " "? , c-.

0

Source: https://habr.com/ru/post/1720983/


All Articles