驱动篇:支持轮询操作的 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:驱动篇:异步通知与异步 I/O(二)
下一篇:驱动篇:内存与 I/O 访问(一)(摘录)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月11日 14时30分09秒