Linux 为什么 for_each_process 不显示每个任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9652111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't for_each_process show every task?
提问by Robert Martin
I am trying to loop through every process in a /proc
utility I'm writing (a kernel module in /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
.
我正在尝试遍历/proc
我正在编写的实用程序(中的内核模块/fs/proc
)中的每个进程。问题是,我只看到根命名空间中的进程。我试图使用宏for_each_process()
从sched.h
。
I can type ps
in a shell and see plenty of processes, but my for_each_process()
loop doesn't see them. What gives?
我可以ps
在 shell 中输入并看到大量进程,但我的for_each_process()
循环没有看到它们。是什么赋予了?
Note:I am wondering if it has something to do with rcu_read_lock()
? I'm afraid to put an rcu_read_lock()
and I don't know where it should go. The trouble is, the documentationI read seems to say that in a preemptive kernel (mine is), it is illegal to sleep inside of rcu_read_lock()
. I need to call down_read(mmap_sem)
which I am afraid will sleep. So that means I can't use rcu_read_lock()
?
注意:我想知道它是否与rcu_read_lock()
? 我害怕放一个rcu_read_lock()
,我不知道它应该放在哪里。问题是,我读过的文档似乎说在抢占式内核中(我的是),在rcu_read_lock()
. 我需要打电话down_read(mmap_sem)
,我怕会睡着。所以这意味着我不能使用rcu_read_lock()
?
采纳答案by jjm
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. Can you paste your proc_read
function over here?
这是打印所有过程。我怀疑你的proc_read
功能。你能把你的proc_read
函数贴在这里吗?
回答by jjm
for_each_process
is a kernel layer function, and it's really not how you're supposed to loop over processes in unix. Something a lot simpler, such as this (python, easily implemented in other languages), might be the solution you want.
for_each_process
是一个内核层函数,它真的不是你应该如何在 unix 中循环进程。一些更简单的东西,比如这个(python,很容易用其他语言实现),可能是你想要的解决方案。
#Print each process id
import re,os
for fn in os.listdir( '/proc' ):
if re.match('\d+', fn):
print 'process %s'%fn
回答by tux
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
回答by brietz
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);