首页 > 其他分享 >timer_list

timer_list

时间:2024-03-25 21:45:22浏览次数:14  
标签:定时器 struct demo void list timer

参考资料: 《正点原子Linux驱动教程》   定时器是一个很常用的功能,需要周期性处理的工作都要用到定时器。Linux 内核定时器采用系统时钟来实现,也就是arch timer。Linux 内核使用 timer_list 结构体表示内核定时器,timer_list 定义在文件 include/linux/timer.h 中,定义如下:

struct timer_list {
     struct list_head entry;
     unsigned long expires; 
    /* 定时器超时时间,单位是节拍数 */
     struct tvec_base *base;
     void (*function)(unsigned long); /* 定时处理函数 */
     unsigned long data; 
    /* 要传递给 function 函数的参数 */
     int slack;
};
要使用内核定时器首先要先定义一个 timer_list 变量,表示定时器,tiemr_list 结构体的expires 成员变量表示超时时间,单位为节拍数。比如需要定义一个周期为 2 秒的定时器,那么这个定时器的超时时间就是 jiffies+(2*HZ),因此 expires=jiffies+(2*HZ)。function 就是定时器超时以后的定时处理函数,我们要做的工作就放到这个函数里面,需要我们编写这个定时处理函数。   timer_list的系列API:
void init_timer(struct timer_list *timer)                        // 初始化timer_list类型变量
void add_timer(struct timer_list *timer)                         // 向Linux内核注册定时器
int del_timer(struct timer_list * timer)                         // 用于删除一个定时器
int mod_timer(struct timer_list *timer, unsigned long expires)   // 用于修改定时值
  测试demo:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/timer.h>

static struct timer_list my_timer;

static void my_timer_callback(struct timer_list *t)
{
    printk("Timer callback function is called!\n");
    // 在这里可以执行定时器触发时需要进行的操作
}

static int __init timer_demo_init(void)
{
    printk("Initializing timer demo\n");

    // 初始化定时器
    timer_setup(&my_timer, my_timer_callback, 0);

    // 设置定时器触发时间为当前时间后的5秒
    mod_timer(&my_timer, jiffies + msecs_to_jiffies(5000));

    return 0;
}

static void __exit timer_demo_exit(void)
{
    del_timer(&my_timer);
    printk("Exiting timer demo\n");
}

module_init(timer_demo_init);
module_exit(timer_demo_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("lethe1203");
MODULE_DESCRIPTION("timer demo");

 

 

 

标签:定时器,struct,demo,void,list,timer
From: https://www.cnblogs.com/lethe1203/p/18095427

相关文章

  • Alist聚合网盘
    Alist是一款支持多种存储服务的文件列表程序,它允许用户将文件保存在本地存储、阿里云盘、OneDrive、GoogleDrive等多个平台上。对于需要将大文件分享且受限于免费云存储服务大小限制的用户来说,Alist提供了一个理想的解决方案,使其成为私人轻量级网盘分享应用的绝佳选择。生......
  • DataTable 转为 List<dynamic>
     staticList<dynamic>DataTableToListDynamic(DataTabledt){List<dynamic>dynamicList=newList<dynamic>();foreach(DataRowrowindt.Rows){dynamicdynamicObj=newExpandoObject();varexpandoDi......
  • QTimer
    1)QTimer定时器需要定义对象。定时器开启后,规定时间内不断触发的是timeout信号,不想触发就关闭定时器,一般QTimer的开启关闭都是配合按钮来进行。相关代码操作://1定义定时器对象QTimerTimer;//2定时器开启Timer.start(1000);//3触发的是timeout信号//4关闭......
  • C# list删除报错 使用for循环倒序删除
    C#list删除报错在C#中,如果您在遍历List的同时尝试删除元素,可能会遇到错误。因为这会改变List的结构,导致枚举器失效。解决方法:使用for循环倒序删除:点击查看代码for(inti=list.Count-1;i>=0;i--){if(/*条件*/){list.RemoveAt(i);}}......
  • Program-List of 32th
    namefunctionfrommemodateoeis02.pyoeis解析OEIS爬虫 20240324                                             ......
  • std::vector 和 std::list 区别
    std::vector和std::list区别?std::vector和std::list是C++标准库中两种不同的容器类型,它们之间有以下几个主要区别:存储结构:std::vector是连续内存空间上的动态数组,元素在内存中是连续存储的。std::list是基于双向链表实现的,元素在内存中是非连续存储的。......
  • Collections工具类,可以使用collections工具类对代码中的list进行分组
    /***根据活动id进行分组*key活动id*value活动id对应的商品id*/Map<Long,Set<Long>>collect=activitySkuList.stream().collect(Collectors.groupingBy(ActivitySku::getActivityId......
  • Java学习笔记:ArrayList集合
    目录为什么要有集合:解决数组自动扩容的问题Java、python数据类型对比Java支持的数据类型主要分为两大类:Python支持多种数据类型,主要包括以下几种:在Java中常见的数据类型实现方式:Java通过使用集合框架来解决一组数据的存储和管理Java集合大致也可分成List、Set、Queue、Map四种接口......
  • 中考英语首字母快速突破014-2021上海徐汇英语二模-The Glamorous Life of TV Journali
    中考英语首字母快速突破014-2021上海徐汇英语二模-TheGlamorousLifeofTVJournalists-电视记者的风光生活PDF格式公众号回复关键字:ZKSZM014原文​HundredsofthousandsofpeopleoftenseetheirfacesontheTVscreen.Theymaybespeaking“live”from......
  • Imagen: Photorealistic Text-to-Image Diffusion Models with Deep Language Underst
    名称Imagen:PhotorealisticText-to-ImageDiffusionModelswithDeepLanguageUnderstanding时间:22/05机构:GoogleTL;DR发现使用LLM(T5)可以作为text2image任务的textencoder,并且提升LLM模型size相对于提升imageDM模型size性价比更高,生成的图像保真度更高,内容也更符合文......