简介


1 int fd;
2 int data = 0;
3
4 fd = open("/dev/xxx_dev", O_RDWR | O_NONBLOCK); /* 非阻塞方式打开 */
5 ret = read(fd, &data, sizeof(data)); /* 读取数据 */
等待队列
等待队列头
阻塞-->中断唤醒-->可以考虑等待队列
39 struct __wait_queue_head {
40 spinlock_t lock;
41 struct list_head task_list;
42 };
43 typedef struct __wait_queue_head wait_queue_head_t;
初始化等待队列头
void init_waitqueue_head(wait_queue_head_t *q)
或者
DECLARE_WAIT_QUEUE_HEAD
等待队列项
struct __wait_queue {
unsigned int flags;
void *private;
wait_queue_func_t func;
struct list_head task_list;
};
typedef struct __wait_queue wait_queue_t;
初始化的等待队列项
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)
等待唤醒
void wake_up(wait_queue_head_t *q) //能唤醒 TASK_INTERRUPTIBLE 和 TASK_UNINTERRUPTIBLE 状态
的进程
void wake_up_interruptible(wait_queue_head_t *q) //只能唤醒处于 TASK_INTERRUPTIBLE 状态的进程
等待事件
除了主动唤醒以外,也可以设置等待队列等待某个事件,当这个事件满足以后就自动唤醒等待队列中的进程。
| wait_event(wq, condition) | 等待以 wq 为等待队列头的等待队列被唤醒,前 提是 condition 条件必须满足(为真),否则一直阻 塞 。 此 函 数 会 将 进 程 设 置 为 TASK_UNINTERRUPTIBLE 状态 |
| wait_event_timeout(wq, condition, timeout) | 功能和 wait_event 类似,但是此函数可以添加超时时间,以 jiffies 为单位。此函数有返回值,如果返回 0 的话表示超时时间到,而且 condition为假。为 1 的话表示 condition 为真,也就是条件满足了。 |
| wait_event_interruptible(wq, condition) | 与 wait_event 函数类似,但是此函数将进程设置为 TASK_INTERRUPTIBLE,就是可以被信号打断。 |
| wait_event_interruptible_timeout(wq, condition, timeout) | 与 wait_event_timeout 函数类似,此函数也将进程设置为 TASK_INTERRUPTIBLE,可以被信号打断。 |
轮询
非阻塞-->轮询
poll、epoll 和 select 可以用于处理轮询,应用程序通过 select、epoll 或 poll 函数来查询设备是否可以操作,如果可以操作的话就从设备读取或者向设备写入数据。当应用程序调用 select、epoll 或 poll 函数的时候设备驱动程序中的 poll 函数就会执行,因此需要在设备驱动程序中编写 poll 函数。
用户空间 select、epoll 或 poll 函数
select 函数
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout)
返回值:0,表示的话就表示超时发生,但是没有任何文件描述符可以进行操作;-1,发生
错误;其他值,可以进行操作的文件描述符个数。
当我们定义好一个 fd_set 变量以后可以使用如下所示几个宏进行操作:
void FD_ZERO(fd_set *set) //将 fd_set 变量的所有位都清零
void FD_SET(int fd, fd_set *set) //是向 fd_set 添加一个文件描述符
void FD_CLR(int fd, fd_set *set) //将一个文件描述符从 fd_set 中删除
int FD_ISSET(int fd, fd_set *set) //用于测试一个文件是否属于某个集合
示例
1 void main(void)
2 {
3 int ret, fd; /* 要监视的文件描述符 */
4 fd_set readfds; /* 读操作文件描述符集 */
5 struct timeval timeout; /* 超时结构体 */
6
7 fd = open("dev_xxx", O_RDWR | O_NONBLOCK); /* 非阻塞式访问 */
8
9 FD_ZERO(&readfds); /* 清除 readfds */
10 FD_SET(fd, &readfds); /* 将 fd 添加到 readfds 里面 */
11
12 /* 构造超时时间 */
13 timeout.tv_sec = 0;
14 timeout.tv_usec = 500000; /* 500ms */
15
16 ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
17 switch (ret) {
18 case 0: /* 超时 */
19 printf("timeout!\r\n");
20 break;
21 case -1: /* 错误 */
22 printf("error!\r\n");
23 break;
24 default: /* 可以读取数据 */
25 if(FD_ISSET(fd, &readfds)) { /* 判断是否为 fd 文件描述符 */
26 /* 使用 read 函数读取数据 */
27 }
28 break;
29 }
30 }
poll 函数
select 函数能够监视的文件描述符数量有最大的限制,一般为 1024, poll 函数没有最大文件描述符限制。
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
struct pollfd {
int fd; /* 文件描述符 */
short events; /* 请求的事件 */
short revents; /* 返回的事件 */
};nfds:poll 函数要监视的文件描述符数量。
返回值:发生事件或错误的文件描述符数量;0:超时;-1:发生错误,并且设置 errno 为错误类型。
fd 是要监视的文件描述符,如果 fd 无效的话那么 events 监视事件也就无效,并且 revents返回 0。events 是要监视的事件,可监视的事件类型如下所示:
POLLIN 有数据可以读取。
POLLPRI 有紧急的数据需要读取。
POLLOUT 可以写数据。
POLLERR 指定的文件描述符发生错误。
POLLHUP 指定的文件描述符挂起。
POLLNVAL 无效的请求。
POLLRDNORM 等同于 POLLIN
示例
1 void main(void)
2 {
3 int ret;
4 int fd; /* 要监视的文件描述符 */
5 struct pollfd fds;
6
7 fd = open(filename, O_RDWR | O_NONBLOCK); /* 非阻塞式访问 */
8
9 /* 构造结构体 */
10 fds.fd = fd;
11 fds.events = POLLIN; /* 监视数据是否可以读取 */
12
13 ret = poll(&fds, 1, 500); /* 轮询文件是否可操作,超时 500ms */
14 if (ret) { /* 数据有效 */
15 ......
16 /* 读取数据 */
17 ......
18 } else if (ret == 0) { /* 超时 */
19 ......
20 } else if (ret < 0) { /* 错误 */
21 ......
22 }
23 }
epoll 函数(主要有三个)
传统的 selcet 和 poll 函数都会随着所监听的 fd 数量的增加,出现效率低下的问题,而且
poll 函数每次必须遍历所有的描述符来检查就绪的描述符,这个过程很浪费时间。为此,epoll
应运而生,epoll 就是为处理大并发而准备的,一般常常在网络编程中使用 epoll 函数。
int epoll_create(int size)
//size:从 Linux2.6.8 开始此参数已经没有意义了,随便填写一个大于 0 的值就可以。
//返回值:epoll 句柄,如果为-1 的话表示创建失败。
int epoll_ctl(int epfd, int op, int fd,struct epoll_event *event)
//epfd:要操作的 epoll 句柄,也就是使用 epoll_create 函数创建的 epoll 句柄。
//op:表示要对 epfd(epoll 句柄)进行的操作,可以设置为:
EPOLL_CTL_ADD 向 epfd 添加文件参数 fd 表示的描述符。
EPOLL_CTL_MOD 修改参数 fd 的 event 事件。
EPOLL_CTL_DEL 从 epfd 中删除 fd 描述符。
//fd:要监视的文件描述符。
//event:要监视的事件类型
struct epoll_event {
uint32_t events; /* epoll 事件 */
epoll_data_t data; /* 用户数据 */
};其中可选的event如下:
EPOLLIN 有数据可以读取。
EPOLLOUT 可以写数据。EPOLLPRI 有紧急的数据需要读取。
EPOLLERR 指定的文件描述符发生错误。
EPOLLHUP 指定的文件描述符挂起。
EPOLLET 设置 epoll 为边沿触发,默认触发模式为水平触发。
EPOLLONESHOT 一次性的监视,当监视完成以后还需要再次监视某个 fd,那么就需要将
fd 重新添加到 epoll 里面。
int epoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout)
epoll 更多的是用在大规模的并发服务器上,当设计到的文件描述符(fd)比较少的时候就适合用 selcet 和 poll。
驱动下面的poll 操作函数
unsigned int (*poll) (struct file *filp, struct poll_table_struct *wait)
//filp:要打开的设备文件(文件描述符)。
//wait:结构体 poll_table_struct 类型指针,由应用程序传递进来的。一般将此参数传递给poll_wait
函数。
//返回值:向应用程序返回设备或者资源状态
poll可以返回的资源状态如下:
POLLIN 有数据可以读取。
POLLPRI 有紧急的数据需要读取。
POLLOUT 可以写数据。
POLLERR 指定的文件描述符发生错误。
POLLHUP 指定的文件描述符挂起。
POLLNVAL 无效的请求。
POLLRDNORM 等同于 POLLIN,普通数据可读
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 参数。
阻塞IO实验
./imx6uirqApp /dev/imx6uirq &
cpu占用率99.6%,!原因就在于我们是直接在 while 循环中通过 read 函数读取按键值,因此 imx6uirqApp 这个软件会一直运行,一直读取按键值,CPU 使用率肯定就会很高。最好的方法就是在没有有效的按键事件发生的时候,imx6uirqApp 这个应用程序应该处于休眠状态,当有按键事件发生以后 imx6uirqApp 这个应用程序才运行,打印出按键值。
66 /* @description : 中断服务函数,开启定时器
67 * 定时器用于按键消抖。
68 * @param - irq : 中断号
69 * @param - dev_id : 设备结构。
70 * @return : 中断执行结果
71 */
72 static irqreturn_t key0_handler(int irq, void *dev_id)
73 {
74 struct imx6uirq_dev *dev = (struct imx6uirq_dev*)dev_id;
75
76 dev->curkeynum = 0;
77 dev->timer.data = (volatile long)dev_id;
78 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10));
79 return IRQ_RETVAL(IRQ_HANDLED);
80 }
81
82 /* @description : 定时器服务函数,用于按键消抖,定时器到了以后
83 * 再次读取按键值,如果按键还是处于按下状态就表示按键有效。
84 * @param - arg : 设备结构变量
85 * @return : 无
86 */
87 void timer_function(unsigned long arg)
88 {
89 unsigned char value;
90 unsigned char num;
91 struct irq_keydesc *keydesc;
92 struct imx6uirq_dev *dev = (struct imx6uirq_dev *)arg;
93
94 num = dev->curkeynum;
95 keydesc = &dev->irqkeydesc[num];
96
97 value = gpio_get_value(keydesc->gpio); /* 读取 IO 值 */
98 if(value == 0){ /* 按下按键 */
99 atomic_set(&dev->keyvalue, keydesc->value);
100 }
101 else{ /* 按键松开 */
102 atomic_set(&dev->keyvalue, 0x80 | keydesc->value);
103 atomic_set(&dev->releasekey, 1);
104 }
105
106 /* 唤醒进程 */
107 if(atomic_read(&dev->releasekey)) { /* 完成一次按键过程 */
108 /* wake_up(&dev->r_wait); */
109 wake_up_interruptible(&dev->r_wait);
110 }
111 }
118 static int keyio_init(void)
119 {
120 unsigned char i = 0;
121 char name[10];
122 int ret = 0;
......
163 /* 创建定时器 */
164 init_timer(&imx6uirq.timer);
165 imx6uirq.timer.function = timer_function;
166
167 /* 初始化等待队列头 */
168 init_waitqueue_head(&imx6uirq.r_wait);
169 return 0;
170 }
179 static int imx6uirq_open(struct inode *inode, struct file *filp)
180 {
181 filp->private_data = &imx6uirq; /* 设置私有数据 */
182 return 0;
183 }
193 static ssize_t imx6uirq_read(struct file *filp, char __user *buf,
size_t cnt, loff_t *offt)
194 {
195 int ret = 0;
196 unsigned char keyvalue = 0;
197 unsigned char releasekey = 0;
198 struct imx6uirq_dev *dev = (struct imx6uirq_dev *)filp->private_data;
199
200 #if 0
201 /* 加入等待队列,等待被唤醒,也就是有按键按下 */
202 ret = wait_event_interruptible(dev->r_wait,atomic_read(&dev->releasekey));
203 if (ret) {
204 goto wait_error;
205 }
206 #endif
207
208 DECLARE_WAITQUEUE(wait, current); /* 定义一个等待队列 */
209 if(atomic_read(&dev->releasekey) == 0) { /* 没有按键按下 */
210 add_wait_queue(&dev->r_wait, &wait); /* 添加到等待队列头 */
211 __set_current_state(TASK_INTERRUPTIBLE);/* 设置任务状态 */
212 schedule(); /* 进行一次任务切换 */
213 if(signal_pending(current)) { /* 判断是否为信号引起的唤醒 */
214 ret = -ERESTARTSYS;
215 goto wait_error;
216 }
217 __set_current_state(TASK_RUNNING); /*设置为运行状态 */
218 remove_wait_queue(&dev->r_wait, &wait); /*将等待队列移除 */
219 }
220 keyvalue = atomic_read(&dev->keyvalue);
221 releasekey = atomic_read(&dev->releasekey);
...
234 return 0;
236 wait_error:
237 set_current_state(TASK_RUNNING); /* 设置任务为运行态 */
238 remove_wait_queue(&dev->r_wait, &wait); /* 将等待队列移除 */
239 return ret;
240
241 data_error:
242 return -EINVAL;
243 }
246 static struct file_operations imx6uirq_fops = {
247 .owner = THIS_MODULE,
248 .open = imx6uirq_open,
249 .read = imx6uirq_read,
250 };
251
257 static int __init imx6uirq_init(void)
258 {
259 /* 1、构建设备号 */
260 if (imx6uirq.major) {
261 imx6uirq.devid = MKDEV(imx6uirq.major, 0);
262 register_chrdev_region(imx6uirq.devid, IMX6UIRQ_CNT,IMX6UIRQ_NAME);
263 } else {
264 alloc_chrdev_region(&imx6uirq.devid, 0, IMX6UIRQ_CNT,IMX6UIRQ_NAME);
265 imx6uirq.major = MAJOR(imx6uirq.devid);
266 imx6uirq.minor = MINOR(imx6uirq.devid);
267 }
......
284
285 /* 5、始化按键 */
286 atomic_set(&imx6uirq.keyvalue, INVAKEY);
287 atomic_set(&imx6uirq.releasekey, 0);
288 keyio_init();
289 return 0;
290 }
291
297 static void __exit imx6uirq_exit(void)
298 {
299 unsigned i = 0;
300 /* 删除定时器 */
301 del_timer_sync(&imx6uirq.timer); /* 删除定时器 */
......
310 class_destroy(imx6uirq.class);
311 }
312
313 module_init(imx6uirq_init);
314 module_exit(imx6uirq_exit);
315 MODULE_LICENSE("GPL");
如果只是一味的while,read,则cpu的占用率高达99.7%。如果使用如上图所示的方式唤醒,cpu占用率为0%,几乎不占用cpu。
非阻塞IO 实验部分
195 static ssize_t imx6uirq_read(struct file *filp,char __user *buf,size_t cnt,loff_t *offt)
196 {
197 int ret = 0;
198 unsigned char keyvalue = 0;
199 unsigned char releasekey = 0;
200 struct imx6uirq_dev *dev = (struct imx6uirq_dev *)filp->private_data;
201
202 if (filp->f_flags & O_NONBLOCK) { /* 非阻塞访问 */
203 if(atomic_read(&dev->releasekey) == 0) /* 没有按键按下 */
204 return -EAGAIN;
205 } else { /* 阻塞访问 */
206 /* 加入等待队列,等待被唤醒,也就是有按键按下 */
207 ret = wait_event_interruptible(dev->r_wait,atomic_read(&dev->releasekey));
208 if (ret) {
209 goto wait_error;
210 }
211 }
......
229 wait_error:
230 return ret;
231 data_error:
232 return -EINVAL;
233 }
234
235 /*
236 * @description : poll 函数,用于处理非阻塞访问
237 * @param - filp : 要打开的设备文件(文件描述符)
238 * @param - wait : 等待列表(poll_table)
239 * @return : 设备或者资源状态,
240 */
241 unsigned int imx6uirq_poll(struct file *filp,struct poll_table_struct *wait)
242 {
243 unsigned int mask = 0;
244 struct imx6uirq_dev *dev = (struct imx6uirq_dev *)filp->private_data;
245
246 poll_wait(filp, &dev->r_wait, wait);
247
248 if(atomic_read(&dev->releasekey)) { /* 按键按下 */
249 mask = POLLIN | POLLRDNORM; /* 返回 PLLIN */
250 }
251 return mask;
252 }
253
254 /* 设备操作函数 */
255 static struct file_operations imx6uirq_fops = {
256 .owner = THIS_MODULE,
257 .open = imx6uirq_open,
258 .read = imx6uirq_read,
259 .poll = imx6uirq_poll,
260 };
261
267 static int __init imx6uirq_init(void)
268 {
......
298 keyio_init();
299 return 0;
300 }
307 static void __exit imx6uirq_exit(void)
308 {
309 unsigned i = 0;
310 /* 删除定时器 */
311 del_timer_sync(&imx6uirq.timer); /* 删除定时器 */
......
320 class_destroy(imx6uirq.class);
321 }
测试APP
30 int main(int argc, char *argv[])
31 {
32 int fd;
33 int ret = 0;
34 char *filename;
35 struct pollfd fds;
36 fd_set readfds;
37 struct timeval timeout;
38 unsigned char data;
39
40 if (argc != 2) {
41 printf("Error Usage!\r\n");
42 return -1;
43 }
44
45 filename = argv[1];
46 if (fd < 0) {
47 /* 非阻塞访问 */
48 printf("Can't open file %s\r\n", filename);
49 return -1;
50 }
51
52#if 0
53 /* 构造结构体 */
54 fds.fd = fd;
55 fds.events = POLLIN;
56
57 while (1) {
58 ret = poll(&fds, 1, 500);
59 if (ret) { /* 数据有效 */
60 ret = read(fd, &data, sizeof(data));
61 if(ret < 0) {
62 /* 读取错误 */
63 } else {
64 if(data)
65 printf("key value = %d \r\n", data);
66 }
67 } else if (ret == 0) {
68 /* 用户自定义超时处理 */
69 } else if (ret < 0) {
70 /* 用户自定义错误处理 */
71 }
72 }
73 #endif
74
75 while (1) {
76 FD_ZERO(&readfds);
77 FD_SET(fd, &readfds);
78 /* 构造超时时间 */
79 timeout.tv_sec = 0;
80 timeout.tv_usec = 500000; /* 500ms */
81 ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
82 switch (ret) {
83 case 0:/* 超时 */
84 /* 用户自定义超时处理 */
85 break;
86 case -1:/* 错误 */
87 /* 用户自定义错误处理 */
88 break;
89 default: /* 可以读取数据 */
90 if(FD_ISSET(fd, &readfds)) {
91 ret = read(fd, &data, sizeof(data));
92 if (ret < 0) {
93 /* 读取错误 */
94 } else {
95 if (data)
96 printf("key value=%d\r\n", data);
97 }
98 }
99 break;
100 }
101 }
102
103 close(fd);
104 return ret;
105 }
cpu占用率也不高。
739

被折叠的 条评论
为什么被折叠?



