Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't time() invoke a system call?

When I "strace" the following C program, time() does not invoke a system call.

#include <time.h>
int main() {
  return (int)time(NULL);
}

Why not? From where does it get the time of day? Here is the full strace output on Ubuntu 12.04.

$ 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)                  = ?
like image 637
Andrew Chi Avatar asked Aug 05 '14 18:08

Andrew Chi


1 Answers

This is probably because time() is implemented through a Virtual Dynamic Shared Object (VDSO). This is used to improve the efficiency of the system calls that just read a tiny amount of data from the kernel. A typical example is gettimeofday().

For more information, you can refer to this article from LWN Anatomy of a system call, part 2, section gettimeofday(): vDSO.

like image 166
Christophe Vu-Brugier Avatar answered Oct 29 '22 07:10

Christophe Vu-Brugier