现在的位置: 首页 > 综合 > 正文

实例:中断的下半部分之三work_queue

2018年02月07日 ⁄ 综合 ⁄ 共 1994字 ⁄ 字号 评论关闭

在linux中使用工作队列work_queue

软硬件环境:linux-2.6.36/s3c2440


工作队列在进程上下文中运行,允许重新调度甚至睡眠

test.c


#include <linux/module.h>
#include <linux/init.h>

#include <linux/mm.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

#include <asm/current.h>


MODULE_LICENSE("GPL");

MODULE_AUTHOR("zhanglong");

struct work_struct my_work;

void work_handle(struct work_struct* arg)
{

    long data = atomic_long_read(&arg->data);


    printk("current: %s, pid: %d\n", current->comm, current->pid);

    ssleep(3);

    printk("after sleep, data = %ld\n", data);    

    //printk("&my_work = 0x%x\n", (unsigned int)&my_work);    

}


irqreturn_t irq_handle(int irq, void *dev_id)
{

    schedule_work(&my_work);

    printk("----irq handle. dev_id = %d\n", (int)dev_id);

    return 0;
}

int test_init(void)
{

    int ret = 0;


    INIT_WORK(&my_work, work_handle);


    set_irq_type(IRQ_EINT0, IRQ_TYPE_EDGE_RISING);

    ret = request_irq(IRQ_EINT0, irq_handle, 0, "my
irq"
, (void *)123);

    if (ret) {    

        printk("request irq failed.\n");

        return -EBUSY;

    }


    return 0;
}

void test_exit(void)
{

    free_irq(IRQ_EINT0, (void *)123);
}


module_init(test_init);

module_exit(test_exit);



Makefile


KERNEL    = /media/STUDY/linux/kernel/my2440-2.6.36

#KERNEL    = /lib/modules/$(shell uname -r)/build


default:

    make -C $(KERNEL) M=$(shell
pwd) modules


clean:

    make -C $(KERNEL) M=$(shell
pwd) modules clean


modules_install:

    make -C $(KERNEL) M=$(shell
pwd) modules_install INSTALL_MOD_PATH=/home/zl/s3c2440_nfs

    depmod --b /home/zl/s3c2440_nfs 2.6.13-my2440


obj-m    += test.o



make后插入生成的test.ko模块。按下GPF0所在的按键,终端会有输出:

[root@zhanglong mywork]# ----irq handle. dev_id = 123

current: kworker/0:1, pid: 267

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

----irq handle. dev_id = 123

after sleep, data = 0

current: kworker/0:1, pid: 267

after sleep, data = 0


[root@zhanglong mywork]# 

抱歉!评论已关闭.