Check if process exists given its pid

后端 未结 6 1280
孤独总比滥情好
孤独总比滥情好 2020-12-01 02:06

Given the pid of a Linux process, I want to check, from a C program, if the process is still running.

6条回答
  •  执念已碎
    2020-12-01 02:24

    kill(pid, 0) is the typical approach, as @blagovest-buyukliev said. But if the process you are checking might be owned by a different user, and you don't want to take the extra steps to check whether errno == ESRCH, it turns out that

    (getpgid(pid) >= 0)
    

    is an effective one-step method for determining if any process has the given PID (since you are allowed to inspect the process group ID even for processes that don't belong to you).

提交回复
热议问题