traversing task_struct->children in linux kernel

我与影子孤独终老i 提交于 2019-12-10 16:45:45

问题


I am trying to traverse a task_struct's children in the linux kernel and get information from the children. I'm having problems with all the information, so let's just keep it at the getting the pid for simplicity.

This is the relavant part of my code.

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);
}

I think the problem is with list_entry but I don't know how to fix it, all the examples I can find seem to be calling it the same way.

This should print out all the child PIDs, instead I always get the same number -17.... it's on the order of 10^9 or 10^11.

can anyone help me out here? compiling takes about 30 minutes, so trying a log of different things isn't really an option.


回答1:


You should be using

list_entry(p, struct task_struct, sibling);

Not

list_entry(p, struct task_struct, children);

Ho and also, you should lock the tasklist_lock when you go through the childrens.




回答2:


The assignment to tsk is in the wrong direction. current contains the, well, current task; to initialize tsk, you need to write

tsk = current;

FWIW, you should avoid copying structures. So in the loop, do

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

thus assigning to task pointer, rather than copying the entire task struct.



来源:https://stackoverflow.com/questions/1446239/traversing-task-struct-children-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!