我想检查我所拥有的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");但是,在内核中是不可用的。还有别的方法可以做到吗?
发布于 2014-01-21 14:17:20
您可以使用pid_task()或get_pid_task()获取指向进程的struct task_struct的指针。如果调用返回NULL,则进程不存在。
请注意,您可能需要小心,因为pid可能已被回收,现在正被另一个进程使用。
发布于 2014-01-21 14:29:44
克利斯朵夫回答后,我找到了解决这个问题的办法。
//necessary imports
#include <linux/sched.h>
#include <linux/pid.h>
//Rest of your module
pid_t pid = myPid; //integer value of pid
struct pid *pid_struct = find_get_pid(pid); //function to find the pid_struct
struct task_struct *task = pid_task(pid_struct,PIDTYPE_PID); //find the task_struct
if (task == NULL)
{
printk (KERN_INFO "Process with pid %d is not running \n",argument);
}
else{
printk (KERN_INFO "Process with pid %d is running \n",argument);
}https://stackoverflow.com/questions/21259672
复制相似问题