我正在尝试遍历所有过程并像这样打印他们的 child :
parent pid = 1
-------------- somename_1, pid = 2, state = 0
-------------- somename_2, pid = 3, state = 0
parent pid = 4
-------------- somename_3, pid = 5, state = 0
-------------- somename_4, pid = 6, state = 0
到目前为止,我在模块中的代码如下所示。我不想打印没有 child 的 parent 的 pid,所以我想在下面的代码中使用 if 条件来检查 child 是否存在。 if 条件似乎对我不起作用。有什么想法吗?
static int my_read_proc(char *buf, char **start, off_t off, int count,
int *peof, void *data)
{
int len = 0;
struct task_struct *parent, *child_ptr;
struct list_head *child_runner;
for_each_process(parent){
len += sprintf(buf+len, "parent pid = %d\n", parent->pid);
list_for_each(child_runner, &parent->children){
child_ptr = list_entry(child_runner, struct task_struct, sibling);
len += sprintf(buf+len, "------------------ %s: pid = %d, state = %ld\n" , child_ptr->comm, child_ptr->pid, child_ptr->state);
}
}
printk(KERN_INFO "%s", buf);
return len;
}
最佳答案
您可以使用 list_empty
查看, 如果一个进程有任何 child
for_each_process(parent) {
if (!list_empty(&parent->children)) {
...
}
}
关于c - 我们如何检查给定 parent 的 child 是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22553573/