我正在尝试迭代所有过程并打印他们的孩子,如:
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
到目前为止,我在模块中的代码看起来像这样。我不想打印没有孩子的父母的pid,所以我想在下面的代码中使用if条件检查是否存在子项。 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;
}
答案 0 :(得分:1)
如果某个流程有子女,您可以查看list_empty
for_each_process(parent) {
if (!list_empty(&parent->children)) {
...
}
}