gpt4 book ai didi

c - 如何从 task_struct 获取最 child 子的 PID

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:22 30 4
gpt4 key购买 nike

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

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

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

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

这是否接近尝试找到父进程中最小的子进程的正确方法?我如何从该进程的 task_struct 中找到该进程最小的子进程的 PID?

最佳答案

作为对 task_struct 字段的注释,children

list of my children

sibling

linkage in my parent's children list

因此可以使用请求指向第一个子任务的指针

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

关于c - 如何从 task_struct 获取最 child 子的 PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092164/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com