Three ways for Linux to get thread IDs __linux

Source: Internet
Author: User

I used the second method, very convenient: #define GETTID () syscall (__nr_gettid) used in the Place Gettid () After the linux2.4 version, Linux uses NPTL as its own line threading, in order to be compatible with the POSIX standard, there are two domains Tgid and TID in the kernel task, the process ID and the thread ID. The way to get thread IDs on Linux is three of what I know right now, and of course three of these are in user-state programs, unless you write kernel module, which is a system call that calls number 224 implementation (2.6 version). First: Gettid (), man Gettid can see how gettid is used. Use to define first: _syscall0 (pid_t, Gettid) where _syscall0 is a macro (due to the different parameters and _syscall1,_syscall2 ...), the definition is as follows:
QUOTE:#define _SYSCALL0 (Type,name)/
Type name (void)/long __res; /
__asm__ volatile ("int $0x80"///int 80, soft interrupt
: "=a" (__res)///input and output used EAX
: "0" (__nr_# #name)); ///#define __NR_GETTID 224
__syscall_return (Type,__res); ///return TID compile-time, the macro expands, the equivalent of defining a pid_t gettid (void) function, in the embedded assembly implementation, in the program can use Gettid () to obtain the thread ID. The second type: Syscall (), named Syscall (), is a library function in glibc. Usage: syscall (__nr_gettid), where __nr_gettid is 224, ditto. Syscall to the implementation of the glibc to find, different hardware platforms have different implementation of the version, in the i386 on the implementation of Syscall. S in:
QUOTE:#include
. text
ENTRY (Syscall)
Pushargs_6/* Save register contents. */
_doargs_6/* Load arguments. */
MOVL (%ESP),%eax/* Load syscall number into%eax. */
Enter_kernel/* Do the system call. */
Popargs_6/* Restore register contents. */
Cmpl $-4095,%eax/* Check%eax for error. */
Jae Syscall_error_label/* Jump to error handler if error. */
L (pseudo_end):
RET/* return to caller. */
Pseudo_end (Syscall)
Entry is also a macro that has been expanded quite long to allow GCC to "see" the link and invoke the Syscall () function written in the assembly. The third: Pthread_self () is also a glibc-supplied function, which is said to return the thread IDs of the current threads in Linux manual. But what you actually see is a very long, seemingly irregular value. What is the reason to see its implementation: in GLIBC, Pthread_self () returns the Thread_self, which is another macro definition as follows
QUOTE:# define THREAD_SELF/
({struct pthread *__self;
ASM ("Movl%%gs:%c1,%0": "=r" (__self)/
: "I" (offsetof (struct pthread, header.self)); /
__self;})
This code returns the current thread's descriptor,pthread_self () to get the descriptor address, which is the unsigned long int type of pthread_t. It's a good idea to find out the definition of thread descriptor:
QUOTE:struct Pthread ...
pid_t Tid; }
Next you know how to do it. Calculate the length n, construct a false pthread structure.
QUOTE:struct pthread_fake void *nothing[n];
pid_t Tid; The thread ID is obtained with the (struct Pthread_fake *) pthread_self ()->tid. This is arguably the most tedious thing to do compared to the first two, but the same way you can get a lot of data that is maintained in GLIBC but does not provide access methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.