在linux内核中遍历task_struct->子节点

时间:2009-09-18 18:40:10

标签: 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)){
     ts = *list_entry(p, struct task_struct, children);
     tmp_pid = ts.pid;
     printk("the pid is %d\n", tmp_pid);
}

我认为问题在于list_entry,但我不知道如何修复它,我能找到的所有示例似乎都是以同样的方式调用它。

这应打印出所有的子PID,而不是总是得到相同的数字-17 ....它的大小为10 ^ 9或10 ^ 11。

任何人都可以帮助我吗?编译大约需要30分钟,因此尝试记录不同的东西并不是一种选择。

2 个答案:

答案 0 :(得分:7)

您应该使用

list_entry(p, struct task_struct, sibling);

list_entry(p, struct task_struct, children);

何而且,当你经过孩子们时,你应该锁定tasklist_lock。

答案 1 :(得分:0)

对tsk的分配方向错误。 current包含当前任务;要初始化tsk,你需要写

tsk = current;

FWIW,你应该避免复制结构。所以在循环中,做

tsk = list_entry(p, struct task_struct, children);

因此分配给任务指针,而不是复制整个任务结构。