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

HXC*_*ine 11 c time kernel-module linux-kernel

在内核模块中以秒(自纪元开始)获取当前时间的标准方法是什么?

我已经看到了涉及获取xtime的技术,这些技术非常冗长且涉及while循环和锁定.肯定有更好的办法.

[这不是重复的.我已经查看了之前的问题.许多这些的答案要么没有指定使用的函数,要么错误地引用内核中不允许的time.h]

cni*_*tar 17

你可以用getnstimeofday它.

/* getnstimeofday - Returns the time of day in a timespec */
void getnstimeofday(struct timespec *ts)
Run Code Online (Sandbox Code Playgroud)

在哪里struct timespec:

struct timespec {
    time_t  tv_sec;     /* seconds */
    long    tv_nsec;    /* nanoseconds */
};
Run Code Online (Sandbox Code Playgroud)

是的,你需要#include <linux/time.h>.