Blocking IO

From RCSWiki

Jump to: navigation, search

Blocking IO is used when a driver request cannot be satisfied immediately. This situation often happens when we want to read from a device while the data is not available, or when we want to write to the device while the device is not ready to accept the data.

The simple example to use Blocking IO is listed below:

  • Include the header file
 #Include <linux/wait.h>
  • Define and initialize the wait queue
statically
 static DECLARE_WAIT_QUEUE_HEAD(queue) ;
 static int flag = 0 ; // flag used for blocking IO
or dynamicly
 wait_queue_head_t my_queue ;
 init_waitqueue_head(&my_queue) ;
 static int flag = 0 ; // flag used for blocking IO
  • Inside the process that you want to block(read, write, etc.)
 {
   ...
   wait_event_interruptible(queue, flag != 0) ;  // until flag !=0 is true, the process continues to sleep
   flag = 0 ;
   ...
 }
  • Inside the process that you want to wake up your blocked process (interrupts)
 {
   ...
   flag = 1 ;
   wake_up_interruptible(&queue) ;
   ...
 }

Refer to Linux Device Driver 3 Chapter 6 P148 - P162 for more details


Personal tools