作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想迭代内核中的所有任务(线程和进程)并使用 for_each_process 宏打印 tid/pid 和名称:
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
如何区分线程和进程?
所以我会这样打印:
if (p->real_parent->pid == NULL)
printk("PROCESS: name: %s pid: %d \n",p->comm,p->pid);
else
printk("THREAD: name: %s tid: %d \n",p->comm,p->pid);
最佳答案
您需要以下宏:
/*
* Careful: do_each_thread/while_each_thread is a double loop so
* 'break' will not work as expected - use goto instead.
*/
#define do_each_thread(g, t) \
for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
#define while_each_thread(g, t) \
while ((t = next_thread(t)) != g)
像这样使用它们:
rcu_read_lock();
do_each_thread(g, t) {
//...
} while_each_thread(g, t);
rcu_read_unlock();
关于c - for_each_process - 它是否也遍历线程和进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14005599/
我是一名优秀的程序员,十分优秀!