2016-09-22 86 views
0

我来到这里与籽粒2.6.18 Linux的麻烦,当我试图让纳秒的时间,像这样:clock_gettime得到ns是0

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int main(void) 
{ 
    struct timespec time_start; 

    clock_gettime(CLOCK_REALTIME, &time_start); 

    printf(CLOCK_REALTIME); 
    printf("\nstart time %llus,%llu ns\n", time_start.tv_sec,time_start.tv_nsec); 

    return 0; 
} 

但是当我运行该脚本,它像打印这样的:

[[email protected] test]# ./time 

start time 1474529199s,506211000 ns 
[[email protected] test]# ./time 

start time 1474529200s,65344000 ns 
[[email protected] test]# ./time 

start time 1474529200s,557196000 ns 

只能得到微秒这里,和NS是0

谁能告诉我,为什么它这样的吗?

+0

我的看法是,并非所有的平台都会实现clock_gettime来返回纳秒,所以你运气不好。 – Bathsheba

回答

1

您可以获得与操作系统相同的精度。在这种情况下,操作系统似乎只能提供微秒级的分辨率,所以你无能为力。

这并不罕见,因为您要求自1970年以来的确切时间(秒)。您可以检查clock()或查找某个可能提供更高分辨率的操作系统相关函数。 (自计算机启动以来,时钟通常会计数)。

这不是内核,它是内核。像“核心”一样。拼错这样的技术术语是很糟糕的形式,因为这里的许多人都是非母语的人,并且可能认为这是正确的,并且错误扩散。