Reputation: 1
I've got a trouble here in linux with kernel 2.6.18 when i try to get ns time,like this:
#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;
}
but when I run this script,It print like this:
[root@master test]# ./time
start time 1474529199s,506211000 ns
[root@master test]# ./time
start time 1474529200s,65344000 ns
[root@master test]# ./time
start time 1474529200s,557196000 ns
can only get μs here,and ns is 0
Can someone tell me why it does like this?
Upvotes: 0
Views: 903
Reputation: 52602
You get as much precision as the operating system provides. In this case, it seems that the operating system only provides microsecond resolution, so there is nothing you can do about it.
That's not unusual since you are asking for the exact time in seconds since 1970. You might check either clock () or look for some OS dependent function that might provide higher resolution. (clock usually counts since the computer was started).
And it's not kernal, it's kernel. Like "core". It's bad form to spell technical terms like this wrong, because many people here are non-native speakers and might think it's correct and the error spreads.
Upvotes: 1