c语言 - 如何从task_struct获得最小的孩子PID



我正在做一个项目,涉及为Linux 3.18.20编写一个新的系统调用。这个系统调用应该在一个新定义的结构体中存储有关当前运行进程的各种信息。

结构体的一个字段是进程最小子进程的PID,我一直在task_struct结构体中搜索有关此的信息,如下所定义:http://lxr.free-electrons.com/source/include/linux/sched.h。我一直在测试我的新系统调用,我似乎无法从struct list_head子类中找到合适的PID值。

我的测试函数包括fork子线程,存储fork的返回值,并将其与我在父线程的task_struct中执行各种操作获得的值进行比较。我得到了父类的task_struct,并且我尝试了struct list_head的所有以下宏来获得正确的PID。没有一个是正确的

    printk("list_next_entry pid = %dn", list_next_entry(task, children)->pid);
printk("list_prev_entry pid = %dn", list_prev_entry(task, children)->pid);
printk("list_entry pid = %dn", list_entry(&task->children, struct task_struct, children)->pid);
printk("list_first_entry pid = %dn", list_first_entry(&task->children, struct task_struct, children)->pid);
printk("list_last_entry pid = %dn", list_last_entry(&task->children, struct task_struct, children)->pid);

这接近于找到父进程的最小子进程的正确方法吗?我如何从进程的task_struct中找到进程的最小子进程的PID ?

作为task_struct注释字段的注释,children

我的孩子名单

siblings

链接在我父母的子列表

所以指向第一个子任务的指针可以使用

请求
list_first_entry(&task->children, struct task_struct, siblings)

一个进程可以从它所做的最后一次fork(2)系统调用中获得最年轻的进程pid。您必须修补clone(2)系统调用(因为它是linux中所有fork调用的基础),并将返回进程的pid存储在u区(或linux内核中的任何名称)中,以便当用户调用您的系统调用时可以从那里获得它。

最新更新