实验四、中断和异常管理

1、使用tasklet实现打印helloworld

tasklet是linux中断处理机制中的软中断延迟机制。引入tasklet,最主要的是考虑支持SMP(多处理,Symmetrical Multi-Processing),提高SMP多个cpu的利用率;不同的tasklet可以在不同的cpu上运行。tasklet可以理解为softirq(软中断)的派生,所以它的调度时机和软中断一样。对于内核中需要延迟执行的多数任务都可以用tasklet来完成,由于同类tasklet本身已经进行了同步保护,所以使用tasklet比软中断要简单的多,tasklet不需要考虑SMP下的并行问题,而又比workqueues有着更好的性能。tasklet通常作为中断下半部来使用,它在性能和易用性之间有着很好的平衡。

相关方法(函数):

  • void tasklet_init(struct tasklet_struct t, void (func)(unsigned long), unsigned long data); //初始化tasklet,func指向要执行的函数,data为传递给函数func的参数

  • tasklet_schedule(&my_tasklet) //调度执行指定的tasklet

  • void tasklet_kill(struct tasklet_struct *t) //移除指定tasklet

  • void tasklet_disable(struct tasklet_struct *t) //禁用指定tasklet

  • void tasklet_enable(struct tasklet_struct *t) //启用先前被禁用的tasklet

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

编译运行:

2、用工作队列实现周期打印helloworld

workqueue.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);

编译运行:(打印4次“Hello World!\n”,模块加载之后5秒开始打印,每次打印之间休眠15秒)

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

信号机制是对中断机制的一种模拟,在实现上是一种软中断。

信号的响应动作:

  1. 中止进程(Term)

  2. 忽略信号(Ign)

  3. 中止进程并保存内存信息(Core)

  4. 停止进程(Stop)

  5. 继续运行进程(Cont)

信号类型参考表:

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

编译、运行:

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯若

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值