linux内核定时器编程

1.linux内核定时器基本结构和函数

1)struct timer_list 一个struct timer_list对应了一个定时器。

#include <linux/timer.h>

以下列出常用的接口:

struct timer_list{/*....*/unsigned long expires;//定时器服务函数开始执行时间void (*function)(unsigned long);//定义一个指向定时器服务函数的指针function,服务函数有一个 unsigned long的参数,并且返回voidunsigned long data;//定时时间到时,data参数会传入服务函数}void init_timer(struct timer_list* timer)//初始化一个定时器

 

 

-----------使用定时器的步骤--------------
struct timer_list  my_timer_list;//定义一个定时器,可以把它放在你的设备结构中
init_timer(&my_timer_list);//初始化一个定时器
my_timer_list.expire=jiffies+HZ;//定时器1s后运行服务程序
my_timer_list.function=timer_function;//定时器服务函数
add_timer(&my_timer_list);//添加定时器
void timer_function(unsigned long);//写定时器服务函数
del_timer(&my_timer_list);//当定时器不再需要时删除定时器
del_timer_sync(&my_timer_list);//基本和del_timer一样,比较适合在多核处理器使用,一般推荐使用del_timer_sync

 

-------------------------------------------------

2.以下是一个定时1s的驱动程序,直接上代码

/*****************************************************************
原子操作,就是不能被更高等级中断抢夺优先的操作。你既然提这个问题,我就说深一点。
由于操作系统大部分时间处于开中断状态,所以,一个程序在执行的时候可能被优先级更高的线程中断。
而有些操作是不能被中断的,不然会出现无法还原的后果,这时候,这些操作就需要原子操作。就是不能被中断的操作。1.atomic_read与atomic_set函数是原子变量的操作,就是原子读和原子设置的作用.
2.原子操作,就是执行操作的时候,其数值不会被其它线程或者中断所影响
3.原子操作是linux内核中一种同步的方式
typedef struct { volatile int counter; } atomic_t;#define ATOMIC_INIT(i)        { (i) }#define atomic_read(v)                ((v)->counter)
#define atomic_set(v,i)                (((v)->counter) = (i))
******************************************************************/  
#include <linux/miscdevice.h>  
#include <linux/delay.h>  
#include <asm/irq.h>  
//#include <mach/regs-gpio.h>  
//#include <mach/hardware.h>  
#include <linux/kernel.h>  
#include <linux/module.h>  
#include <linux/init.h>  
#include <linux/mm.h>  
#include <linux/fs.h>  
#include <linux/types.h>  
#include <linux/delay.h>  
#include <linux/moduleparam.h>  
#include <linux/slab.h>  
#include <linux/errno.h>  
#include <linux/ioctl.h>  
#include <linux/cdev.h>  
#include <linux/string.h>  
#include <linux/list.h>  
#include <linux/pci.h>  
#include <asm/uaccess.h>  
#include <asm/atomic.h>  
#include <asm/unistd.h>  
#include <asm/io.h>  
#include <asm/system.h>  
#include <asm/uaccess.h>  
#define TIMER_MAJOR 300  
#define TIMER_MINOR 0  
dev_t timer_dev_t;//设备号  
dev_t timer_dev_major=TIMER_MAJOR;  
dev_t timer_dev_minor=TIMER_MINOR;  
struct TIMER_DEV  
{  struct cdev cdev;  atomic_t count;  struct timer_list timer_list;  };  
struct TIMER_DEV* timer_dev;  
//---------timer interrupt function----------------  
static void timer_function(unsigned long data)  
{  mod_timer(&(timer_dev->timer_list),jiffies+HZ);//重新设置时间  printk("current jiffies is %ld,count=%d\n",jiffies,timer_dev->count);  //(timer_dev->count)++;  atomic_inc(&(timer_dev->count));  
}  
//--------timer release function--------------------  
static int timer_release(struct inode* inode, struct file* filp)  
{  del_timer_sync(&(timer_dev->timer_list));  return 0;  
}  //----------------file open function-----------------  
static int timer_open(struct inode* inode,struct file* filp)  
{  init_timer(&(timer_dev->timer_list));//初始化定时器  timer_dev->timer_list.function=timer_function;//设置定时器处理函数  timer_dev->timer_list.expires=jiffies+HZ;//处理函数1s后运行  add_timer(&timer_dev->timer_list);//添加定时器  atomic_set(&(timer_dev->count),0); //原子操作函数return 0;  
}  
//--------------------------------------  //----------------timer_read function---------------  
static int timer_read(struct file* filp,char __user *buf,size_t count,loff_t* f_pos)  
{  unsigned int counter=atomic_read(&(timer_dev->count));  if(copy_to_user(buf,(unsigned int*)&counter,sizeof(unsigned int)))  {  printk("copy to user error\n");  goto out;  }  return (sizeof(unsigned int));  out:  return (-EFAULT);  }  struct file_operations timer_ops={  .owner=THIS_MODULE,  .open=timer_open,  .read=timer_read,  .release=timer_release,  
};  
static int __init timer_init(void)  
{  int ret;  if(TIMER_MAJOR)//主设备号大于0,静态申请设备号  {  timer_dev_t=MKDEV(TIMER_MAJOR,TIMER_MINOR);  ret=register_chrdev_region(TIMER_MAJOR,1,"timer_dev");//first,count,name  }  else  {  ret=alloc_chrdev_region(&timer_dev_t,0,1,"time_dev");  timer_dev_major=MAJOR(timer_dev_t);  }  if(ret<0)  {  printk("can't get major %d\n",timer_dev_major);  return ret;  }  
//-----------------------------------------------------------  timer_dev=kmalloc(sizeof(struct TIMER_DEV),GFP_KERNEL);   memset(timer_dev,0,sizeof(struct TIMER_DEV));  cdev_init(&(timer_dev->cdev),&timer_ops); //linux字符设备的注册cdev_add(&(timer_dev->cdev),timer_dev_t,1);  //添加字符设备printk("init timer_dev success\n");  
return 0;  }  
static void __exit timer_exit(void)  
{  kfree(timer_dev);  cdev_del(&(timer_dev->cdev));  unregister_chrdev_region(MKDEV(TIMER_MAJOR,0),1);  
}  
module_init(timer_init);  
module_exit(timer_exit); 

 

 

测试程序

[cpp] view plaincopy

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <fcntl.h>  
  5. int main()  
  6. {  
  7. int fd;  
  8. int i;  
  9. int counter=0;  
  10. int old_counter=0;  
  11. fd=open("/dev/timer_dev",O_RDWR);  
  12. if(fd<0)  
  13. {  
  14. printf("open file error\n");  
  15. exit(1);  
  16. }  
  17. for(i=0;i<30;)//运行30s  
  18.   {  
  19.   read(fd,&counter,sizeof(int));  
  20.  if(counter!=old_counter)  
  21.     {  
  22.      printf("second=%d\n",counter);  
  23.      old_counter=counter;  
  24.     i++;  
  25.     }  
  26.   
  27.   }  
  28. close(fd);  
  29. exit (0);  
  30.   
  31. }  


测试结果 添加模块后使用dmesg查看内核信息

[ 1239.176994] current jiffies is 235468,count=123
[ 1240.174459] current jiffies is 235718,count=124
[ 1241.171920] current jiffies is 235968,count=125
[ 1242.169383] current jiffies is 236218,count=126

测试程序的结果

second=1
second=2
second=3
second=4
second=5
second=6
second=7
second=8
second=9
second=10
second=11

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/469600.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

django ModuleNotFoundError: No module named 'tinymce***'

django ModuleNotFoundError: No module named ‘***’ 1、检查对应的模块是否有安装&#xff0c;可以使用pip list查看 没有安装请执行安装 python -m pip install *** (--user)&#xff0c;某些电脑user没有权限需要加上括号中的 2、如果有安装 请检查python的django配置安…

度量时间差和jiffies计数器

HZ 1、内核通过定时器中断来跟踪时间流 2、时钟中断由系统定时硬件以周期性的间隔产生&#xff0c;这个间隔由内核根据HZ的值设定&#xff0c;HZ是一个与体系结构有关的常数&#xff0c;定义在<linux/param.h>或者该 文件包含的某个子平台相关的文件中。 jiffies 1、…

智慧交通day04-特定目标车辆追踪03:siamese在目标跟踪中的应用-SiamRPN(2017)

3.2 SiamRPN(2017) 3.2.1 网络结构 Siam-RPN提出了一种基于RPN的孪生网络结构&#xff0c;由孪生子网络和RPN网络组成&#xff0c;前者用来提取特征&#xff0c;后者用来产生候选区域。其中&#xff0c;RPN子网络由两个分支组成&#xff0c;一个是用来区分目标和背景的分类分…

点绛唇-王禹偁

diǎn jinɡ chn ɡǎn xnɡ 点 绛 唇 感 兴 wnɡ yǔ chēnɡ 王 禹 偁 yǔ hn yn chu &#xff0c; jiānɡ nn yī ji chēnɡ jiā l 。 雨 恨 云 愁 &#xff0c; 江 南 依 旧 称 佳 丽 。 shuǐ cūn y sh &#xff0c; y lǚ ɡū yān x…

ubuntu 18 Cannot find installed version of python-django or python3-django.

ubuntu系统下安装了django&#xff0c;但是启动django项目时报错 Cannot find installed version of python-django or python3-django. 原因&#xff1a; ubuntu大于14版本的应该安装python3-django 解决办法&#xff1a; apt-get install python3-django 如果提示你有几个…

智慧交通day04-特定目标车辆追踪03:siamese在目标跟踪中的应用-DaSiamRPN(2018)

DaSiamRPN网络的全称为Distractor-aware SiamRPN&#xff0c;是基于SiamRPN网络结构&#xff0c;提出更好的使用数据&#xff0c;针对跟踪过程的干扰物&#xff0c;利用更好的训练方式是跟踪更加的鲁棒。 DaSiamRPN认识到了现有的目标追踪数据集中存在的不平衡问题&#xff0c…

SyntaxError: Non-ASCII character '\xe9' in file...ubuntu系统下python运行时提示编码格式不正确以及提示No module named xxx

在Ubuntu 18下创建django项目&#xff0c;运行python文件时报错&#xff1a; SyntaxError: Non-ASCII character \xe9 in file /home/image/桌面/django_test/test_proj/ test_proj/settings.py on line 127, but no encoding deared; see http://python.org/dev/ peps/pep-02…

智慧交通day04-特定目标车辆追踪03:siamese在目标跟踪中的应用-SiamRPN++(2019)

3.4.1 模型结构 SiamRPN网络结构如下图所示&#xff0c;虚线的两边都是网络结构图&#xff0c;虚线左侧是特征提取网络结构&#xff0c;右侧是RPN结构图。其实SiamRPN的网络结构与SiamRPN网络结构十分相似&#xff0c;而SiamRPN就是在SiamRPN的基础上加入许多的创新点。 SiamR…

lanmp之二 (奇葩问题)

ps&#xff1a;该篇是接 lanmp —— 动静分离 lanmp —— 奇葩问题 话说&#xff0c;在 搭建 bbs.abc.com &#xff08;discuz论坛&#xff09;的 时候。。。。 1、说明&#xff1a;web机器上以前已经有一个 discuz 旧论坛 要做数据迁移&#xff08;改域名/拷贝数据/迁移网站&a…

[已解决]user is not in the sudoers file. This incident will be reported.(简单不容易出错的方式)

linux Ubuntu中报错&#xff0c;XXX is not in the sudoers file. This incident will be reported. 今天在学习的时候&#xff0c;不小心把自己的用户sudo玩坏了&#xff08;Broken sudo&#xff09;&#xff0c;于是上谷歌搜索XXX is not in the sudoers file. This inciden…

智慧交通day04-特定目标车辆追踪03:siamese在目标跟踪中的应用-SiamMask(2019)

与普通的视频跟踪网络不同的是&#xff0c;SiamMask可以同时完成视频跟踪和实例级分割的任务。如下图所示&#xff0c;与传统的对象跟踪器一样&#xff0c;依赖于一个简单的边界框初始化&#xff08;蓝色&#xff09;并在线操作。与ECO&#xff08;红色&#xff09;等最先进的跟…

编译器的大小端模式

大端模式&#xff08;Big_endian&#xff09;&#xff1a;字数据的高字节存储在低地址中&#xff0c;而字数据的低字节则存放 在高地址中。 小端模式&#xff08;Little_endian&#xff09;&#xff1a;字数据的高字节存储在高地址中&#xff0c;而字数据的低字节则存放 在低地…

【原】jQuery编写插件

分享一下编写设置和获取颜色的插件&#xff0c;首先我将插件的名字命名为jquery.color.js。该插件用来实现以下两个功能1.设置元素的颜色。2.获取元素的颜色。 先在搭建好如下编写插件的框架&#xff1a; ;(function($){//这里编写插件代码 })(jQuery); 我这里采用jQuery.fn.ex…

数据结构链表之单向链表:Python3 实现单向链表——1

Python3 实现单向链表 链表定义与简介 定义&#xff1a;链表与顺序表(Python中列表)性质相反&#xff0c;链表是物理单元上非顺序的、非连续的&#xff0c;在逻辑顺序上其数据元素是通过指针实现的&#xff0c;组成链表的每一个元素也可以叫做链表的节点&#xff0c;节点可以…

智慧交通day04-特定目标车辆追踪03:siamese在目标跟踪中的应用-汇总

总结&#xff1a; Siamese网络衡量两个输入的相似程度&#xff0c;输出是一个[0,1]的浮点数&#xff0c;表示二者的相似程度。孪生神经网络有两个输入&#xff08;Input1 and Input2&#xff09;,将两个输入feed进入两个神经网络&#xff08;Network1 and Network2&#xff09…

C语言(贪心法)

C语言有这样一个规则&#xff0c;每一个符号应该包含尽可能多的字符。也就是说&#xff0c;编译器将程序分解成符号的方法是&#xff0c;从左到右一个一个字符地读入&#xff0c;如果字条可能组成一个符号&#xff0c;那么再读入下一个字符&#xff0c;判断已经读入的两个字符组…

数据结构链表之双向链表:Python3 实现双向链表——2

Python3 实现双向链表 双向链表 定义&#xff1a;双向链表是链表中的一种&#xff0c;双向链表也叫双链表&#xff0c;它由多个节点组成&#xff0c;每个节点由一个数据域和两个指针域组成&#xff0c;一个指针指向前驱元素&#xff0c;一个指向后继元素 双向链表一般用来构…

linux驱动之ioctl

大部分驱动除了需要具备读写设备的能力之外&#xff0c;还需要具备对硬件控制的能力。 一、在用户空间&#xff0c;使用ioctl系统调用来控制设备&#xff0c;原型如下&#xff1a; int ioctl(int fd,unsigned long cmd,...); /* fd:文件描述符 cmd:控制命令 ...:可选参数:插入*…

轮播图的无限轮播

简介 在现在的一些App中常常见到图片轮播器&#xff0c;一般用于展示广告、新闻等数据&#xff0c;在iOS内并没有现成的控件直接实现这种功能&#xff0c;但是通过UIScrollView的允许分页设置&#xff0c;可以实现滚动轮播的功能。 轮播原理 UIScrollView对象有pagingEnable成员…