Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check running processes in C

Tags:

c

unix

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().

like image 544
Sachin Chourasiya Avatar asked Mar 28 '11 14:03

Sachin Chourasiya


People also ask

How do you check if a process is still running in C?

/proc/<PID> should exists if the process PID is still running.

How do you check if any process is running?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.

What command shows a list of running processes?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How do I know if a process is running on PID?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


1 Answers

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 */
}
like image 190
Juliano Avatar answered Sep 28 '22 17:09

Juliano