c - 当我创建一个新的用户空间线程时,它会出现在内核进程列表中吗?

标签 c linux multithreading

我写了一个程序名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/

相关文章:

windows - Qt 多头控制台应用程序在退出时挂起(使用 ctrl-c)

c - 从 gcc 和 mingw 中删除不需要的代码

c - 请解释以下并行代码模板

c - fgets() 将\n 提供给后续字符串,而不接受输入

c - 从程序内部调用 gdb 以打印其堆栈跟踪的最佳方法?

linux - 使用 cat 在管道之间弹跳角色

linux - 为什么 php 'date' 函数返回错误的时间(大约 24 秒)?

linux - 什么时候调用setsockopt?在 bind() 和 connect() 之前?

c# - 顶级任务导致错误 "The current SynchronizationContext may not be used as a TaskScheduler."

c++ - C++线程加入后不会中止