Linux内核阻塞IO(wait_queue)和非阻塞IO(轮询poll)

阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后再进行操作。被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。而非阻塞的进程在不能进行设备操作时,并不挂起,它或者放弃,或者不停地查询,直到可以操作为止。

int fd;
int data = 0;
fd = open("/dev/xxx_dev", O_RDWR); /* 阻塞方式打开 */
ret = read(fd, &data, sizeof(data)); /* 读取数据*/

int fd;
int data = 0;
fd = open("/dev/xxx_dev", O_RDWR | O_NONBLOCK); /* 非阻塞方式打开 */
ret = read(fd, &data, sizeof(data)); /* 读取数据 */

一、等待队列

在linux驱动程序中,可以使用等待队列(wait queue)来实现阻塞进程的唤醒。

阻塞访问最大的好处就是当设备文件不可操作的时候进程可以进入休眠态,这样可以将CPU 资源让出来。但是,当设备文件可以操作的时候就必须唤醒进程,一般在中断函数里面完成唤醒工作。

1. 等待队列头

一个等待队列有一个“等待队列头”来管理,wait_queue_head_t定义在linux/wait.h,实现在kernel/wait.c中。

struct __wait_queue_head {
    spinlock_t      lock;
    struct list_head    task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
DECLARE_WAIT_QUEUE_HEAD(name);  //静态  等价于下面两行
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);

2. 定义等待队列

等待队列头就是一个等待队列的头部,每个访问设备的进程都是一个队列项,当设备不可用的时候就要将这些进程对应的等待队列项添加到等待队列里面。

typedef struct __wait_queue wait_queue_t;
typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);

struct __wait_queue {
    unsigned int        flags;
#define WQ_FLAG_EXCLUSIVE   0x01
    void            *private;
    wait_queue_func_t   func;
    struct list_head    task_list;
};

DECLARE_WAIT_QUEUE(name, tsk);

该宏用于定义并初始化一个名为name的等待队列。

name 就是等待队列项的名字,tsk 表示这个等待队列项属于哪个任务(进程),一般设置为current,在 Linux内核中current相当于一个全局变量 , 表当前进程 。 因此宏DECLARE_WAITQUEUE 就是给当前正在运行的进程创建并初始化了一个等待队列项。

#define __WAITQUEUE_INITIALIZER(name, tsk) {                \
    .private    = tsk,                      \
    .func       = default_wake_function,            \
    .task_list  = { NULL, NULL } }

#define DECLARE_WAITQUEUE(name, tsk)                    \
    wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)

3. 移除和添加等待队列

extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);

4. 唤醒队列

void wake_up(wait_queue_head_t *queue);
void wake_up_interruptible(wait_queue_head_t *queue);

参数 q 就是要唤醒的等待队列头,这两个函数会将这个等待队列头中的所有进程都唤醒。

wake_up函数可以唤醒处于TASK_INTERRUPTIBLE和TASK_UNINTERRUPTIBLE状态的进程,而wake_up_interruptible函数只能唤醒处于 TASK_INTERRUPTIBLE 状态的进程。

5. 等待事件

除了主动唤醒以外,也可以设置等待队列等待某个事件,当这个事件满足以后就自动唤醒等待队列中的进程。

#define wait_event(wq, condition) 
#define wait_event_timeout(wq, condition, timeout) 
#define wait_event_interruptible(wq, condition)
#define wait_event_interruptible_timeout(wq, condition, timeout) 
/**
 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
 * @wq: the waitqueue to wait on
 * @condition: a C expression for the event to wait for
 * @timeout: timeout, in jiffies
 *
 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 * @condition evaluates to true or a signal is received.
 * The @condition is checked each time the waitqueue @wq is woken up.
 *
 * wake_up() has to be called after changing any variable that could
 * change the result of the wait condition.
 *
 * Returns:
 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
 * a signal, or the remaining jiffies (at least 1) if the @condition
 * evaluated to %true before the @timeout elapsed.
 */

等待第一个参数wq作为等待队列头的等待队列被唤醒,而且第2个参数condition必须满足,否则继续阻塞。

timeout以jiffy为单位。

6. 在等待队列上睡眠

extern void sleep_on(wait_queue_head_t *q);
extern long sleep_on_timeout(wait_queue_head_t *q, signed long timeout);
extern void interruptible_sleep_on(wait_queue_head_t *q);
extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, signed long timeout);

将当前进程添加到等待队列中,从而在等待队列上睡眠。当超时发生时,进程被唤醒。

使用等待队列实现阻塞访问重点注意两点:

  1. 将任务或者进程加入到等待队列头;
  2. 在合适的点唤醒等待队列,一般都是中断处理函数里面。

二、轮询

应用层可使用select、poll和epoll实现轮询访问设备目的。

当应用程序调用 select 或 poll 函数来对驱动程序进行非阻塞访问的时候,驱动程序file_operations 操作集中的poll函数就会执行。

unsigned int (*poll) (struct file *filp, struct poll_table_struct *wait)

wait:结构体 poll_table_struct 类型指针,由应用程序传递进来的。一般将此参数传递给poll_wait 函数。

返回值:向应用程序返回设备或者资源状态,可以返回的资源状态如下:

POLLIN     有数据可以读取。
POLLPRI       有紧急的数据需要读取。
POLLOUT    可以写数据。
POLLERR    指定的文件描述符发生错误。
POLLHUP    指定的文件描述符挂起。
POLLNVAL    无效的请求。
POLLRDNORM 等同于 POLLIN,普通数据可读

在驱动程序的 poll 函数中调用 poll_wait 函数,poll_wait 函数不会引起阻塞,只是将应用程序添加到 poll_table 中,poll_wait 函数原型如下:

void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)

参数 wait_address 是要添加到 poll_table 中的等待队列头,参数 p 就是 poll_table,就是file_operations 中 poll 函数的 wait 参数。

// read函数判断O_NONBLOCK,不可用时返回-EAGAIN,可用,直接返回结果
    if(filp->f_flags & O_NONBLOCK){
        if(atomic_read(&dev->releasekey) == 0){
            return -EAGAIN;
        }
    } else {}

unsigned int chrtest_poll(struct file *filp, 
    struct poll_table_struct *wait)
{
   unsigned int mask = 0; 
   struct chrtest_dev *dev = (struct chrtest_dev*)filp->private_data;

   poll_wait(filp, &dev->r_wait, wait);
   
   if(atomic_read(&dev->releasekey)){
       mask = POLLIN | POLLRDNORM;
   }

   return mask;
}

static struct file_operations chrtest_fops = {
    .owner = THIS_MODULE,
    .open = chrtest_open,
    .read = chrtest_read,
    .poll = chrtest_poll,
};

Linux 内核源码分析

整理了一些Linux内核源码学习书籍、视频资料,有需要的可以自行添加学习交流群:739729163 领取!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值