2011-04-20 48 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/