【Linux设备驱动程序(第三版)】----获取当前时间

本文介绍了一种在Linux设备驱动程序中获取当前时间的方法,通过使用`jiffies`、`get_jiffies_64()`、`do_gettimeofday()`等函数,实现了将时间以十六进制和浮点数格式输出到`/proc`目录下的`jit_currentime`文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 【Linux设备驱动程序(第三版)】----获取当前时间

jit.c

 

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <asm/hardirq.h>
#include <linux/sched.h>//jiffies
#include <linux/kernel.h>
#include <linux/types.h>//u64
#include <linux/fs.h>//file_operations, file
#include <linux/completion.h>
#include <asm/uaccess.h>//copy_to_user & copy_from_user

int jit_currentime(char *buf, char **start, off_t offset, int len, int *eof, void *data)
{
	struct timeval tv1;
	struct timespec tv2;
	unsigned long j1;
	u64 j2;

	j1 = jiffies;
	j2 = get_jiffies_64();
	do_gettimeofday(&tv1);
	tv2 = current_kernel_time();
	
	len = 0;
	len += sprintf(buf,"0x%08lx 0x%016Lx %10i.%06i\n"
		       "%40i.%09i\n",
		       j1, j2,
		       (int) tv1.tv_sec, (int) tv1.tv_usec,
		       (int) tv2.tv_sec, (int) tv2.tv_nsec);
	*start = buf;
	return len;
}

int __init jit_init(void)
{
	printk(KERN_DEBUG "jit_init......");
	create_proc_read_entry("jit_currentime", 0, NULL, jit_currentime, NULL);
	return 0;
}

void __exit jit_exit(void)
{
	remove_proc_entry("jit_currentime", NULL);
}

MODULE_LICENSE("Dual BSD/GPL");
module_init(jit_init);
module_exit(jit_exit);


Makefile

obj-m:= jit.o
modules-objs:= jit.o

KDIR:= /usr/src/linux-headers-2.6.31-14-generic/
PWD:= $(shell pwd)

default:
 make -C $(KDIR) M=$(PWD) modules
clean:
 rm -rf *.ko *.mod.c *.mod.o *.o *.markers *.symvers *.order


装载驱动

insmod jit.ko


测试

cat /proc/jit_currentime


输出结果:

0x000d52fd 0x00000001000d52fd 1310097626.455468
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455470
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455472
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455474
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455476
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455478
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455481
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455483
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455485
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455487
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455489
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455491
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455493
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455495
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455497
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455499
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455502
                              1310097626.454516548
0x000d52fd 0x00000001000d52fd 1310097626.455504
                              1310097626.454516548


卸载

rmmod jit


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值