获取时间
1、jiffies
jiffies:从系统启动到当前,时钟中断产生的次数marco HZ 每秒产生的时钟中断次数,范围100~1000
2、使用do_gettimeofday函数
延时
1、长延迟
2、段延迟以jiffy为单位的延迟叫长延迟(占用CPU)time_before(jiffies, new_jiffies)time_after(new_jiffies, jiffies)
延迟时间小于一个jiffyudelay 微妙延迟 占用CPUmdelay 毫秒延迟 占用CPUmsleep 毫秒随眠 不占用CPUb
内核定时器
内核定时器用法
- struct timer_list my_timer; //定义定时器
- void handler(unsigned long data); //定义执行函数
- inti_timer(&my_timer); //初始化定时器
- my_timer.function = handler
- my_timer.expires = jiffies + HZ;
- add_timer(&my_timer); //添加定时器
- del_timer(&my_timer); //删除定时器,执行函数中调用
- 可以执行函数中调用mod_timer,重新计时
参考代码
======================================================================#include <linux/module.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/cdev.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#include <linux/timer.h> /*包括timer.h头文件*/
#define SECOND_MAJOR 250 /*预设的second的主设备号*/
static int second_major = SECOND_MAJOR;
/*second设备结构体*/struct second_dev{struct cdev cdev; /*cdev结构体*/int counter;/* 一共经历了多少秒?*/struct timer_list s_timer; /*设备要使用的定时器*/} second_dev;
/*定时器处理函数*/static void second_timer_handle(unsigned long arg){mod_timer(&second_dev.s_timer,jiffies + HZ);second_dev.counter++;
printk(KERN_NOTICE "current jiffies is %ld\n", jiffies);}
/*文件打开函数*/int second_open(struct inode *inode, struct file *filp){/*初始化定时器*/init_timer(&second_dev.s_timer);second_dev.s_timer.function = second_timer_handle;second_dev.s_timer.expires = jiffies + HZ;
add_timer(&second_dev.s_timer); /*添加(注册)定时器*/second_dev.counter = 0; //计数清0
return 0;}
/*文件释放函数*/int second_release(struct inode *inode, struct file *filp){del_timer(&second_dev.s_timer);
return 0;}
/*globalfifo读函数*/static ssize_t second_read(struct file *filp, char __user *buf, size_t count,loff_t *ppos){
if(put_user(second_dev.counter, (int*)buf))return -EFAULT;elsereturn sizeof(unsigned int);}
/*文件操作结构体*/static const struct file_operations second_fops ={.owner = THIS_MODULE,.open = second_open,.release = second_release,.read = second_read,};
/*初始化并注册cdev*/static void second_setup_cdev(struct second_dev *dev, int index){int err, devno = MKDEV(second_major, index);
cdev_init(&dev->cdev, &second_fops);dev->cdev.owner = THIS_MODULE;err = cdev_add(&dev->cdev, devno, 1);if (err)printk(KERN_NOTICE "Error %d adding LED%d", err, index);}
/*设备驱动模块加载函数*/int second_init(void){int ret;dev_t devno = MKDEV(second_major, 0);
/* 申请设备号*/ret = register_chrdev_region(devno, 1, "second");if (ret < 0)return ret;
second_setup_cdev(&second_dev, 0);
return 0;}
/*模块卸载函数*/void second_exit(void){cdev_del(&second_dev.cdev); /*注销cdev*/unregister_chrdev_region(MKDEV(second_major, 0), 1); /*释放设备号*/}
module_init(second_init);module_exit(second_exit);
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
int main()
{
int fd;
int counter = 0;
int old_counter = 0;
/*打开/dev/second设备文件*/
fd = open("/dev/second", O_RDONLY);
if (fd < 0)
{
perror("fail to open");
exit(-1);
}
while (1)
{
read(fd,&counter, sizeof(unsigned int));//读目前经历的秒数
if(counter != old_counter)
{
printf("seconds after open /dev/second :%d\n",counter);
old_counter = counter;
}
}
return 0;
}