【问题标题】:How can I get the children process list in kernel code如何在内核代码中获取子进程列表
【发布时间】:2011-04-20 10:03:34
【问题描述】:

我想获取一个进程的子任务(进程)列表,代码如下:

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 不正确。 我做错了什么?

【问题讨论】:

  • 我对linux-kernel一无所知,但我觉得应该是struct list_head *children_list和后来的children_list = &current_task->children
  • 谢谢,但 current_task->children 返回 list_head 而不是 list_head*
  • 我知道它已经过去了 5 年,但 @king_nak 的意思是你不应该复制结构,而是使用指向它的指针。所以你把'&'放在 current_task->children 之前。

标签: c linux-kernel linked-list task


【解决方案1】:
child_task = list_entry(&children_list,struct task_struct,children);

注意,list_entry 的最后一个参数应该是children

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 2011-09-20
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多