Sachin Chourasiya Sachin Chourasiya - 3 months ago 21
C Question

Check running processes in C

Can some one please tell me how to check if a unix process with a given process id is running inside a C program. I know I can call system() and use the ps command but I dont want to call the system().

Answer

Using kill(2):

if (kill(pid, 0) == 0) {
    /* process is running or a zombie */
} else if (errno == ESRCH) {
    /* no such process with the given pid is running */
} else {
    /* some other error... use perror("...") or strerror(errno) to report */
}
Comments