Call gettid witin glibc

I work in glibc and I need to get the id of the current thread. For this I use syscall(SYS_gettid); Problem: I have to include bits/syscall.h instead of the ideal case ie sys/syscall.h .

sys/syscall.h internally calls bits/syscall.h but ends with the #ifndef _LIBC macro. i.e

  #ifndef _LIBC /* The Linux kernel header file defines macros `__NR_<name>', but some programs expect the traditional form `SYS_<name>'. So in building libc we scan the kernel list and produce <bits/syscall.h> with macros for all the `SYS_' names. */ # include <bits/syscall.h> #endif 

also bits/syscall.h states that "Never use bit / syscall.h directly, use sys / syscall.h instead."

Since _LIBC will be defined in my case, as I write the code directly in malloc.c , please offer me how I can overcome this.

Thanks Kapil

+6
source share
1 answer

gettid () is a system call. As for, as I know, there is no glibc shell for Gettid. You need to call gettid () using syscall (). The following code works for me.

 #include <sys/syscall.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> int main() { long tid; tid = syscall(SYS_gettid); printf("%ld\n", tid); return EXIT_SUCCESS; } 
+16
source

Source: https://habr.com/ru/post/909988/


All Articles