c - 我们如何检查给定 parent 的 child 是否存在?

标签 c linux-kernel

我正在尝试遍历所有过程并像这样打印他们的 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/

相关文章:

c - 提交和锁定虚拟内存有什么区别?

c - 如果我们在 SMP 上的中断处理程序中休眠会发生什么

android - Nexus 7 Linux 内核 : What file to fiddle with for volume control hacking ?

linux - 当软重启 Linux 内核卡在 "Uncompressing Linux... done, booting the kernel"

linux - 为什么无法标记所需的内核模块进行编译、隐藏依赖?

c - 如果您只能从用户空间使用 outb/inb,那么 Linux 字符设备驱动程序有什么意义?

c - 字符工具函数

c - c中无限字符串的strcat

c - 骰子游戏数字不是随机的

c++ - unsigned long 和 UINT64 的区别