本文介绍了clock_gettime(CLOCK_REALTIME ....)和time()之间的任何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题:do time(...)和 clock_gettime(CLOCK_REALTIME,...)

A simple question: do time(...) and clock_gettime( CLOCK_REALTIME, ... ) produce the same time theoretically (in respect to seconds only)?

这是我的意思:

time_t epoch;
time( &epoch );

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

这两个是否应该返回完全相同的结果

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

我通过改变时间和时区以及纪元和 tv_sec 始终显示相同的结果,但是 CLOCK_REATIME 的文档令我困惑,我不确定,他们将永远是相同的。

I "tested" this with changing time and time zones and epoch and spec.tv_sec always show the same result, but the documentation of CLOCK_REATIME confuses me a bit and I'm not sure, that they will always be the same.

现实情况:我有一段代码,使用时间。现在我想有毫秒的时间(可以从 spec.tv_nsec ,乘以1000000)。所以我想删除时间并直接使用 clock_gettime ,但我不知道这将保持不变所有情况。

Real world situation: I have a piece of code, which uses time. Now I want to have the time in milliseconds (which can be taken from spec.tv_nsec, multiplied by 1000000). So I think about removing time and using directly clock_gettime, but I'm not sure if this will remain the same in all situations.

推荐答案

[Note :

time(我使用的是git master分支,v4.7用于下面的引用链接, )实际上是同样命名的syscall的别名,它调用 get_seconds ,发生在。该系统调用使用函数返回从核心时间结构读取的UNIX时间戳,更准确地说,从Current CLOCK_REALTIME time in seconds字段( xtime_sec )

time() is in fact an alias for the equally named syscall, which calls get_seconds, happens at kernel/time/time.c. That syscall uses the get_seconds function to return the UNIX timestamp, which is read from the core timekeeping struct, more precisely from the "Current CLOCK_REALTIME time in seconds" field (xtime_sec).

clock_gettime()是中的glibc函数sysdeps\unix \ clock_gettime.c ,如果提供的时钟ID为 CLOCK_REALTIME ,它只需调用 gettimeofday 再次支持同样命名的系统调用(源是在同一个 time.c 文件,上面)。此电话号码会调用,最终最终调用,它查询与上述相同结构体的 xtime_sec 字段。

clock_gettime() is a glibc function in sysdeps\unix\clock_gettime.c, which simply calls gettimeofday if the supplied clock ID is CLOCK_REALTIME, which is again backed by the equally named syscall (source is in the same time.c file, above). This one calls do_gettimeofday, which eventually ends up calling __getnstimeofday64, that queries... the very same xtime_sec field from the same struct as above.

更新:

正如@MaximEgorushkin聪明地指出,一个新的vDSO机制劫持(一个好兆头,二进制取决于 linux-vdso.so。) clock_gettime 调用并将其重定向到 __vdso_clock_gettime 。这一个使用了一个新的时钟源管理框架(gtod - ,来自与上述相同的计时器结构。

As @MaximEgorushkin cleverly pointed out, a new vDSO mechanism hijacks (a good sign it is present, if your binary depends on linux-vdso.so.*) the clock_gettime call and redirects it to __vdso_clock_gettime. This one uses a new clock source management framework (gtod - Generic Time Of Day). A call to do_realtime, and it reads from a structure, struct vsyscall_gtod_data's wall_time_sec field. This structure is maintained by update_vsyscall, from the same timekeeper struct as the above.

答案是:,他们从同一个时钟源获取时间。

The answer is: yes, they get the time from the same clock source.

这篇关于clock_gettime(CLOCK_REALTIME ....)和time()之间的任何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 10:25