任务1:调用内核时钟接口打印当前时间(20min)
文章目录
任务要求
- 编写内核模块,调用内核时钟接口,打印出系统当前时间。格式示例:2020-03-09 11:54:31;
- 加载、卸载模块并查看模块打印信息。
任务代码
current_time.c
#include <linux/module.h>
#include <linux/time.h>
#include <linux/rtc.h>
MODULE_LICENSE("GPL");
struct timeval tv;
struct rtc_time tm;
static int __init currenttime_init(void)
{
int year, mon, day, hour, min, sec;
printk("Start current_time module...\n");
do_gettimeofday(&tv);
rtc_time_to_tm(tv.tv_sec, &tm);
year = tm.tm_year + 1900;
mon = tm.tm_mon + 1;
day = tm.tm_mday;
hour = tm.tm_hour + 8