Given the pid of a Linux process, I want to check, from a C program, if the process is still running.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Issue a
kill(2)system call with0as the signal. If the call succeeds, it means that a process with this pid exists.If the call fails and
errnois set toESRCH, a process with such a pid does not exist.Quoting the POSIX standard:
Note that you are not safe from race conditions: it is possible that the target process has exited and another process with the same pid has been started in the meantime. Or the process may exit very quickly after you check it, and you could do a decision based on outdated information.
Only if the given pid is of a child process (
fork‘ed from the current one), you can usewaitpid(2)with theWNOHANGoption, or try to catchSIGCHLDsignals. These are safe from race conditions, but are only relevant to child processes.