- 论坛徽章:
- 9
|
static inline struct task_struct *next_thread(const struct task_struct *p)
{
return list_entry_rcu(p->thread_group.next,
struct task_struct, thread_group);
}
static inline int thread_group_empty(struct task_struct *p)
{
return list_empty(&p->thread_group);
}
上面是2.6.34.10的代码,可以采用thread_group_empty来判断一个进程是否有线程。thread_group链表为线程组所有线程的链表。
下面这个函数判断当前task_struct是否线程组的组长。
/* de_thread depends on thread_group_leader not being a pid based check */
#define thread_group_leader(p) (p == p->group_leader)
下面两个宏是内核用于遍历所有的task_struct的宏,上面一个用于编译进程(或线程组组长),下面一个遍历所有的子线程。
#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)
以上的代码出自2.6.34.10的sched.h文件。其它内核版本请作为参考。 |
|