在内核模块中以秒为单位(自纪元以来)获取当前时间的标准方法是什么?
What is the standard way to get the current time in seconds (since the epoch) in a kernel module?
我见过涉及获取 xtime 的技术,这些技术非常冗长,涉及 while 循环和锁.一定有更好的方法.
I have seen techniques involving getting xtime which are very long-winded and involve while-loops and locks. There must be a better way.
[这不是重复的.我已经浏览了以前关于 SO 的问题.其中许多的答案要么没有指定使用的函数,要么错误地引用了内核中不允许的 time.h]
[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]
推荐答案你可以使用 getnstimeofday 来解决这个问题.
You can use getnstimeofday for that.
/* 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 .
更多推荐
获取内核模块中的当前时间(以秒为单位)
发布评论