前言
阻塞和非阻塞io是两种不同的设备访问方式。
阻塞
阻塞IO表示在执行设备操作时若是不能获得资源,则会挂起等到满足条件时候再进行操作。被挂起之后进程进入睡眠,知道等待的条件被满足。
非阻塞
非阻塞IO表示在执行设备操作时候要是不能获得资源,则要么放弃,要么一直查询直到操作成功为止
## 应用层操作示例
int mem;
fd = open("abc.txt",O_RDWR);//以阻塞的方式打开abc.txt文件
ret = read(fd,&buf,sizeof(mem));//当adb.txt文件有东西时候才返回
fd_nblk = open("abc.txt",O_RDWR|O_NONBLOCK);//以非阻塞方式打开
while(read(fd,&buf,sizeof(mem))!=1)
continue;
fcntl(fd,F_SETFL,O_NONBLOCK)//可以设置fd对应的I/O为非阻塞。
一、等待队列
等待队列可以唤醒正在阻塞的进程。
1.等待队列头
#include <linux/wait.h>
struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);//初始化等待队列头
DECLARE_WAIT_QUEUE_HEAD (name);//定义并初始话等待队列头
2.等待队列
struct __wait_queue {
unsigned int flags;
void *private;
wait_queue_func_t func;
struct list_head task_list;
};
DECLARE_WAITQUEUE(name, tsk)//定义并初始化一个等待队列
void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);//向等待队列添加一个等待队列头
void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);/向等待队列删除一个等待队列头
//等待事件
wait_event(queue, condition) wait_event_interruptible(queue, condition)//可以被信号打断
wait_event_timeout(queue, condition, timeout)//超时返回
wait_event_interruptible_timeout(queue, condition, timeout)
//唤醒列队
void wake_up(wait_queue_head_t *queue); //可唤醒处于TASK_INTERRUPTIBLE和TASK_UNINTERRUPTIBLE的进程
void wake_up_interruptible(wait_queue_head_t*queue;//与上面wait_event_interruptible / wait_event_interruptible_timeout一起使用,只能唤醒TASK_INTERRUPTIBLE进程
//等待队列头睡眠
sleep_on(wait_queue_head_t *q ); //是将目前进程的状态置成TASK_UNINTERRUPTIBLE
interruptible_sleep_on(wait_queue_head_t *q );
#define TASK_RUNNING 0 //进程运行或者就绪状态
#define TASK_INTERRUPTIBLE 1//处于等待队伍中,等待资源有效时唤醒
#define TASK_UNINTERRUPTIBLE 2//处于等待队伍中,等待资源有效时唤醒,但不能被中断唤醒
#define __TASK_STOPPED 4 //进程停止状态
模板
wait_queue_head_t xxx_wait; /* 定义待队列头 */
init_waitqueue_head(&xxx_wait);
static ssize_t my_write(struct file *file, const char *buffer, size_t count,loff_t *ppos){
DECLARE_WAITQUEUE(wait, current);//定义等待队列元素
add_wait_queue(&xxx_wait, &wait);//添加等待队列头到等待队列上
/* 等待设备缓冲区可写 */
do {
//不可以写缓冲
if (enable< 0) {
if (file->f_flags &O_NONBLOCK) { //判断是否非阻塞
ret = -EAGAIN;
goto out;
}
//不可以写缓冲区 并且是阻塞状态
__set_current_state(TASK_INTERRUPTIBLE); //改变进程状态
schedule(); //调度其他进程执行
//判断是否为信号唤醒
if (signal_pending(current)) {
ret = -ERESTARTSYS;
goto out;
}
}
} while (enable< 0);
/* 写缓冲区 */
write_mem();
out:
remove_wait_queue(&xxx_wait, &wait); /* 将元素移出xxx_wait指引的队列 */
set_current_state(TASK_RUNNING); /* 设置进程状态为TASK_RUNNING */
return ret;
}
二、轮询
在应用层通常使用select ,poll ,epolll来对设备进行无阻塞访问,内核中对应file_operations中的poll函数指针
/*
对可能引起设备文件状态变化的等待队列调用poll_wait函数,将对应的等待队列头部添加到poll_table中。
返回表示是否能对设备进行无阻塞读、 写访问的掩码。
POLLIN无阻塞的读
POLLOUT无阻塞的写
*/
unsigned int (*poll) (struct file *, struct poll_table_struct *);
void poll_wait(struct file *filp, wait_queue_heat_t *queue, poll_table * wait);//不会引起阻塞
模板
wait_queue_head_t read_wait; /* 定义待队列头 */
init_waitqueue_head(&read_wait);
wait_queue_head_t write_wait; /* 定义待队列头 */
init_waitqueue_head(&write_wait);
static unsigned int xxx_poll(struct file *filp, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(filp, &read_wait, wait); /* 加入读等待队列 */
poll_wait(filp, &write_wait, wait); /* 加入写等待队列 */
if (read) /* 可读条件满足 */
mask |= POLLIN | POLLRDNORM; /* 标示数据可获得(对用户可读) */
if (write) /* 可读条件满足*/
mask |= POLLOUT | POLLWRNORM; /* 标示数据可写入*/
return mask;
}
总结
设备驱动中阻塞IO一般使用等待队列来实现。非阻塞IO在驱动中一般以poll来实现(应用层调用select poll 和epoll函数)。