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?
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);
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.
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).
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With