2011-04-20 85 views
5

我想要去的过程中孩子们的任务(进程)列表,这里是代码:我怎样才能在内核代码孩子进程列表

void myFunc() 
{ 
    struct task_struct* current_task; 
    struct task_struct* child_task; 
    struct list_head children_list;  

    current_task = current; 
    children_list = current_task->children; 
    child_task = list_entry(&children_list,struct task_struct,tasks); 
    printk("KERN_INFO I am parent: %d, my child is: %d \n", 
      current_task->pid,child_task->pid); 
} 

当前PID是正确的,但孩子的pid不正确。 我在做什么错?

+0

我所不知道的关于linux-kernel,但我觉得它应该是'struct list_head * children_list'和后来'children_list =&current_task-> children'。 – 2011-04-20 10:09:47

+0

谢谢,但'current_task-> children'返回'list_head'而不是'list_head *' – Noga 2011-04-21 05:02:03

+0

我知道它已经过了5年,但@king_nak说的是你**不应该复制struct,而是使用指针指着它。所以你把'&'放在current_task-> children之前。 – 2017-03-15 16:40:26

回答

5
child_task = list_entry(&children_list,struct task_struct,children); 

注意,在LIST_ENTRY最后一个参数应该是children

顺便说一句:如果你不是很熟悉LIST_ENTRY,下面的文章是一个很好的来源: http://isis.poly.edu/kulesh/stuff/src/klist/