如何在中断代码中模拟竞争条件场景

时间:2013-11-15 03:00:22

标签: linux testing linux-kernel linux-device-driver race-condition

我是学习Linux设备驱动程序的初学者。我写了一个键盘驱动程序:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <asm/io.h>

/*
 This function services keyboard interrupts.
*/
int packetNumber=3000;
EXPORT_SYMBOL(packetNumber);
irqreturn_t irq_handler (int irq, void *dev_id, struct pt_regs *regs)
{
    static unsigned char scancode;  
    /*
     Read keyboard status
    */
    scancode = inb (0x60);
    if (scancode == 0x01)
    {
        packetNumber += 10;
        printk(KERN_ALERT "packetNumber:%x\n",&packetNumber);
        printk (KERN_ALERT "You pressed Esc ! packetNumber: %d\n", packetNumber);
    }
    return (irqreturn_t) IRQ_HANDLED;
}

/*
 Initialize the module ? register the IRQ handler
*/
static int hello_keyboard (void)
{
    /*
     Request IRQ 1, the keyboard IRQ, to go to our irq_handler SA_SHIRQ means we're willing to have othe handlers on this IRQ. SA_INTERRUPT can be used to make the handler into a fast interrupt.
    */
    packetNumber += 10;
    printk(KERN_ALERT "hello, keyboard.%d\n", packetNumber);
    return request_irq (1, (irqreturn_t) irq_handler, SA_SHIRQ, "test_keyboard_irq_handler", (void *)(irq_handler));
}

static void bye_keyboard(void)
{
    printk(KERN_ALERT "Goodbye, keyboard\n");
}

module_init(hello_keyboard);
module_exit(bye_keyboard);

MODULE_AUTHOR("e-Friends");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("a simple keyboard driver.");

我想使用这个键盘驱动程序模拟这样的竞争条件场景: http://ww3.sinaimg.cn/mw690/5f10acdbjw1ealisec0frj20lk0e8gnm.jpg


我对上面的图片不太了解。

  

为了模拟场景,我想知道:

  • main_task函数应该在哪里?在驱动程序或用户中 申请使用驱动程序?
  • 如果main_task函数应该在用户应用程序中,怎么可能 我定义了packetNumber变量来让两个中断处理程序 和用户应用程序可以使用同一个变量吗?

PS: 我暂时在init函数中使用了packetNumber变量(hello_keyboard函数),对吗?

0 个答案:

没有答案