【OS】实验四:中断和异常管理

527 阅读1分钟

实验四:中断和异常管理

任务1:使用tasklet实现打印helloworld

编写源文件

tasklet_intertupt.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_intertupt.o
else
	KERNELDIR ?= /root/kernel-5.10.0-13.0.0
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
.PHONY:clean
clean:
	-rm *.mod.c *.o *.order *.symvers *.ko

内核模块加载结果

image-20211225163117290

任务2:用工作队列实现周期打印helloworld

编写源文件

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;

//work handle
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 ?= /root/kernel-5.10.0-13.0.0
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
.PHONY:clean
clean:
	-rm *.mod.c *.o *.order *.symvers *.ko

内核模块加载结果

image-20211225164045813

任务3:编写一个信号捕获程序,捕获终端按键信号

编写源文件

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(;;);
}

运行结果

image-20211225164618251

image-20211225164648544

image-20211225164702412