曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4408|回复: 0
打印 上一主题 下一主题

定时器简单应用

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2011-2-18 20:54:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
定时器一个相关的数据结构(内核版本:Debian5@2.6.26):
struct timer_list {
    struct list_head entry;
    unsigned long expires;

    void (*function)(unsigned long);
    unsigned long data;

    struct tvec_t_base_s *base;
#ifdef CONFIG_TIMER_STATS
    void *start_site;
    char start_comm[16];
    int start_pid;
#endif
};

其中常用到的几个量为:
expires --- 超时时间
function --- 超时后执行的函数
data --- 传入 function 函数的变量。


定时器应用简单实例:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/timer.h>

MODULE_LICENSE("Dual BSD/GPL");

struct timer_list timer_test;

void print_message(unsigned long parameter)
{
    printk ("time over and the parameter is %lu\n", parameter);
   
}
static int time_init (void)
{
    init_timer (&timer_test);
    timer_test.expires = jiffies + 3*HZ;
    timer_test.data = 888;   
     timer_test.function = print_message;
    add_timer(&timer_test);   
        return 0;
}

static void time_exit (void)
{
    del_timer(&timer_test);
}

Makefile 文件

obj-m := timer.o
all:
    @make -C /lib/modules/`uname -r`/build M=`pwd` modules



加载后通过 demsg 可以看到输出(3秒后):
[ 9702.790529] time over and the parameter is 888
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 12:35 , Processed in 0.075555 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表