driver.c源代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
MODULE_AUTHOR("sunsea");
MODULE_DESCRIPTION("module example");
MODULE_LICENSE("GPL");
static int time;
module_param(time, int, 0);
int __init
akae_init(void)
{
printk("hello world/n");
set_current_state(TASK_INTERRUPTIBLE); //延时
schedule_timeout(time*HZ); //延时时间,单位秒
printk("count = %d/n", count);
printk("time = %d/n", time);
return 0;
}
void __exit
akae_exit(void)
{
printk("module exit/n");
return;
}
module_init(akae_init);
module_exit(akae_exit);
Makefile文件
ifneq ($(KERNELRELEASE),)
#kbuild part of makefile
obj-m := driver.o
else
KERNELSRC :=/lib/modules/`uname -r`/build
modules:
make -C $(KERNELSRC) SUBDIRS=$(PWD) $@
clean:
rm -f *.o *.ko *.mod.c *~
endif
执行方法
sudo insmod driver.ko time=3 //加载模块
sudo rmmod driver //删除模块