Chinaunix首页 | 论坛 | 博客
  • 博客访问: 237147
  • 博文数量: 60
  • 博客积分: 1509
  • 博客等级: 上尉
  • 技术积分: 882
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-15 10:30
文章分类

全部博文(60)

文章存档

2012年(2)

2011年(58)

分类: LINUX

2011-02-12 17:52:28

在用户态程序编程时,延时常常使用sleep(),我在 windows下面写Bat脚本还用过”ping localhost”来延时1秒。再写驱动程序时,sleep就不能直接调用了。这里介绍的time_list结构体可以用户延时处理,不过它的功能远比 sleep系统调用强大。

time_list结构体位于内核文件include/linux/timer.h

 

  1. struct timer_list {
  2.         struct list_head entry; //timer_list结构体链表的头部

  3.         unsigned long expires; //用于存放延时结束时间

  4.    
  5.         void (*function)(unsigned long); //延时结束时执行的回调函数,注意这里传递一个无符号长整型数字

  6.         unsigned long data; //常用于存储数据的指针

  7.    
  8.         struct tvec_base *base;
  9. #ifdef CONFIG_TIMER_STATS
  10.         void *start_site;
  11.         char start_comm[16];
  12.         int start_pid;
  13. #endif
  14. };
  15. 这里常用到三个函数init_timer()、add_timer()、del_timer()
  16. /**
  17. * init_timer – 初始化结构体timer.
  18. * @timer: 要被初始化的结构体指针
  19. *
  20. * init_timer() 必须在其他timer相关函数执行前调用,对结构体初始化
  21. */
  22. void init_timer(struct timer_list *timer)
  23. /**
  24. * add_timer – 添加一个timer结构体
  25. * @timer: 要被添加的结构体的指针
  26. *
  27. * The kernel will do a ->function(->data) callback from the
  28. * timer interrupt at the ->expires point in the future. The
  29. * current time is ‘jiffies’.
  30. *
  31. * The timer’s ->expires, ->function (and if the handler uses it, ->data)
  32. * fields must be set prior calling this function.
  33. *
  34. * Timers with an ->expires field in the past will be executed in the next
  35. * timer tick.
  36. */
  37. static inline void add_timer(struct timer_list *timer)
  38. /**
  39. * del_timer – 清除一个timer结构体
  40. * @timer: 要被清除的结构体的指针
  41. *
  42. * del_timer() 清除一个活跃/不活跃的timer结构体
  43. *
  44. * 清除一个不活跃的timer结构时返回0,清除一个活跃(延时结束)的timer结构体时返回1。
  45. */
  46. int del_timer(struct timer_list *timer)
  47. 下面是一个简单的测试模块程序,输出递增数字,时间间隔为一秒。
  48. /* include/linux/timer.c:struct timer_list */
  49. /*
  50.  * 建议将模块插入后,使用xconsole查看程序结果
  51.  * print:
  52.  * 1
  53.  * 2
  54.  * 3
  55.  * 4
  56.  * ...
  57.  */
  58. #include
  59.    
  60. #define T HZ/1 //T is 1s

  61.    
  62. struct timer_list my_timer;
  63.    
  64. static void my_timer_func(unsigned long p){
  65.  int i=my_timer.data;
  66.  printk("%d\n", i);
  67.  my_timer.data = i+1;
  68.  my_timer.expires = jiffies + T; // 延时1s

  69.  add_timer(&my_timer);
  70. }
  71.    
  72. static int __init my_timer_init(void)
  73. {
  74.  init_timer(&my_timer); //初始化结构体

  75.  my_timer.function = my_timer_func; //初始化回调函数

  76.  my_timer.data = 0; //初始化data字段,这里也可以是一个结构体的指针,从而传递更多的数据

  77.  my_timer.expires = jiffies + T; //延时1s

  78.  add_timer(&my_timer);
  79.  printk("Welcome!\n");
  80.  return 0;
  81. }
  82.    
  83. static void __exit my_timer_cleanup(void)
  84. {
  85.  del_timer(&my_timer); //清除结构体

  86.  printk("Bye!\n");
  87. }
  88.    
  89. module_init(my_timer_init);
  90. module_exit(my_timer_cleanup);
  91.    
  92. MODULE_LICENSE("GPLv3");
  93. MODULE_AUTHOR("Jianjun Kong ");
  94. MODULE_DESCRIPTION("This is a test program of struct timer_list.\n");
阅读(2603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~