I'm learning about the blocking I/O functions for writing linux device driver and I'm wondering what is the usage of ERESTARTSYS
. Consider the following:
Global variable :
wait_queue_head_t my_wait_q_head;
int read_avail = 0;
device_init() :
init_waitqueue_head(&my_wait_q_head);
device_read():
printk("I'm inside driver read!\n");
wait_event_interruptible(&my_wait_q_head, read_avail != 0);
printk("I'm awaken!\n");
device_write():
read_avail = 1;
wake_up_interruptible(&my_wait_q_head);
When I call the read()
from user space, the command prompt hang until I call the write()
as expected. The printk
messages appear accordingly as well in dmesg
. However, I'm seeing some of the drivers written like this :
Another version of device_read():
printk("I'm inside driver read!\n");
if(wait_event_interruptible(&my_wait_q_head, read_avail != 0))
{return -ERESTARTSYS;}
printk("I'm awaken!\n");
I tested the second version of device_read()
using the same method in user space, and the result is exactly the same, so, what's the use of ERESTARTSYS?
p/s: I've read the book Linux Device Driver on this but I don't get it, can someone give an example to eleborate?:
Once we get past that call, something has woken us up, but we do not know what. One possibility is that the process received a signal. The if statement that contains the wait_event_interruptible call checks for this case. This statement ensures the proper and expected reaction to signals, which could have been responsible for waking up the process (since we were in an interruptible sleep). If a signal has arrived and it has not been blocked by the process, the proper behavior is to let upper layers of the kernel handle the event. To this end, the driver returns -ERESTARTSYS to the caller; this value is used internally by the virtual filesystem (VFS) layer, which either restarts the system call or returns -EINTR to user space. We use the same type of check to deal with signal handling for every read and write implementation.