【问题标题】:Need some explanation about linux kernel wait queues需要一些关于linux内核等待队列的解释
【发布时间】:2011-11-30 02:25:52
【问题描述】:

我需要为我的大学课程编写一些内核模块,现在我正在尝试了解我必须使用的各种内核机制。其中之一是等待队列。我编写了一个简单的模块,它注册了一个/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");
    }
}

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

第一个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

我将不胜感激对这种现象的一些解释。

【问题讨论】:

    标签: linux-kernel kernel


    【解决方案1】:

    查看fs/proc/generic.c 中关于“如何成为 proc 读取函数”的评论。由于您没有更改eof,因此__proc_file_read 中的循环将多次调用您的read_proc 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多