我对计算一个过程中的孩子和兄弟姐妹的数量感到有点困惑。我有一个进程信息的结构,如下所示:
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函数计算子项和兄弟的数量,但我不知道这是否是正确的方法。
答案 0 :(得分:0)
struct task_struct的子节点和兄弟节点链接了所述任务的所有子节点和兄弟节点,所以是的,你正在做的是正确的。