千家信息网

Linux内核定时器timer_list怎么用

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,Linux内核定时器timer_list怎么用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、简单介绍一下定时器timer_list:
千家信息网最后更新 2026年11月03日Linux内核定时器timer_list怎么用

Linux内核定时器timer_list怎么用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、简单介绍一下定时器timer_list:
1、所在头文件:linux/timer.h
2、结构体:

struct timer_list {/*     * All fields that change during normal runtime grouped to the     * same cacheline     */struct list_head entry;    unsigned long expires;struct tvec_base *base;void (*function)(unsigned long);    unsigned long data;int slack;#ifdef CONFIG_TIMER_STATSint start_pid;void *start_site;char start_comm[16];#endif#ifdef CONFIG_LOCKDEPstruct lockdep_map lockdep_map;#endif};

3、主要成员介绍:
list 实现的时候使用的,和定时器功能无关;
expires 是定时器定时的滴答数(当前的滴答数为jiffies);
void (*function)(unsigned long) 定时器超时处理函数;
data 传递到超时处理函数的参数,主要在多个定时器同时使用时,区别是哪个timer超时。
4、提供的API接口:
a、init_timer(struct timer_list*):定时器初始化函数;
b、add_timer(struct timer_list*):往系统添加定时器;
c、mod_timer(struct timer_list *, unsigned long jiffier_timerout):修改定时器的超时时间为jiffies_timerout;
d、timer_pending(struct timer_list *):定时器状态查询,如果在系统的定时器列表中则返回1,否则返回0;
e、del_timer(struct timer_list*):删除定时器。
5、使用方法:
a、创建定时器时需要先定义struct timer_list my_timer;
b、在file_operation指定的open函数中初始化定时器init_timer(&my_timer);
c、在超时处理函数结尾重新加载定时器时间mod_timer(&my_timer,HZ);
d、如果自己编写的驱动中有中断,需要在中断入口处del_timer(&my_timer);并且在入口处重新重新加载定时器时间mod_timer(&my_timer,HZ)。
二、实例演示:
1、驱动程序代码:

#include #include #include #include                         /*delay*/#include #include #include                          /*kmalloc*/#include                       /*vmalloc*/#include                         /*ssize_t*/#include                            /*file_operaiotns*/#include #include #include #include #include #include #include #include /************硬件相关*************/#include                 /*普通IO*/#define    LED    IMX_GPIO_NR(1,15)          /*SD2_DAT0*/    /*分配内存空间大小*/#define WRITE_MALLOC_SIZE 4096/**主设备号和次设备号**/#define DEVICE_MAJOR 102#define DEVICE_MINOR 0/*缓存区指针,指向内存区*/static char *led_spvm;/*在/sys目录创造一个类*/static struct class *led_class;    /*在这个类下,创造一个设备节点*/static struct cdev *led_class_dev;/*定义定时器结构体*/static struct timer_list timer;   typedef unsigned short int unit;            /*2个字节,16bit*//*超时函数声明*/void mytimeout(void);  /*定时器初始化函数*/void mytimer_init(void){    init_timer(&timer);                               /*初始化定时器*/         timer.expires = jiffies + HZ;                  /*设置超时时间为1S*/   timer.function = mytimeout;                    /*设置超时之后中断服务子程序入口*/add_timer(&timer);                                /*启动定时器*/}/*open函数的实现*/static int led_open(struct inode *inode, struct file *file){/*定时器初始化*/mytimer_init();return 0;}/*release函数的实现*/static int led_close(struct inode *inode, struct file *file){    /*释放占用的资源*/gpio_free(LED);/*删除定时器*/del_timer(&timer);/*打印提示退出信息*/printk(KERN_ALERT"LED is closed!\n");return 0;}/*具体的文件操作集合*/static const struct file_operations led_fops = {/*这是拥有者*/.owner        = THIS_MODULE,    .open        = led_open,    .release     = led_close,};/*超时中断服务子函数*/void mytimeout(void)  {  /*LED闪烁*/__gpio_set_value(LED,1);    mdelay(1000);    __gpio_set_value(LED,0);/*重新设置定时时间为1s*/mod_timer(&timer,jiffies + HZ); }/*驱动的初始化函数*/static int led_init(void){/*设备初始化*/int devno,error;/*设备号的申请,创建*/devno = MKDEV(DEVICE_MAJOR,DEVICE_MINOR);/*分配设备结构体的地址空间*/led_spvm = (char *)vmalloc(WRITE_MALLOC_SIZE);    led_class_dev = cdev_alloc();/*字符设备初始化,绑定相关操作到设备*/cdev_init(led_class_dev,&led_fops);/*设备的拥有者*/led_class_dev->owner = THIS_MODULE;/*添加设备到内核*/cdev_add(led_class_dev,devno,1);/*静态申请设备号*/register_chrdev(DEVICE_MAJOR,"led",&led_fops);    /*创建设备类,用于自动创建设备文件*/led_class = class_create(THIS_MODULE, "led");    /*依据以前创建的设备类,创建设备*/device_create(led_class,NULL,MKDEV(DEVICE_MAJOR,DEVICE_MINOR),NULL,"led");    /*申请gpio*/error = gpio_request(LED,"gpio");if (error < 0)    {        printk(KERN_ALERT"failed to request GPIO LED\n");goto fail1;    }/*设置IO方向为输出*/error = gpio_direction_output(LED,0);if (error < 0)    {        printk(KERN_ALERT"failed to configure direction for LED\n");goto fail2;    }return 0;fail1:return error;fail2:    gpio_free(LED);}/*退出函数*/static void led_exit(void){/*定时器卸载*/     del_timer(&timer);        /*设备卸载*/unregister_chrdev(DEVICE_MAJOR,"led");                                    device_destroy(led_class,MKDEV(DEVICE_MAJOR,DEVICE_MINOR));                            class_destroy(led_class);}/*LICENSE信息*/MODULE_LICENSE("GPL");/*卸载和加载*/module_init(led_init);module_exit(led_exit);

2、测试程序代码:

#include #include #include #include #include int main(int argc,char **argv){    int fd;/*对应加载进去的设备名:dev/led*/fd = open("/dev/led",O_RDWR);/*如果打开设备出错,打印信息*/if (fd < 0)    {printf("can`t open fd_write!\n");    }while(1)    {        sleep(1000);    }/*退出时候,关闭设备*/close(fd);}

看完上述内容,你们掌握Linux内核定时器timer_list怎么用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

定时器 设备 函数 时间 内核 信息 入口 文件 方法 结构 处理 驱动 代码 入口处 内存 内容 拥有者 时候 更多 程序 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网与科技 百单网互联网科技有限公司怎么样 深圳九邦互联网科技有限公司 互联网科技开发公司 互联网公司 科技公司 北京互联网科技有限公司 浩特互联网科技 互联网高科技股票 中国的互联网科技企业的崛起 宝灵互联网科技有限公司 速维有品互联网信息科技(苏州)有限公司 东莞新华互联网科技学校 互联网数据科技专业 为客找房互联网科技公司怎么样 四川互联网展示科技馆设计 广东猫王互联网科技有限公司电话 瑞安市蟠桃会互联网科技有限公司 现代科技的标志之一的互联网 每日互联网科技新闻 广州全民伙伴互联网科技有限公司 互联网科技公司的优势 四川狮子座互联网科技 欧洲十大科技互联网公司 互联网还是高科技吗 互联网科技和信息 深圳育鹏互联网科技有限公司招聘 互联网科技融合创新 萌小萌互联网科技河南有限公司 互联网公司属于科技吗 互联网科技收购价值
0