c

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

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();
链接地址: http://www.djcxy.com/p/67754.html

上一篇: git推送错误:HTTP请求失败:请求的URL返回504

下一篇: C