linux timer
来源:互联网 发布:vscode前端必备插件 编辑:程序博客网 时间:2024/05/22 04:48
目录(?)[-]
- 包含的头文件linuxtimerh
- 数据类型struct timer_list
- 主要相关的API函数
- 时间与jiffies的转换函数
- 使用简例
Linux内核中提供了timer使用的API,做一个简单的记要。
1. 包含的头文件:linux/timer.h
2. 数据类型:struct timer_list;
包含的主要成员:a. data:传递到超时处理函数的参数,主要在多个定时器同时使用时,区别是哪个timer超时。b. expires:定时器超时的时间,以linux的jiffies来衡量。c. void (*function)(unsigned long):定时器超时处理函数。
3. 主要相关的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*):删除定时器。
4. 时间与jiffies的转换函数:
Linux系统中的jiffies类似于Windows里面的TickCount,它是定义在内核里面的一个全局变量,只是它的单位并不是秒或是毫秒。通常是250个jiffies为一秒,在内核里面可以直接使用宏定义:HZ。这里有几个时间和jiffies的相互转换函数:unsigned int jiffies_to_msecs(unsigned long);unsigned int jiffies_to_usecs(unsigned long);unsigned long msecs_to_jiffies(unsigned int);unsigned long usecs_to_jiffies(unsigned int);
5. 使用简例:
步骤:init_timer->[timer.expires=? & timer.function=?]->add_timer->[mod_timer]->del_timer.
<code class="hljs perl has-numbering"><span class="hljs-comment">#include <linux/init.h></span><span class="hljs-comment">#include <linux/module.h></span><span class="hljs-comment">#include <linux/timer.h></span>struct timer_list timer;void timer_handler(unsigned long data) { printk(KERN_INFO<span class="hljs-string">"timer pending:<span class="hljs-variable">%d</span>\n"</span>, timer_pending(&timer)); mod_timer(&timer, jiffies+msecs_to_jiffies(<span class="hljs-number">1000</span>)); printk(KERN_INFO<span class="hljs-string">"jiffies:<span class="hljs-variable">%ld</span>, data:<span class="hljs-variable">%ld</span>\n"</span>, jiffies, data);}<span class="hljs-keyword">int</span> timer_init(void) { printk(KERN_INFO<span class="hljs-string">"<span class="hljs-variable">%s</span> jiffies:<span class="hljs-variable">%ld</span>\n"</span>, __func_<span class="hljs-number">_</span>, jiffies); printk(KERN_INFO<span class="hljs-string">"ji:<span class="hljs-variable">%d</span>,HZ:<span class="hljs-variable">%d</span>\n"</span>, jiffies_to_msecs(<span class="hljs-number">250</span>), HZ); init_timer(&timer); timer.data = <span class="hljs-number">45</span>; timer.function = timer_handler; timer.expires = jiffies + HZ; add_timer(&timer); printk(KERN_INFO<span class="hljs-string">"timer pending:<span class="hljs-variable">%d</span>\n"</span>, timer_pending(&timer)); <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;}void timer_exit(void) { printk(KERN_INFO<span class="hljs-string">"<span class="hljs-variable">%s</span> jiffies:<span class="hljs-variable">%ld</span>\n"</span>, __func_<span class="hljs-number">_</span>, jiffies); del_timer(&timer);}module_init(timer_init);module_exit(timer_exit);MODULE_LICENSE(<span class="hljs-string">"GPL"</span>);</code>
0 0
- linux timer
- linux timer
- **linux timer
- linux timer
- linux timer
- linux 计时器 timer
- timer linux编程学习
- linux timer 代码示例
- Linux timer example
- linux time和timer
- linux 定时器timer使用
- Linux Posix Timer使用
- linux timer using example
- Linux 高精度timer解析
- linux timer(1)
- linux timer(2)
- linux Timer 定时器使用
- linux timer应用
- android动画
- 滚动心电图形的绘制(Core Graphics)
- Vijos P1688 病毒传递
- handy : TcpConn
- 提取不重复整数(从右向左去重)
- linux timer
- 从0开始学习 GitHub 系列之「03.Git 速成」
- 常用的一些Android代码
- Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-3.2.xsd'
- NSUserDefaults的使用
- unexpected token: Member
- crontab文件的位置
- 【数据结构】算法10.2 插入排序-折半插入
- HDU 3729 二分图匹配