Linux驱动开发5——同步机制

上一章讲到了并发,指的是多个进程同时存取临界区资源的处理机制。这一章讲的同步机制,讲的是多个进程之间协同工作的处理机制,如临界区数据还没有准备好,A进程负责准备数据,B进程等待A进程完成之后读取数据。

同步机制分为阻塞I/O和非阻塞I/O两种,前者等待数据准备就绪,后者立即返回。

1、阻塞I/O

1.1、等待队列(read/write中使用)

读进程进入等待队列睡眠,写进程准备好数据后,唤醒读进程。

#include <linux/wait.h>

初始化
DECLARE_WAIT_QUEUE_HEAD(wq);
或者
void init_waitqueue_head(wait_queue_head_t *wq);


进入等待队列,知道condition为真
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 *wq);
void wake_up_interruptible(wait_queue_head_t *wq);

2、非阻塞I/O

2.1、poll

unsigned int poll(struct file *filp, poll_table *wait);

2.2、select

2.3、epoll

原文地址:https://www.cnblogs.com/justin-y-lin/p/10692216.html