当我" strace"以下C程序,time()不会调用系统调用。
#include <time.h>
int main() {
return (int)time(NULL);
}
为什么不呢?它从哪里获得时间?这是Ubuntu 12.04上的完整strace输出。
$ gcc -Wall -o testtime testtime.c && strace ./testtime
execve("./testtime", ["./testtime"], [/* 34 vars */]) = 0
brk(0) = 0x11f1000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd6e052f000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=38828, ...}) = 0
mmap(NULL, 38828, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd6e0525000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\30\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1815224, ...}) = 0
mmap(NULL, 3929304, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fd6dff4f000
mprotect(0x7fd6e0104000, 2097152, PROT_NONE) = 0
mmap(0x7fd6e0304000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b5000) = 0x7fd6e0304000
mmap(0x7fd6e030a000, 17624, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fd6e030a000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd6e0524000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd6e0523000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd6e0522000
arch_prctl(ARCH_SET_FS, 0x7fd6e0523700) = 0
mprotect(0x7fd6e0304000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7fd6e0531000, 4096, PROT_READ) = 0
munmap(0x7fd6e0525000, 38828) = 0
exit_group(1407260210) = ?
答案 0 :(得分:12)
这可能是因为time()
是通过虚拟动态共享对象(VDSO)实现的。这用于提高仅从内核读取少量数据的系统调用的效率。一个典型的例子是gettimeofday()
。
有关详细信息,请参阅LWN的这篇文章 Anatomy of a system call, part 2, gettimeofday():vDSO 。