AzzUrr1 AzzUrr1 - 4 months ago 37
Linux Question

Kernel: Right way to check if process is running in c

I want to check if a process which pid I have is running from an kernel extension.

In user-space this would be simple:

if (kill(pid, 0) == 0) {
printf("Process %d is running\n", pid);
} else if (errno == ESRCH) {
printf("Process %d is not running\n", pid);
} else {
printf("This shouldn't happen oO\n");

But somehow kill() is not available in kernel. Is there another approach to do this?


You can use pid_task() or get_pid_task() to get a pointer to the struct task_struct of the process. If the call returns NULL then the process does not exist.

Note that you may need to be careful as the pid might have been recycled and is now used by a different process.