问题描述
给出一个进程或线程的任务结构,遍历属于同一进程的所有其他线程的惯用法是什么?
Given a task struct for a process or a thread, what's the idiom of iterating through all other threads belonging to the same process?
推荐答案
Linux不区分进程(任务)和线程.该库调用fork()和pthread_create()使用相同的系统调用clone(). fork()和pthread_create()之间的区别是传递给clone()的位掩码.此位掩码描述了哪些资源(内存,文件,文件系统,信号处理程序等).有关详细信息,请参见man clone(2).
Linux does not distinguish between a process(task) and a thread. The library calls fork() and pthread_create() use the same system call clone(). The difference between fork() and pthread_create() is the bitmask passed to clone(). This bitmask describes which resources (memory, files, filesystems, signal handler,...). See man clone(2) for the details.
无论如何,都存在一个称为线程组的东西,并且对clone()调用有一个特殊标志,该标志指示新进程属于同一线程组.此机制通常用于将使用clone()在位掩码中指定CLONE_THREAD的所有创建的任务放在一起.对于此线程,sched.h包含文件中存在宏* while_each_thread *.它的用法是这样的:
Anyway there is something called a thread group and a special flag to the clone() call which indicates that the new process belongs the the same thread group. This mechanism is normally used to keep together all tasks which are created with clone() specifying CLONE_THREAD in the bitmask.For this threads there exists the macro *while_each_thread* in the sched.h include file. It is used like this:
struct task_struct *me = current();
struct task_stuct *t = me;
do {
whatever(t);
}while_each_thread(me, t);
这篇关于内核:如何从进程的task_struct查找所有线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!