简体   繁体   中英

Where is the kernel time stored in memory in Linux?

For an assignment I have to use a video driver and system timer handler to display the current running time of the Linux system to the corner of the screen.

However, I have not found anywhere that points me into the direction of obtaining the system time from the kernel when my program runs. I am guessing it is in kernel memory at some address and I can just do something like:

hour = get_word(MEM_LOCATION_OF_HOUR);
sec = get_word(MEM_LOCATION_OF_SEC);
ect...

But I cannot find out if this is possible. My guess is that we are not allowed to use library calls like clock() but if that is the only possible way then maybe we are.

Thanks

Can't use library calls? - that's just craziness. Anyway:

getnstimeofday(struct timespec *ts); is one of many methods from here

In kernel, ktime can be used.

a simple example(for calculating time diff) for your reference.

#include <linux/ktime.h>

int fun()
{
ktime_t entry_stamp, now;
s64 delta;

/* Get the current time .*/
entry_stamp = ktime_get();

/* Do your Stuff... */

now = ktime_get();
delta = ktime_to_ns(ktime_sub(now, entry_stamp));
printk(KERN_INFO "Time Taken:%lld ns to execute\n",(long long)delta);
return 0;
}

I have found that the Real-time clock holds the correct values on boot. The CMOS contains all the needed info.

Here is a link to what I found. http://wiki.osdev.org/CMOS#The_Real-Time_Clock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM