问题描述
我正在glibc中工作,我需要获取当前线程的ID.为此,我用syscall(SYS_gettid);
问题是,我被迫加入bits/syscall.h
而不是理想情况,即sys/syscall.h
.
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
内部调用bits/syscall.h
,但是用#ifndef _LIBC
宏包装.即
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
也bits/syscall.h
指出 切勿直接使用bits/syscall.h;而应使用sys/syscall.h."
also bits/syscall.h
states that "Never use bits/syscall.h directly; include sys/syscall.h instead."
由于在我的情况下将定义_LIBC
,因为我直接在malloc.c
中编写代码,请提出我该如何克服这个问题.
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.
谢谢,卡皮尔
推荐答案
gettid()是系统调用.就我所知,没有gettid的glibc包装器.您需要使用syscall()调用gettid().以下代码对我有用.
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;
}
这篇关于用glibc打电话给gettid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!