Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

down_interruptible in Linux

I am having some difficulty in understanding the below statement from LDD3. "down_interruptible - it allows a user-space process that is waiting on a semaphore to be interrupted by the user".

A userspace application wont be direclty making the down_interruptible call. Lets say a device driver does and the application is put to sleep by the device driver triggered by a call to down_interruptible. Now how does a signal to the user space application invokes the application from sleep because its the device driver which called the down_interruptible not the application.

Somebody please clarify this to me.

like image 268
Joey Alex Avatar asked Oct 08 '15 05:10

Joey Alex


1 Answers

Any device driver does not run of its own, device driver run on behalf of a process via system calls.

Suppose any device driver invokes down_interruptible();, it means if semaphore is not available the respective process will be put on the semaphore wait-queue. And task state will be change to TASK_INTERRUPTIBLE and scheduler will invoked to run any other process. Now the sleeping process can be wake up either by the event for it waiting (semaphore) or by the signal.

Example: kill -SIGINT <pid> will cause the process to change its state to TASK_RUNNING and will add the process to run queue.

Here it is the pseudo code of wait queue, how a process wait for any event.

/* ‘q’ is the wait queue we wish to sleep on */
DEFINE_WAIT(wait);
add_wait_queue(q, &wait);
while (!condition) /* condition is the event that we are waiting for */
{
    prepare_to_wait(&q, &wait, TASK_INTERRUPTIBLE);
    if (!signal_pending(current))
    {
        schedule();
        continue;
    }
    ret = -ERESTARTSYS; 
}
finish_wait(&q, &wait);

In your example the process is added to wait queue and waiting for the condition to release it. Meanwhile it checks for any pending signal also, if there is any, it will return -ERESTARTSYS, otherwise again go for sleep.

like image 123
AnshuMan Gupta Avatar answered Sep 19 '22 10:09

AnshuMan Gupta