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

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

问题描述

什么是标准的方式一个内核模块中获得秒当前时间(从epoch)?

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.

[这不是重复。我已经通过previous问题,看着左右。许多的这些问题的答案要么不指定使用的功能,或者不正确地参阅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

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

其中,结构的timespec 是:

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

是的,你需要的#include< Linux的/ time.h中方式>

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

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