如何获取内核线程ID?

Far*_*had 7 linux-kernel

当我们使用创建内核线程时kthread_run(),我们如何获得线程的tid,是否有类似pthread_self()gettid()内核空间?

mya*_*aut 9

在内核空间中,您不需要通过调用来询问有关用户空间中的线程的信息gettid()- 您已经可以访问task_struct任务:

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

  • (过了一会儿)我想通了,如果有几个线程`pid_t tid = current-> pid`将为正在运行的线程做好工作. (4认同)