【发布时间】: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