我写了一个程序名test.c。在这个程序中,我通过pthead_create 创建了三个线程。这些线程通过 prctl(PR_SET_NAME, name) 命名为 thread0、thread1、thread2。线程函数代码如下:
void *output(void *arg) {
char *x = (char *) arg;
char name[40] = "thread";
strcat(name, x);
prctl(PR_SET_NAME, name);
while(1){
printf("%s\n", name);
sleep(10000);
};
}
然后我写了一个内核模块print.c,它的功能是打印每个task_struct infomation int内核进程列表。代码如下:
struct task_struct *task = &init_task;
do{
printk("%s\n", task->comm);
}while((task=next_task(task)) != &init_task);
我先正确运行程序test.c,然后正确insmod模块print.ko。没想到没有找到三线程的信息。所以,我想问一下,pthread_create创建的线程不会出现在内核进程列表中。这个观点对不对?
最佳答案
线程在内核中也由struct task
表示。您可以使用例如遍历任务中的每个线程while_each_thread宏:
struct task_struct *task = ...;
struct task_struct *t = task;
do {
//use t
} while_each_thread(task, t);
注意:我不知道围绕您需要能够像这样迭代它们的任务结构的正确锁定(如果有的话)。
关于c - 当我创建一个新的用户空间线程时,它会出现在内核进程列表中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42970390/