一、任务1:使用tasklet实现打印helloworld(20min)
1、任务描述
编写内核模块,使用tasklet实现打印helloworld。
加载、卸载模块并查看模块打印信息。
2、实验过程
tasklet_interrupt.c文件
#include <linux/module.h> #include <linux/interrupt.h> MODULE_LICENSE("GPL"); static struct tasklet_struct my_tasklet; static void tasklet_handler(unsigned long data) { printk("Hello World! tasklet is working...\n"); } static int __init mytasklet_init(void) { printk("Start tasklet module...\n"); tasklet_init(&my_tasklet, tasklet_handler, 0); tasklet_schedule(&my_tasklet); return 0; } static void __exit mytasklet_exit(void) { tasklet_kill(&my_tasklet); printk("Exit tasklet module...\n"); } module_init(mytasklet_init); module_exit(mytasklet_exit);Makefile文件
ifneq ($(KERNELRELEASE),) obj-m := tasklet_interrupt.o else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif .PHONY:clean clean: -rm *.mod.c *.o *.order *.symvers *.ko编写好上述文件
内核模块加载结果:
二、任务2:用工作队列实现周期打印helloworld(25min)
1、任务描述
编写一个内核模块程序,用工作队列实现周期打印helloworld。
加载、卸载模块并查看模块打印信息。
2、实验过程
workqueue_test.c文件
#include <linux/module.h> #include <linux/workqueue.h> #include <linux/delay.h> MODULE_LICENSE("GPL"); static struct workqueue_struct *queue = NULL; static struct delayed_work mywork; static int i = 0; void work_handle(struct work_struct *work) { printk(KERN_ALERT "Hello World!\n"); } static int __init timewq_init(void) { printk(KERN_ALERT "Start workqueue_test module."); queue = create_singlethread_workqueue("workqueue_test"); if(queue == NULL){ printk(KERN_ALERT "Failed to create workqueue_test!\n"); return -1; } INIT_DELAYED_WORK(&mywork, work_handle); for(;i <= 3; i++){ queue_delayed_work(queue, &mywork, 5 * HZ); ssleep(15); } return 0; } static void __exit timewq_exit(void) { flush_workqueue(queue); destroy_workqueue(queue); printk(KERN_ALERT "Exit workqueue_test module."); } module_init(timewq_init); module_exit(timewq_exit);Makefile文件
ifneq ($(KERNELRELEASE),) obj-m := workqueue_test.o else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif .PHONY:clean clean: -rm *.mod.c *.o *.order *.symvers *.ko编写好上述文件
内核模块加载结果:
由代码逻辑知:工作队列延时 5*HZ(5秒)开始执行,对应上图中模块加载后5秒才打印HelloWorld!;而后每次执行工作队列中间休眠15秒,与上图对应。
三、任务3:编写一个信号捕获程序,捕获终端按键信号(25min)
1、任务描述
在用户态编写一个信号捕获程序,捕获终端按键信号(包括ctrl+c、ctrl+z、ctrl+\)。
编译上述程序后运行,在终端输入按键信号(ctrl+c、ctrl+z、ctrl+\),查看输出信息。
2、实验过程
catch_signal.c文件
#include <signal.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> void signal_handler(int sig) { switch(sig){ case SIGINT: printf("\nGet a signal:SIGINT. You pressed ctrl+c.\n"); break; case SIGQUIT: printf("\nGet a signal:SIGQUIT. You pressed ctrl+\\.\n"); break; case SIGTSTP: printf("\nGet a signal:SIGHUP. You pressed ctrl+z.\n"); break; } exit(0); } int main() { printf("Current process ID is %d\n", getpid()); signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); signal(SIGTSTP, signal_handler); for(;;); }