for_each_process - Does it iterate over the threads and the processes as well?

只谈情不闲聊 提交于 2019-12-21 12:12:02

问题


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?

So I'll print it like that:

 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);

回答1:


The following macros are what you need:

/*
 * 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)

Use them like this:

        rcu_read_lock();
        do_each_thread(g, t) {
            //...
        } while_each_thread(g, t);

        rcu_read_unlock();


来源:https://stackoverflow.com/questions/14005599/for-each-process-does-it-iterate-over-the-threads-and-the-processes-as-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!