Linux kernel 遍历任务结构->;linux内核中的子级

Linux kernel 遍历任务结构->;linux内核中的子级,linux-kernel,linked-list,Linux Kernel,Linked List,我试图在linux内核中遍历task_struct的子对象,并从子对象获取信息。我对所有的信息都有问题,所以为了简单起见,让我们保持在获取pid的时候 这是我代码的相关部分 struct list_head * p; struct task_struct ts, *tsk; pid_t tmp_pid; INIT_LIST_HEAD(&ts.children); current = tsk; list_for_each(p, &(tsk->children)){

我试图在linux内核中遍历task_struct的子对象,并从子对象获取信息。我对所有的信息都有问题,所以为了简单起见,让我们保持在获取pid的时候

这是我代码的相关部分

struct list_head * p;
struct task_struct ts, *tsk;
pid_t tmp_pid;
INIT_LIST_HEAD(&ts.children);

current = tsk;

list_for_each(p, &(tsk->children)){
     ts = *list_entry(p, struct task_struct, children);
     tmp_pid = ts.pid;
     printk("the pid is %d\n", tmp_pid);
}
我认为问题在于列表项,但我不知道如何修复它,我能找到的所有示例似乎都以相同的方式调用它

这应该打印出所有的孩子PID,相反,我总是得到相同的数字-17。。。。大约是10^9或10^11


有人能帮我吗?编译大约需要30分钟,因此尝试不同内容的日志实际上不是一个选项。

分配给tsk的任务方向错误。当前包含了当前的任务;要初始化tsk,您需要编写

tsk = current;
FWIW,您应该避免复制结构。因此,在循环中,请执行以下操作:

tsk = list_entry(p, struct task_struct, children);
因此,分配给任务指针,而不是复制整个任务结构。

您应该使用

list_entry(p, struct task_struct, sibling);
不是


还有,当你检查孩子时,你应该锁定任务列表。\u锁定。

哦,哎哟-忽略反向电流/tsk分配。在我的真实代码中,这是错误的。你应该真正编辑你的问题,发布一个真实的代码示例来演示问题,而不是重新构建。你应该使用list_for_each_entry()。
list_entry(p, struct task_struct, children);