简体   繁体   English

获取内核模块中的当前时间(秒)

[英]Get current time in seconds in kernel module

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. 我已经看到了涉及获取xtime的技术,这些技术非常冗长且涉及while循环和锁定。 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] 许多这些的答案要么没有指定使用的函数,要么错误地引用内核中不允许的time.h]

You can use getnstimeofday for that. 你可以使用getnstimeofday

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

where struct timespec is: struct timespec是:

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

And yes, you'll need #include <linux/time.h> . 是的,你需要#include <linux/time.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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