c - 在内核模块中以秒为单位获取当前时间

标签 c time linux-kernel kernel-module

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

我见过涉及获取 xtime 的技术,这些技术非常冗长并且涉及 while 循环和锁。一定有更好的方法。

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

最佳答案

您可以使用 getnstimeofday为此。

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

哪里struct timespec是:

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

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

关于c - 在内核模块中以秒为单位获取当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552536/

相关文章:

c - 程序产生错误的输出

c - 是否可以将变量作为结构成员传递?

c - K&R 中的 "File Copying"部分

c - GNU time 实用程序如何忽略管道符号并将命令行中剩下的所有内容作为一个 execve 参数?

linux - linux中shm.c的修改

c - 我应该如何读取 C 中的逆序字节?

android - 我怎样才能得到 "network"时间,(从 "Automatic"设置称为 "Use network-provided values"),而不是电话上的时间?

java - -System.nanoTime() + System.nanoTime() 是否保证 >= 0?

linux - Linux 中的进程

linux-kernel - 文件结构中的linux内核完整路径