我是内核模块编程的新手。在我的程序中,我需要显示进程名称及其父进程的 pid。
下面是我的simple_init()函数
int simple_init(void){
printk(KERN_INFO "--------------Starting module--------------\n");
struct task_struct *intask = &init_task;
struct list_head fd = intask->children;
struct list_head *list;
struct task_struct *task;
int k =0;
list_for_each(list, &fd){
task = list_entry(list, struct task_struct, sibling);
struct task_struct *parent = task->parent;
pid_t parent_pid = parent -> pid;
printk(KERN_INFO "Name: %s ---- %d ----Parent: %d\n",task->comm, task->pid, parent_pid);
if (k==2) break;
k++;
}
return 0;
}
问题是在我添加了这一行之后:
struct task_struct *parent = task->parent;
现在,当我运行 insmod 命令时,它显示 Segementation fault,我必须重新启动机器(虚拟机)再试一次。
谁能告诉我这有什么问题吗?
最佳答案
内核代码,尤其是模块初始化代码,并不总是根据某些进程的行为运行。与中断和调度程序相关的代码都在没有任何特定进程的情况下运行。
所以我猜你的任务
可能是NULL
关于linux - 检索父 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040114/