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的参数,并且返回void
   unsigned 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的驱动程序,直接上代码

[cpp]  view plain copy
  1. #include <linux/miscdevice.h>  
  2. #include <linux/delay.h>  
  3. #include <asm/irq.h>  
  4. //#include <mach/regs-gpio.h>  
  5. //#include <mach/hardware.h>  
  6. #include <linux/kernel.h>  
  7. #include <linux/module.h>  
  8. #include <linux/init.h>  
  9. #include <linux/mm.h>  
  10. #include <linux/fs.h>  
  11. #include <linux/types.h>  
  12. #include <linux/delay.h>  
  13. #include <linux/moduleparam.h>  
  14. #include <linux/slab.h>  
  15. #include <linux/errno.h>  
  16. #include <linux/ioctl.h>  
  17. #include <linux/cdev.h>  
  18. #include <linux/string.h>  
  19. #include <linux/list.h>  
  20. #include <linux/pci.h>  
  21. #include <asm/uaccess.h>  
  22. #include <asm/atomic.h>  
  23. #include <asm/unistd.h>  
  24. #include <asm/io.h>  
  25. #include <asm/system.h>  
  26. #include <asm/uaccess.h>  
  27. #define TIMER_MAJOR 300  
  28. #define TIMER_MINOR 0  
  29. dev_t timer_dev_t;//设备号  
  30. dev_t timer_dev_major=TIMER_MAJOR;  
  31. dev_t timer_dev_minor=TIMER_MINOR;  
  32. struct TIMER_DEV  
  33. {  
  34.   struct cdev cdev;  
  35.   atomic_t count;  
  36.   struct timer_list timer_list;  
  37.   
  38. };  
  39. struct TIMER_DEV* timer_dev;  
  40. //---------timer interrupt function----------------  
  41. static void timer_function(unsigned long data)  
  42. {  
  43. mod_timer(&(timer_dev->timer_list),jiffies+HZ);//重新设置时间  
  44. printk("current jiffies is %ld,count=%d\n",jiffies,timer_dev->count);  
  45. //(timer_dev->count)++;  
  46. atomic_inc(&(timer_dev->count));  
  47. }  
  48. //--------timer release function--------------------  
  49. static int timer_release(struct inode* inode, struct file* filp)  
  50. {  
  51. del_timer_sync(&(timer_dev->timer_list));  
  52. return 0;  
  53. }  
  54.   
  55. //----------------file open function-----------------  
  56. static int timer_open(struct inode* inode,struct file* filp)  
  57. {  
  58. init_timer(&(timer_dev->timer_list));//初始化定时器  
  59. timer_dev->timer_list.function=timer_function;//设置定时器处理函数  
  60. timer_dev->timer_list.expires=jiffies+HZ;//处理函数1s后运行  
  61. add_timer(&timer_dev->timer_list);//添加定时器  
  62. atomic_set(&(timer_dev->count),0);  
  63. return 0;  
  64. }  
  65. //--------------------------------------  
  66.   
  67. //----------------timer_read function---------------  
  68. static int timer_read(struct file* filp,char __user *buf,size_t count,loff_t* f_pos)  
  69. {  
  70.   
  71.  unsigned int counter=atomic_read(&(timer_dev->count));  
  72. if(copy_to_user(buf,(unsigned int*)&counter,sizeof(unsigned int)))  
  73.   {  
  74.    printk("copy to user error\n");  
  75.    goto out;  
  76.   }  
  77. return (sizeof(unsigned int));  
  78. out:  
  79. return (-EFAULT);  
  80.   
  81. }  
  82.   
  83. struct file_operations timer_ops={  
  84. .owner=THIS_MODULE,  
  85. .open=timer_open,  
  86. .read=timer_read,  
  87. .release=timer_release,  
  88. };  
  89. static int __init timer_init(void)  
  90. {  
  91.  int ret;  
  92.    
  93.  if(TIMER_MAJOR)//主设备号大于0,静态申请设备号  
  94.     {  
  95.     timer_dev_t=MKDEV(TIMER_MAJOR,TIMER_MINOR);  
  96.     ret=register_chrdev_region(TIMER_MAJOR,1,"timer_dev");//first,count,name  
  97.     }  
  98.  else  
  99.     {  
  100.     ret=alloc_chrdev_region(&timer_dev_t,0,1,"time_dev");  
  101.     timer_dev_major=MAJOR(timer_dev_t);  
  102.     }  
  103.  if(ret<0)  
  104.     {  
  105.     printk("can't get major %d\n",timer_dev_major);  
  106.     return ret;  
  107.     }  
  108. //-----------------------------------------------------------  
  109.  timer_dev=kmalloc(sizeof(struct TIMER_DEV),GFP_KERNEL);   
  110.     memset(timer_dev,0,sizeof(struct TIMER_DEV));  
  111.  cdev_init(&(timer_dev->cdev),&timer_ops);  
  112.  cdev_add(&(timer_dev->cdev),timer_dev_t,1);  
  113.  printk("init timer_dev success\n");  
  114. return 0;  
  115.   
  116.   
  117. }  
  118. static void __exit timer_exit(void)  
  119. {  
  120. kfree(timer_dev);  
  121. cdev_del(&(timer_dev->cdev));  
  122. unregister_chrdev_region(MKDEV(TIMER_MAJOR,0),1);  
  123. }  
  124. module_init(timer_init);  
  125. module_exit(timer_exit);  
测试程序
[cpp]  view plain copy
  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值