Is there any way to get the cuurent time using C in LINUX. I tried with
clock_get time(). But i'm getting the same time for a particular amout of function call. The code i've used is given below. Is there any way to get the time in an higher resolution.

write()
{
struct timespec time0;
clock_gettime(CLOCK_REALTIME,&time0);
printf("writetime=%lu",time0.tv_nsec);
}
 
main()
{
 ......
for(i=0;i<15;i++)
   write();
 
....
}

Thank You

Dani AI

Generated

A few practical diagnostics and fixes for the symptom reported by (consecutive calls returning the same timestamp).

First, check what resolution the kernel actually provides for the clock you are using. Querying the clock resolution lets you know whether consecutive calls in a tight loop can return identical values:

struct timespec r;
if (clock_getres(CLOCK_MONOTONIC_RAW, &r) == 0)
    printf("resolution: %ld s %ld ns\n", r.tv_sec, r.tv_nsec);

Prefer a monotonic clock for interval timing (CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW) rather than the adjustable wall clock; raw gives a hardware-backed value without NTP adjustments. When printing timestamps, combine seconds and nanoseconds into a single 64-bit value so comparisons are correct and you are not misled by printing only tv_nsec:

struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
long long ns = (long long)ts.tv_sec * 1000000000LL + ts.tv_nsec;
printf("%lld\n", ns);

Other common causes to rule out: output buffering (add a newline or call fflush(stdout) to see each value immediately), naming a function write() which shadows the system call, and using an incorrect printf format for the type you print. On some virtual machines or older kernels the chosen clocksource has coarse granularity — inspect /sys/devices/system/clocksource/clocksource0/ to see the active clocksource.

For background reading on clocks, resolution and available clock IDs, see the Linux manual pages for clock_gettime and time(7): clock_gettime(2) and time(7). As suggested, kernel documentation and man pages are the right next step if you need deeper, platform-specific details.

Is there any way to get the cuurent time using C in LINUX. I tried with
clock_get time(). But i'm getting the same time for a particular amout of function call. The code i've used is given below. Is there any way to get the time in an higher resolution.


write()
{
struct timespec time0;
clock_gettime(CLOCK_REALTIME,&time0);
printf("writetime=%lu",_nsec);
}

main()
{
......
for(i=0;i<15;i++)
write();

....
}

Thank You

Here is a list of Linux system calls, you might find something in there: http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html

Also, IF you're a Linux enthusiast I'd recommend getting a good kernel programming book. I've read "Linux Kernel Programming" by by Michael Beck, Harald Bohme, Mirko Dziadzka, and Ulrich Kunitz. Here is a link to the book:
http://www.amazon.com/s/ref=nb_ss_gw/104-1800642-4107953?url=search-alias%3Dstripbooks&field-keywords=Linux+Kernel+Programming&Go.x=8&Go.y=13&Go=Go

Good luck, LamaBot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.