获取内核模块中的当前时间(以秒为单位) [英] Get current time in seconds in kernel module

查看:28
本文介绍了获取内核模块中的当前时间(以秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 .

这篇关于获取内核模块中的当前时间(以秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆