Get current time in seconds in kernel module

无人久伴 提交于 2019-12-28 17:11:39

问题


What is the standard way to get the current time in seconds (since the epoch) in a kernel module?

I have seen techniques involving getting xtime which are very long-winded and involve while-loops and locks. There must be a better way.

[This is not a duplicate. I have looked through previous questions on SO. The answers to many of these either don't specify the function used, or incorrectly refer to time.h which is not allowed in the kernel]


回答1:


You can use getnstimeofday for that.

/* getnstimeofday - Returns the time of day in a timespec */
void getnstimeofday(struct timespec *ts)

where struct timespec is:

struct timespec {
    time_t  tv_sec;     /* seconds */
    long    tv_nsec;    /* nanoseconds */
};

And yes, you'll need #include <linux/time.h>.



来源:https://stackoverflow.com/questions/13552536/get-current-time-in-seconds-in-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!