实验环境
Kernel Version: 4.15.0-66-generic
Operating System: Ubuntu 18.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.947GiB
测试代码
创建一个 1M 的文件
dd if=/dev/zero of=in.txt bs=1M count=1
系统调用测试代码
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int in;
int i;
in = open("in.txt", O_RDONLY);
for(i=0; i<1000000; i++){ // 一百万次系统调用
read(in,&c,1);
}
return 0;
}
时间统计
root@aliyun:c-code# time ./read-syscall
real 0m0.746s
user 0m0.388s
sys 0m0.340s
由上,一百万次 read
系统调用,耗时 0.340s
。也就是一次系统调用是 0.34
微秒,也就是 340
纳秒。一次系统调用大概是几百纳秒。