问题
I am working in glibc and I need to get the id of the current thread. For this i use
syscall(SYS_gettid);
Issue is, i am forced to include bits/syscall.h
instead of ideal case i.e sys/syscall.h
.
sys/syscall.h
internally calls bits/syscall.h
but that is wrapped with #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's 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 bits/syscall.h directly; include sys/syscall.h instead."
Since _LIBC
will be defined in my case as i am writing code directly in malloc.c
,
please suggest my how can i overcome this.
Thanks, Kapil
回答1:
gettid() is a system call. As for as I know there is no glibc wrapper for gettid. You need to invoke 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;
}
来源:https://stackoverflow.com/questions/9565177/call-gettid-witin-glibc