Three methods to obtain the thread ID in Linux

Source: Internet
Author: User

I used the second method, which is very convenient: # define gettid () syscall (_ nr_gettid) Where gettid () is used after linux2.4, linux uses nptl as its own thread library. to be compatible with the POSIX standard, there are two domain tgid and TID in the kernel task. The former is the process ID and the latter is the thread ID. I know three methods to obtain the thread ID in Linux. Of course, the three methods here refer to user-state programs. Otherwise, unless you write your own kernel module, all are implemented by calling the system call number 224 (version 2.6 ). First: gettid (), man gettid, you can see how to use gettid. For use, you must first define _ syscall0 (pid_t, gettid). _ syscall0 is a macro (_ syscall1, _ syscall2. ..), which is defined as follows:
Quote:# DEFINE _ syscall0 (type, name )/
Type name (void)/long _ res ;/
_ ASM _ volatile ("int $0x80" // int 80, Soft Interrupt
: "= A" (_ res) // eax used for Input and Output
: "0" (_ nR _ # Name); // # DEFINE _ nr_gettid 224
_ Syscall_return (type ,__ res); // when the TID is returned for compilation, after the macro is expanded, A pid_t gettid (void) function is defined and implemented using Embedded Assembly, in the program, you can use gettid () to obtain the thread ID. The second type is syscall (), which is called syscall (). It is a library function in glibc. Usage: syscall (_ nr_gettid), where _ nr_gettid is 224, same as above. The implementation of syscall should be found in glibc. Different hardware platforms have different implementation versions. The implementation of i386 is in syscall. S:
Quote:# Include
. Text
Entry (syscall)
Pushargs_6/* save register contents .*/
_ Doargs_6 (44)/* load arguments .*/
Movl 20 (% 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)
The entry is also a macro, Which is expanded quite long. It is mainly used to enable GCC to "see" and call the syscall () function written in assembly. Third: pthread_self () is also a function provided by glibc. In Manual of Linux, the returned thread ID is the thread ID of the current thread. but what you actually see is a long, irregular value. Why should we check its implementation: in glibc, pthread_self () returns thread_self, which is a macro defined 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 descriptor of the current thread. pthread_self () obtains the address of the descriptor, that is, pthread_t of the unsigned long int type. You can understand this point and find the definition of thread descriptor:
Quote:Struct pthread...
Pid_t tid ;}
Do you know what to do next? Calculate the length N to construct a false pthread structure.
Quote:Struct pthread_fake void * nothing [N];
Pid_t tid; Use (struct pthread_fake *) pthread_self ()-> TID to get the thread ID. Compared with the first two methods, this is undoubtedly the most tedious, but likewise, we can obtain a lot of data maintained in glibc, but it 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.