kernel: how to find all threads from a process's task_struct?

别等时光非礼了梦想. 提交于 2020-01-13 17:34:28

问题


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?


回答1:


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.

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


来源:https://stackoverflow.com/questions/8457890/kernel-how-to-find-all-threads-from-a-processs-task-struct

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