一个简单的程序是:我想使用这个gettid函数获取两个线程的线程ID.我不想直接做sysCall.我想使用这个功能.
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/date_time/date.hpp>
#include <unistd.h>
#include <sys/types.h>
using namespace boost;
using namespace std;
boost::thread thread_obj;
boost::thread thread_obj1;
void func(void)
{
char x;
cout << "enter y to interrupt" << endl;
cin >> x;
pid_t tid = gettid();
cout << "tid:" << tid << endl;
if (x == 'y') {
cout << "x = 'y'" << endl;
cout << "thread interrupt" << endl;
}
}
void real_main() {
cout << "real main thread" << endl;
pid_t tid …Run Code Online (Sandbox Code Playgroud) 我在glibc工作,我需要得到当前线程的id.为此我使用
syscall(SYS_gettid);问题是,我被迫包括bits/syscall.h而不是理想的情况即 sys/syscall.h.
sys/syscall.h 内部调用bits/syscall.h但是用#ifndef _LIBC宏包装.即
#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
Run Code Online (Sandbox Code Playgroud)
还bits/syscall.h指出"永远不要直接使用bits/syscall.h;而是包含sys/syscall.h".
由于_LIBC我将在我的情况下定义,因为我正在直接编写代码malloc.c,请建议我如何克服这个问题.
谢谢,卡皮尔
让我们以gettid系统调用为例:
http ://man7.org/linux/man-pages/man2/gettid.2.html
我知道gettidlibc 中没有实现,我需要直接进行系统调用才能使用它(syscall(SYS_gettid))。我自己用这个 C 代码验证了这一点:
#include <stdio.h>
#include <sys/types.h>
int main(){
pid_t a = gettid();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不链接并在编译时给出此警告:warning: implicit declaration of function 'gettid'; did you mean 'getline'。
现在我的问题是,为什么 Linux 文档记录它就好像这个函数确实存在一样?
SYNOPSIS
#include <sys/types.h>
pid_t gettid(void);
Run Code Online (Sandbox Code Playgroud)
他们没有如何进行直接系统调用的示例,而是有上述不存在且无法使用的代码片段。我有什么遗漏的吗?
我正在阅读 linux功能测试宏页面,并在注释部分指出:
<features.h> 是 Linux/glibc 特定的头文件。其他系统也有类似的文件,但通常具有不同的名称。该头文件会根据需要自动包含在其他头文件中:无需显式包含它即可使用功能测试宏。
我知道其他系统不会具有与 features.h 相同的文件名,因此您不应直接包含它。但是,如果不包含头文件,如何知道是否可以使用功能测试宏?
system-calls ×3
c ×2
linux ×2
linux-kernel ×2
boost-thread ×1
c++11 ×1
c99 ×1
glibc ×1
header-files ×1
libc ×1
macros ×1
malloc ×1