本文介绍了for_each_process-是否还会遍历线程和进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想迭代内核中的所有任务(线程和进程),并使用for_each_process宏打印tid/pid和名称:
I would like to iterate all the tasks in the kernel (threads and processes) and print tid/pid and name using for_each_process macro:
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
如何区分线程和进程?
How can I distinguish between thread and process?
所以我将这样打印:
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();
这篇关于for_each_process-是否还会遍历线程和进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!