我想遍历所有的任务在内核(线程和进程)和打印TID / PID和名称使用for_each_process宏:
#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();
文章来源: for_each_process - Does it iterate over the threads and the processes as well?