需要一些关于linux内核等待队列的解释

时间:2011-11-29 14:57:41

标签: linux-kernel kernel

我需要为我的大学课程编写一些内核模块,现在我正在尝试理解我必须使用的各种内核机制。其中一个是等待队列。我写了一个简单的模块,它注册了一个/proc条目,并在其read函数中做了一些简单的逻辑:

DECLARE_WAIT_QUEUE_HEAD(event);

volatile int condvar = 0;
volatile int should_wait = 1;

int simple_read_proc(char *page, char **start, off_t offset, int count, int *eof, void *data) {
    printk(KERN_INFO "simple_read_proc from %i\n", current->pid);
    if(should_wait == 1) {
        printk(KERN_INFO "Waiting in %i\n", current->pid);
        should_wait = 0;
        wait_event_interruptible(event, condvar == 1);
        printk(KERN_INFO "Wait finished in %i\n", current->pid);
        condvar = 0;
        return sprintf(page, "%s", "The wait has finished!\n");
    } else {
        printk(KERN_INFO "Waking up in %i\n", current->pid);
        condvar = 1;
        wake_up_interruptible(&event);
        return sprintf(page, "%s", "Woke up the other process!\n");
    }
}

当我尝试在已注册的cat文件上运行/proc两次时会发生这种情况:

第一个cat进程等待第二个进程唤醒他。第二个cat进程就是这样做的。每个过程都打印出应有的内容。到目前为止,一切都按计划进行但后来我调查了dmesg,这就是我所看到的:

[11405.484168] Initializing proc_module
[11413.209535] simple_read_proc from 6497
[11413.209543] Waiting in 6497
[11415.498482] simple_read_proc from 6499
[11415.498489] Waking up in 6499
[11415.498507] simple_read_proc from 6499
[11415.498514] Wait finished in 6497
[11415.498518] Waking up in 6499
[11415.498546] simple_read_proc from 6497
[11415.498550] Waking up in 6497
[11415.498554] simple_read_proc from 6497
[11415.498557] Waking up in 6497
[11415.498689] simple_read_proc from 6499
[11415.498694] Waking up in 6499
[11415.498753] simple_read_proc from 6497
[11415.498757] Waking up in 6497

我的问题是:为什么多次调用simple_read_proc函数?我认为这可能是因为cat多次调用了read,但我使用strace进行了检查,情况并非如此 - 每个cat都调用read只有一次。

我很感激对这种现象的一些解释。

1 个答案:

答案 0 :(得分:2)

查看fs/proc/generic.c中关于“如何成为proc读取函数”的注释。由于您没有更改eof__proc_file_read中的循环会多次调用您的read_proc函数。

相关问题