相信自己,只有不想做的,没有做不到的。
分类: LINUX
2013-12-12 13:02:17
定时器
1>
/*second设备结构体*/
struct second_dev
{
struct cdev cdev; /*cdev结构体*/
atomic_t counter;/* 一共经历了多少秒?*/
struct timer_list s_timer; /*设备要使用的定时器*/
};
2>
/*文件打开函数*/
int second_open(struct inode *inode, struct file *filp)
{
/*初始化定时器*/
init_timer(&second_devp->s_timer);
second_devp->s_timer.function = &second_timer_handle;
second_devp->s_timer.expires = jiffies + HZ ;
add_timer(&second_devp->s_timer); /*添加(注册)定时器*/
atomic_set(&second_devp->counter,0); //计数清0
return 0;
}
3>
/*定时器处理函数*/
static void second_timer_handle(unsigned long arg)
{
mod_timer(&second_devp->s_timer,jiffies + HZ );
atomic_inc(&second_devp->counter);
printk(KERN_NOTICE "current jiffies is %ld\n", jiffies);
}
4>
/*文件释放函数*/
int second_release(struct inode *inode, struct file *filp)
{
del_timer(&second_devp->s_timer);
return 0;
}