Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a kernel thread ID?

Tags:

linux-kernel

When we create a kernel thread using kthread_run(), how we can get the tid of the thread, is there something like pthread_self() or gettid() in kernel space?

like image 657
Farhad Avatar asked Apr 11 '15 19:04

Farhad


People also ask

How do I find my kernel thread ID?

gettid returns the pid_t (int type) thread ID, which is the pid field of the kernel task_struct structure mentioned above. NPTL thread model ensures that each thread (process) ID is unique and will not conflict. pid_t ttid = syscall(SYS_gettid);

How do I get thread ID?

The pthread_self() function is used to get the ID of the current thread. This function can uniquely identify the existing threads. But if there are multiple threads, and one thread is completed, then that id can be reused. So for all running threads, the ids are unique.

How do I find the thread ID of a process in Linux?

In the GNU C Library implementation running on Linux, the process ID is the thread group ID of all threads in the process. You can get the process ID of a process by calling getpid . The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID).

What is a thread ID?

Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated.


1 Answers

In kernel-space, you don't need to ask something about thread like in userspace you do by calling gettid() -- you already have access to task_struct of your task:

struct task_struct* tsk = kthread_run(...);
pid_t tid = tsk->pid; // Thread id of newly created task (if it was successful)
like image 108
myaut Avatar answered Jan 07 '23 14:01

myaut