如何获取内核线程ID?

How to get a kernel thread ID?

当我们使用kthread_run()创建内核线程时,如何获取线程的tid,内核space中是否有类似pthread_self()gettid()的东西?

在 kernel-space 中,您不需要像在 userspace 中那样通过调用 gettid() 询问有关线程的问题——您已经可以访问 task_struct 你的任务:

struct task_struct* tsk = kthread_run(...);
pid_t tid = tsk->pid; // Thread id of newly created task (if it was successful)