简体   繁体   English

为什么 for_each_process 不显示每个任务?

[英]Why doesn't for_each_process show every task?

I am trying to loop through every process in a /proc utility I'm writing (a kernel module in /fs/proc ).我正在尝试遍历我正在编写的/proc实用程序( /fs/proc的内核模块)中的每个进程。 The problem is, I am only seeing processes in the root namespace.问题是,我只看到根命名空间中的进程。 I'm trying to use the macro for_each_process() from sched.h .我正在尝试使用sched.h的宏for_each_process()

I can type ps in a shell and see plenty of processes, but my for_each_process() loop doesn't see them.我可以在 shell 中输入ps并看到大量进程,但是我的for_each_process()循环没有看到它们。 What gives?是什么赋予了?

Note: I am wondering if it has something to do with rcu_read_lock() ?注意:我想知道它是否与rcu_read_lock() I'm afraid to put an rcu_read_lock() and I don't know where it should go.我害怕放一个rcu_read_lock() ,我不知道它应该放在哪里。 The trouble is, the documentation I read seems to say that in a preemptive kernel (mine is), it is illegal to sleep inside of rcu_read_lock() .问题是,我读过的文档似乎说在抢占式内核(我的是)中,在rcu_read_lock()内睡觉是非法的。 I need to call down_read(mmap_sem) which I am afraid will sleep.我需要调用down_read(mmap_sem) ,我担心它会睡觉。 So that means I can't use rcu_read_lock() ?所以这意味着我不能使用rcu_read_lock()

It should show you all the processes.它应该向您显示所有过程。 I have written code like this.我写过这样的代码。

struct task_struct *task;

for_each_process(p) {
    printk("Task %s (pid = %d)\n",p->comm, task_pid_nr(p));
}

This is printing all the processes.这是打印所有过程。 I suspect your proc_read function.我怀疑你的proc_read函数。 Can you paste your proc_read function over here?你能把你的proc_read函数贴在这里吗?

I think for_each_process() just gives you the thread group leader.我认为for_each_process()只是给你线程组的领导者。

This iterates over all task_struct variables (defined in sched.h).这将遍历所有task_struct变量(在 sched.h 中定义)。

struct task_struct *g, *p;
do_each_thread(g, p) {
    //do something with p
} while_each_thread(g, p);

This example prints out a long list, looks like it might be printing almost all of them....thought I have not counted them这个例子打印了一个很长的列表,看起来它可能打印了几乎所有的......以为我没有计算它们

http://tuxthink.blogspot.in/2011/03/using-foreachprocess-in-proc-entry.html http://tuxthink.blogspot.in/2011/03/using-foreachprocess-in-proc-entry.html

for_each_process is a kernel layer function, and it's really not how you're supposed to loop over processes in unix. for_each_process是一个内核层函数,它真的不是你应该如何在 unix 中循环进程。 Something a lot simpler, such as this (python, easily implemented in other languages), might be the solution you want.一些更简单的东西,比如这个(python,很容易用其他语言实现),可能是你想要的解决方案。

#Print each process id
import re,os
for fn in os.listdir( '/proc' ):
    if re.match('\d+', fn):
        print 'process %s'%fn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM