【问题标题】:Calculating the number of children and sibiling of a process in the kernel mode计算内核模式下进程的子进程数和兄弟进程数
【发布时间】:2016-07-31 21:10:05
【问题描述】:

我对计算进程的子进程和同级进程的数量有点困惑。我有一个进程信息的结构,如下所示:

struct process_info {
    long pid; /* Process ID */
    char name[/* Some size. */]; /* Program name of process */
    long state; /* Current process state */
    long uid; /* User ID of process owner */
    long nvcsw; /* # voluntary context switches */
    long nivcsw; /* # involuntary context switches */
    long num_children; /* # children process has */
    long num_siblings; /* # sibling process has */
};

而且我有一个函数可以从上述结构中填充并返回进程信息:

struct process_info get_process_info(struct task_struct* this_task) {
    struct process_info res;
    int temp_num_children = 0;
    int temp_num_sibling = 0;
    struct list_head* traverse_ptr;

    res.pid = this_task->pid;
    memcpy(res.name, this_task->comm, /* The size of the string declared before the struct above. */);
    res.state = this_task->state;
    res.uid = this_task->cred->uid.val;
    res.nvcsw = this_task->nvcsw;
    res.nivcsw = this_task->nivcsw;


    list_for_each(traverse_ptr, &(this_task->children)) {
        ++temp_num_children;
    }


    list_for_each(traverse_ptr, &(this_task->sibling)) {
        ++temp_num_sibling;
    }
    res.num_children = temp_num_children;
    res.num_siblings = temp_num_sibling;

    return res;
}

除了孩子和兄弟姐妹的数量之外,填写所有信息非常简单,因为它们已经在 struct task_list 实例中。但是,我想使用 list_for_each 函数计算孩子和兄弟姐妹的数量,但我不知道这是否是正确的方法。

【问题讨论】:

    标签: c linux kernel system-calls


    【解决方案1】:

    struct task_struct 的孩子和兄弟姐妹链接了所述任务的所有孩子和兄弟姐妹,所以是的,你所做的是正确的。

    【讨论】:

    • 另外,这个任务是否与它的兄弟姐妹一起计算,因为我对每个 process_info 的计数器(我将每个进程及其祖先的进程信息存储在一个不同函数的数组中,它使用这个获取信息的函数)总是至少计数 1?这是正确的吗?
    • sibling 是共享同一个父进程的进程,所以在大多数情况下它是 1。因此您通过分叉多个进程进行验证,然后在其中一个子进程中调用您的函数。
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多