内核:检查进程是否在c中运行的正确方法

arn*_*app 2 c linux kernel process linux-kernel

我想检查我的pid进程是否从内核扩展运行.

在用户空间中,这很简单:

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");
Run Code Online (Sandbox Code Playgroud)

但不知何故,kill()在内核中不可用.有另一种方法可以做到这一点吗?

Chr*_*ier 5

您可以使用pid_task()get_pid_task()获取指向struct task_struct进程的指针.如果调用返回,NULL则该进程不存在.

请注意,您可能需要小心,因为pid可能已被回收,现在由不同的进程使用.