驱动篇:支持轮询操作的 globalfifo 驱动
发布日期:2021-06-29 11:34:47
浏览次数:2
分类:技术文章
本文共 1983 字,大约阅读时间需要 6 分钟。
驱动篇:支持轮询操作的 globalfifo 驱动
1.globalfifo 驱动中增加轮询操作
在 globalfifo 的 poll()函数中, 首先将设备结构体中的 r_wait 和 w_wait 等待队列头添加到等待列表,然后通过判断 dev->current_len 是否等于 0 来获得设备的可读状态,通过判断 dev->current_len 是否等于 GLOBALF IFO_ SIZE 来获得设备的可写状态static unsigned int globalfifo_poll(struct file *filp, poll_table *wait) { unsigned int mask = 0;struct globalfifo_dev *dev = filp->private_data; /*获得设备结构体指针*/down(&dev->sem); poll_wait(filp, &dev->r_wait, wait); poll_wait(filp, &dev->w_wait, wait); /*fifo 非空*/ if (dev->current_len != 0) { mask |= POLLIN | POLLRDNORM; /*标示数据可获得*/ } /*fifo 非满*/ if (dev->current_len != GLOBALFIFO_SIZE) { mask |= POLLOUT | POLLWRNORM; /*标示数据可写入*/} up(&dev->sem); return mask; }
把 globalfifo_poll 赋给 globalfifo_fops 的 poll 成员
static const struct file_operations globalfifo_fops ={ ...poll = globalfifo_poll,...};
2.在用户空间验证 globalfifo 设备的轮询
监控 globalfifo 是否可非阻塞读写的应用程序#include ... #define FIFO_CLEAR 0x1 #define BUFFER_LEN 20 main() { int fd, num;char rd_ch[BUFFER_LEN];fd_set rfds,wfds; //读/写文件描述符集 /*以非阻塞方式打开/dev/globalfifo 设备文件*/ fd = open("/dev/globalfifo", O_RDONLY | O_NONBLOCK); if (fd != - 1) { /*FIFO 清零*/ if (ioctl(fd, FIFO_CLEAR, 0) < 0) { printf("ioctl command failed\n"); } while (1) { FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(fd, &rfds); FD_SET(fd, &wfds); select(fd + 1, &rfds, &wfds, NULL, NULL); /*数据可获得*/ if (FD_ISSET(fd, &rfds)) { printf("Poll monitor:can be read\n"); } /*数据可写入*/ if (FD_ISSET(fd, &wfds)) { printf("Poll monitor:can be written\n"); } } } else { printf("Device open failure\n"); } }
运行时看到, 当没有任何输入,即 FIFO 为空时,程序不断地输出“Poll monitor:can be written” ;当通过 echo 向/dev/globalfifo 写入一些数据后, 将输出“Poll monitor:can be read”和“Poll monitor:can be written”如果不断地通过 echo 向/dev/globalfifo 写入数据直至写满 FIFO,发现 pollmonitor 程序将只输出“Poll monitor:can be read” 。对于 globalfifo 而言,不会出现既不能读、又不能写的情况
转载地址:https://blog.csdn.net/zytgg123456/article/details/109984121 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月11日 14时30分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
excel的逻辑函数
2019-04-29
excel文本函数
2019-04-29
excel时间日期函数
2019-04-29
excel查找引用函数
2019-04-29
棱镜门事件
2019-04-29
拼多多远程删除用户照片事件
2019-04-29
power BI desktop
2019-04-29
中国信息化发展八大趋势(一)
2019-04-29
中国信息化发展八大趋势(二)
2019-04-29
中国信息化发展八大趋势(三)
2019-04-29
中国信息化发展八大趋势(四)
2019-04-29
电商大战二十年
2019-04-29
神奇的麦考林
2019-04-29
centos7使用rpm安装mysql遇到的error: Failed dependencies: mysql-community-common(x86-64) 5.7.9 is needed
2019-04-29
spark报错:warn util.utils::service ‘sparkUI‘ can not bind on part 4040.Attempting port 4041.4042等错误
2019-04-29
Error getting authority: Error initializing authority: Could not connect: No such file or directory
2019-04-29
编程程软件测试思维方式:如何科学制定测试计划
2019-04-29
BLE蓝牙4.0串口调试助手
2019-04-29
树莓派WIFI设置
2019-04-29