title: Need some explanation about linux kernel wait queues
body: <p>I need to write some kernel module for my university classes and now I'm trying to understand various kernel mechanisms that I have to use. One of them are <strong>wait queues</strong>. I wrote a simple module that registers a <code>/proc</code> entry and does some simple logic inside its <code>read</code> function:</p>
  
  <pre><code>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-&gt;pid);
      if(should_wait == 1) {
          printk(KERN_INFO "Waiting in %i\n", current-&gt;pid);
          should_wait = 0;
          wait_event_interruptible(event, condvar == 1);
          printk(KERN_INFO "Wait finished in %i\n", current-&gt;pid);
          condvar = 0;
          return sprintf(page, "%s", "The wait has finished!\n");
      } else {
          printk(KERN_INFO "Waking up in %i\n", current-&gt;pid);
          condvar = 1;
          wake_up_interruptible(&amp;event);
          return sprintf(page, "%s", "Woke up the other process!\n");
      }
  }
  </code></pre>
  
  <p>This is what happens when I try to run <code>cat</code> twice on the registered <code>/proc</code> file:</p>
  
  <p>First <code>cat</code> process waits for the second one to wake him up. Second <code>cat</code> process does exactly that. Each process prints what it should. So far, everything as planned. But then I look into <code>dmesg</code> and that is what I see:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>My question is: why the <code>simple_read_proc</code> function was invoked so many times? I thought that this may be because <code>cat</code> invoked <code>read</code> many times, but I checked it with <code>strace</code> and this is not the case - each <code>cat</code> invoked <code>read</code> only once.</p>
  
  <p>I would be grateful for some explanation of this phenomenon.</p>