Linux内核定时器--timer_list

简介: Linux内核定时器--timer_list

​

定时器:

linux操作系统提供了一个内核定时器
内核定时器可在未来的某个特定时间点调度执行某个函数,完成指定任务
linux内核定时器提供了一种异步处理的机制
用户通过设置将来某一时刻的滴答值来实现定时功能
假设HZ的值为200,Linux定时器最短定时时间为5ms,小于该时间的定时可直接通过硬件定时器完成

定时器使用过程:
定时器接口函数介绍:

#include <linux/timer.h>

定时器结构体类型

struct timer_list

{

    struct list_head entry;//链表节点,由内核管理

    //定时器到期时间(指定一个时刻)

    unsigned long expires;

    // 定时器处理函数

    void (*function)(unsigned long);

    // 作为参数被传入定时器处理函数

    unsigned long data;

    ......

};

初始化定时器

    void init_timer(struct timer_list *timer);

添加定时器。定时器开始计时

    void add_timer(struct timer_list * timer);

删除定时器,在定时器到期前禁止一个已注册定时器

    int del_timer(struct timer_list * timer);

如果定时器函数正在执行则在函数执行完后返回(SMP)

    int del_timer_sync(struct timer_list *timer);

更新定时器到期时间,并开启定时器

    int mod_timer(struct timer_list *timer,unsigned long expires);

查看定时器是否正在被调度运行

    int timer_pending(const struct timer_list*timer);

    /*返回值为真表示正在被调度运行*/

实例代码:
驱动端:

include <linux/module.h>

include <linux/kernel.h>

include <linux/init.h>

include <linux/fs.h>

include <asm/uaccess.h>

include <linux/device.h>

include <linux/errno.h>

include <linux/timer.h>

include <linux/jiffies.h>

define DRIVER_NAME "timer_test"

define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)

define __debug(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)

static int major = 0;
static int minor = 0;
struct class *timer_class;
struct device *timer_device;

struct timer_list timer;

static void timer_handler(unsigned long dat)
{

static int sec = 0;

sec++;
printk("<kernel>: deal timer handler.... sec = %d\n", sec);        //打印测试信息
/*更新定时器到时时间,并开启定时器*/
mod_timer(&timer, jiffies+1*HZ);            
return;

}

static int timer_test_open(struct inode inode, struct file file)
{

__debug("<kernel>: start timer test...\n");

/* return value: 1 if the timer is pending, 0 if not.*/
if(!timer_pending(&timer)){        //查看定时器是否正在被调度运行
    timer.expires = jiffies + 1*HZ;
    add_timer(&timer);
}
    return 0;

}

static int timer_test_release(struct inode inode, struct file file)
{

del_timer_sync(&timer);
__debug("<kernel>: stop timer test...\n");
    return 0;

}

static struct file_operations timer_test_fops = {

.owner = THIS_MODULE,
.open = timer_test_open,
.release = timer_test_release,

};

static int __init timer_test_init(void)
{

int retval;

//__debug("in key_eint_init\n");


init_timer(&timer);            //初始化定时器
timer.data = 0L;                //设定定时器参数
timer.function = timer_handler;    //设定定时器服务函数


major = register_chrdev(major, DRIVER_NAME, &timer_test_fops);
if(major < 0){
    err("register char device fail!");
    retval = major;
    goto error;
}
timer_class=class_create(THIS_MODULE,DRIVER_NAME);
if(IS_ERR(timer_class)){
    err("class create failed!");
    retval =  PTR_ERR(timer_class);
    goto error_class;
}
timer_device=device_create(timer_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
if(IS_ERR(timer_device)){
    err("device create failed!");
    retval = PTR_ERR(timer_device);
    goto error_device;
}
__debug("register timer test OK! Major = %d\n", major);
return 0;

error_device:

class_destroy(timer_class);

error_class:

unregister_chrdev(major, DRIVER_NAME);

error:

return retval;

}

static void __exit timer_test_exit(void)
{

unregister_chrdev(major, DRIVER_NAME);
device_destroy(timer_class,MKDEV(major, minor));
class_destroy(timer_class);

return;

}

module_init(timer_test_init);
module_exit(timer_test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eric");

[点击并拖拽以移动]

应用层:

include <stdio.h>

include <stdlib.h>

include <fcntl.h>

include <unistd.h>

int main()
{
char *devname = "/dev/timer_test";
int fd;
unsigned int sec;

fd = open(devname, O_RDWR);
if(fd < 0){
printf("file open failed!\n");
return 1;
}
while(1);
close(fd);
}

​

相关文章
|
安全 网络协议 Linux
深入理解Linux内核模块:加载机制、参数传递与实战开发
本文深入解析了Linux内核模块的加载机制、参数传递方式及实战开发技巧。内容涵盖模块基础概念、加载与卸载流程、生命周期管理、参数配置方法,并通过“Hello World”模块和字符设备驱动实例,带领读者逐步掌握模块开发技能。同时,介绍了调试手段、常见问题排查、开发规范及高级特性,如内核线程、模块间通信与性能优化策略。适合希望深入理解Linux内核机制、提升系统编程能力的技术人员阅读与实践。
1270 1
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
Ubuntu Linux
Ubuntu 23.10 现在由Linux内核6.3提供支持
如果你想在你的个人电脑上测试一下Ubuntu 23.10的最新开发快照,你可以从官方下载服务器下载最新的每日构建ISO。然而,请记住,这是一个预发布版本,所以不要在生产机器上使用或安装它。
|
传感器 监控 Ubuntu
10 月发布,Ubuntu 23.10 已升级到 Linux Kernel 6.3 内核
硬件方面,Linux 6.3 引入了在 HID 中引入了原生的 Steam Deck 控制器接口,允许罗技 G923 Xbox 版赛车方向盘在 Linux 上运行;改善 8BitDo Pro 2 有线控制器的行为;并为一系列华硕 Ryzen 主板添加传感器监控。
|
Ubuntu Linux
Ubuntu24.04LTS默认采用Linux 6.8内核,实验性版本可通过PPA获得
IT之家提醒,当下的 Ubuntu 23.10 也是一个“短期支持版本”,该版本将在今年 7 月终止支持,而今年 4 月推出的 Ubuntu 24.04 LTS 长期支持版本将获得 5 年的更新支持。
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
1309 0
|
Web App开发 缓存 Rust
|
Ubuntu 安全 Linux
Ubuntu 发行版更新 Linux 内核,修复 17 个安全漏洞
本地攻击者可以利用上述漏洞,攻击 Ubuntu 22.10、Ubuntu 22.04、Ubuntu 20.04 LTS 发行版,导致拒绝服务(系统崩溃)或执行任意代码。
|
Ubuntu 机器人 物联网
Linux Ubuntu 22.04 LTS 测试版实时内核已可申请
请注意,在启用实时内核后您需要手动配置 grub 以恢复到原始内核。更多内容请参考: