首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内核:检查进程是否在c#中运行的正确方法。

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

Stack Overflow用户
提问于 2014-01-21 13:33:05
回答 2查看 4.7K关注 0票数 3

我想检查我所拥有的pid进程是否是从内核扩展运行的。

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

代码语言:javascript
复制
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");

但是,在内核中是不可用的。还有别的方法可以做到吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-21 14:17:20

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

请注意,您可能需要小心,因为pid可能已被回收,现在正被另一个进程使用。

票数 6
EN

Stack Overflow用户

发布于 2014-01-21 14:29:44

克利斯朵夫回答后,我找到了解决这个问题的办法。

代码语言:javascript
复制
//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);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21259672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档