1
我需要为我的大学类编写一些内核模块,现在我正试图理解我必须使用的各种内核机制。其中之一是等待队列。我写了一个简单的模块,注册一个/proc
项,也不它read
函数里面的一些简单的逻辑:需要关于linux内核等待队列的一些解释
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
一次。
我将不胜感激这种现象的一些解释。