我是靠谱客的博主 甜美煎蛋,最近开发中收集的这篇文章主要介绍内核定时器 jiffies和显示时间,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

jiffies概念:
全局变量jiffies用来记录自系统启动以来产生的节拍的总数。启动时,内核将该变量初始化为0,每次时钟中断处理程序就会增加该变量的值,因此,1S内时钟中断的次数等于HZ。
jiffies变量是一个无符号整型数值,即unsigned long类型。
jiffies转换为秒可采用公式:(jiffies/HZ)计算,将秒转换为jiffies可采用公式:(seconds*HZ)计算。

#include <linux/jiffies.h>
unsigned long timeout  = jiffies + HZ/2;//0.5s后超时
unsigned long timeout  = jiffies + 10*HZ;//10s后超时

1、 定时器数据结构

struct timer_list {
    struct list_head entry;//定时器链表的入口
    unsigned long expires;//以jiffies为单位的定时值
    void (*function)(unsigned long);//定时器处理函数
    unsigned long data;//传给处理函数的长整型参数
    struct tvec_t_base_s *base;//定时器内部值,用户不要使用
};

2、 定义及初始化

struct timer_list my_timer;//定义

init_timer(&my_timer);//初始化

//成员赋值
my_timer.expires = jiffies + delay;//定时器超时时的节拍数
my_timer.data = 0;//给定时器处理函数传入0值
my_timer.functioin = my_function;//定时器超时时调用的函数

//处理函数原型
void my_timer_function(unsigned long data);

3、激活定时器

add_timer(&my_timer);

注意:一般来说,定时器都在超时后立马执行,但是也有可能推迟到下一次时钟节拍时才能运行,所以不能用定时器来实现任何硬实时任务。

参考链接:
https://www.cnblogs.com/chaozhu/p/6183537.html
https://www.cnblogs.com/mewmicro/p/6421254.html?utm_source=itdadao&utm_medium=referral
https://www.cnblogs.com/chaozhu/p/6183537.html

查看时间:

#include <sys/time.h>

struct timeval{
long tv_sec; //秒
long tv_usec; //微秒
};

struct timeval tv;
    gettimeofday(&tv,NULL);
    printf("second:%ldn",tv.tv_sec);  //秒
    printf("millisecond:%ldn",tv.tv_sec*1000 + tv.tv_usec/1000);  //毫秒
    printf("microsecond:%ldn",tv.tv_sec*1000000 + tv.tv_usec);  //微秒


又例如:
struct timeval stTimex;
struct rtc_time stTimeRtc;

do_gettimeofday(&stTimex);
rtc_time_to_tm(stTimex.tv_sec, &stTimeRtc);//时间转换

printk("rn %d-%d-%d %d:%d:%d", stTimeRtc.tm_year + 1900, stTimeRtc.tm_mon + 1, stTimeRtc.tm_mday, stTimeRtc.tm_hour, stTimeRtc.tm_min, stTimeRtc.tm_sec);

参考链接:https://blog.csdn.net/deyuzhi/article/details/51814934

最后

以上就是甜美煎蛋为你收集整理的内核定时器 jiffies和显示时间的全部内容,希望文章能够帮你解决内核定时器 jiffies和显示时间所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部