1. 概念
syscall()是一个小的库函数,它调用系统调用,该系统调用的汇编语言接口具有指定的带指定参数的数字。
syscall()在进行系统调用之前保存CPU寄存器,从系统调用返回时恢复寄存器,如果发生错误,将系统调用返回的任何错误代码存储在errno。
2. 函数定义
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/syscall.h> /* For SYS_xxx definitions */
int syscall(int number, ...);
3. 返回值
成功返回0,失败返回-1。
4. 参数
syscall() 执行一个系统调用,根据指定的参数number和所有系统调用的汇编语言接口来确定调用哪个系统调用。
系统调用所使用的符号常量可以在头文件<sys/syscll.h>里面找到。
5. 示例
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
pid_t tid;
tid = syscall(SYS_gettid);
printf("tid:%d\n",tid);
tid = syscall(SYS_tgkill, getpid(), tid, SIGHUP);
printf("tid:%d\n",tid);
return 0;
}
编译运行:
[root@192 syscall]# gcc syscall.c -o syscall
[root@192 syscall]# ls
syscall syscall.c
[root@192 syscall]# ./syscall
tid:1811
挂起
[root@192 syscall]#