阻塞与非阻塞I/O
阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后再进行操作。
被挂起的进程进入休眠状态,被移出调度器的运行队列,直到等待的条件被满足。
非阻塞操作的进程在不能进行设备操作时,并不挂起,它或者放弃,或者不停地查询,直到可用进行
操作为止。
驱动程序通常需要提供这样的能力,支持阻塞和非阻塞的进行read(),write等系统调用。
也就是应用程序调用-->系统调用(read(),write())-->调用驱动程序(字符设备)/文件系统(块设备)的xxx_read(),
xxx_write(),而阻塞和非阻塞就在这些xxx函数中实现。
等待队列
linux驱动程序中,可以使用等待队列(wait queue)来实现阻塞进程的唤醒,它与队列为基础数据结构与进程调
度机制紧密结合,能够用于实现内核中的异步事件通知机制,同时等待队列可以用来同步对系统资源的访问,例
如信号量在内核中就是依赖等待队列实现。
/*当条件condition满足立即返回,否则阻塞等待condition满足*/ #define wait_event(wq, condition) \ do { \ if (condition) /*条件满足立即返回*/ \ break; \ __wait_event(wq, condition);/*添加等待队列阻塞*/ \ } while (0) #define __wait_event(wq, condition) \ do { \ DEFINE_WAIT(__wait); \ \ for (;;) { \ prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ if (condition) \ break; \ schedule(); /*将自己调度出去,放弃cpu*/ \ } \ finish_wait(&wq, &__wait); \ } while (0) void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state) { unsigned long flags; wait->flags &= ~WQ_FLAG_EXCLUSIVE; spin_lock_irqsave(&q->lock, flags); if (list_empty(&wait->task_list)) __add_wait_queue(q, wait); /*加入等待队列*/ set_current_state(state); /*设置进程状态*/ spin_unlock_irqrestore(&q->lock, flags); } void finish_wait(wait_queue_head_t *q, wait_queue_t *wait) { unsigned long flags; __set_current_state(TASK_RUNNING); /* * We can check for list emptiness outside the lock * IFF: * - we use the "careful" check that verifies both * the next and prev pointers, so that there cannot * be any half-pending updates in progress on other * CPU's that we haven't seen yet (and that might * still change the stack area. * and * - all other users take the lock (ie we can only * have _one_ other CPU that looks at or modifies * the list). */ if (!list_empty_careful(&wait->task_list)) { spin_lock_irqsave(&q->lock, flags); list_del_init(&wait->task_list); spin_unlock_irqrestore(&q->lock, flags); } }唤醒等待队列
void wake_up(wait_queue_head_t * queue):
void wake_up_interruptible(wait_queue_head_t * queue):
它们唤醒以queue作为等待队列头的所有等待队列中所有属于该等待队列头的等待队列对应的进程。
wake_up()应该与wait_event()或wait_event_timeout()成对使用,wake_up()可唤醒处于TASK_INTERRUPTIBLE
和TASK_UNINTERRUPTIBLE的进程。
wake_up_interruptible()应该与wait_event_interruptible()或wait_event_interruptible_timeout()成对使用,
wake_up_interruptible()只能唤醒处于TASK_INTERRUPTIBLE的进程。
在等待队列上睡眠
sleep_on(wait_queue_head_t * q);作用将目前进程的状态置成TASK_UNINTERRUPTIBLE,并定义一个等待队列,之后
把它附属到等待队列头q,直到资源可获得,q引导的等待队列被唤醒。
interruptible_sleep_on(wait_queue_head_t * q);它和sleep_on()作用类似只不过是将当期进程的状态置成TASK_INTERRUPTIBLE
同时,q引导的等待队列被唤醒或者进程收到信号。
sleep_on()应该和wake_up()成对使用,interruptible_sleep_on()应该与wake_up_interruptible()成对使用。
在许多设备驱动中,并不调用sleep_on()或者interruptible_sleep_on(),而是亲自进行进程的状态改变和切换,代码如下
static ssize_t xxx_write(struct file *file,const char *buffer, size_t count, loff_t *ppos) { ... DECLARE_WAITQUEUE(wait, current); /*定义等待队列*/ add_wait_queue(&xxx_wait, &wait); /*添加等待队列*/ ret = count; /*等待设备缓冲区可写*/ do{ avail = device_writable(...); if(avail < 0) __set_current_state(TASK_INTERRUPTIBLE); /*改变进程状态*/ if(avail < 0){ if(file->f_flags & O_NONBLOCK){ /*非阻塞*/ if(!ret) ret = -EAGAIN; goto out; } schedule(); /*调度其他进程执行*/ if(signal_pending(current)){ if(!ret) ret = -ERESTARTSYS; goto out; } } }while(avail < 0); /*写设备缓冲区*/ device_write(...); out: remove_wait_queue(&xxx_wait, &wait); /*将等待队列移出等待队列头*/ set_current_state(TASK_RUNNING); /*设置进程状态为TASK_RUNNING*/ return ret; }1,如果是非阻塞访问(O_NONBLOCK被设置),设备忙时,直接返回“-EAGAIN” 2,对于阻塞访问,会进行状态切换并显示通过“schudle()”调度其他进程执行 3,醒来的时候要注意,由于调度出去的时候,进程状态是TASK_INTERRUPTIBLE,即浅度睡眠,因此唤醒它的有可能是信号,因此 我们首先通过“signal_pending(current)” 了解是不是信号唤醒的,如果是,立即返回“-ERESTARTSYS”。