Any difference between clock_gettime (CLOCK_REALTIME ....) and time ()?

A simple question: do time(...)and clock_gettime( CLOCK_REALTIME, ... )produce theoretical time (only in relation to seconds)?


Here is what I mean:

time_t epoch;
time( &epoch );

and

struct timespec spec;
clock_gettime( CLOCK_REALTIME, &spec );

Are these two people supposed to return exactly the same result (in relation to seconds)?

I “tested” it with changing time and time zones, epochand I spec.tv_secalways showed the same result, but the documentation CLOCK_REATIMEconfuses me a little, and I'm not sure that they will always be the same.


: , time. ( spec.tv_nsec, 1000000). time clock_gettime, , .


- Linux - getrusage vs clock_gettime vs gettimeofday vs timespec_get?, .. .

+4
1

[: git v4.7 , x86, .]

time() syscall, get_seconds, kernel/time/time.c. syscall get_seconds, UNIX, , CLOCK_REALTIME "(xtime_sec).

clock_gettime() glibc sysdeps\unix\clock_gettime.c, gettimeofday, CLOCK_REALTIME, syscall ( time.c, ). do_gettimeofday, __getnstimeofday64, ... xtime_sec , .

Update:

@MaximEgorushkin, vDSO ( , linux-vdso.so.*), clock_gettime __vdso_clock_gettime. (gtod - ). do_realtime, struct vsyscall_gtod_data wall_time_sec. update_vsyscall, -, .

TL;DR

: , .

+5

Source: https://habr.com/ru/post/1651036/


All Articles