驱动开发,IO模型之IO多路复用实现过程,select方式

2023-09-14 19:54:08

1.IO多路复用简介

        当在应用程序中同时实现对多个硬件数据读取时就需要用到IO多路复用。io多路复用有select/poll/epoll三种实现方式。如果进程同时监听的多个硬件数据都没有准备好,进程切换进入休眠状态,当一个或者多个硬件数据准备就绪后,休眠的进程被唤醒,读取准备好的硬件数据。

         本实验监听自定义事件和鼠标事件;

 

2.代码

---pro1.c---应用程序(IO多路复用)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>

int main(int argc, const char *argv[])
{
    char buf[128] = {0};
    int fd1,fd2;
    fd1 = open("/dev/input/mouse0", O_RDWR);
    if (fd1 < 0)
    {
        printf("鼠标事件文件失败\n");
        exit(-1);
    }

    fd2 = open("/dev/myled0", O_RDWR);
    if (fd2 < 0)
    {
        printf("自定义事件文件失败\n");
        exit(-1);
    }

    //定义事件集合
    fd_set readfds;
    while(1)
    {
        //清空事件集合
        FD_ZERO(&readfds);
        //将要监听的文件描述符添加到可读集合中
        FD_SET(fd1,&readfds);
        FD_SET(fd2,&readfds);
        
        //等待事件就绪
        int ret =  select(fd2+1,&readfds,NULL,NULL,NULL);
        if(ret < 0)
        {
            printf("select err\n");
            exit(-1);
        }
        //判断事件的发生
        if(FD_ISSET(fd1,&readfds))
        {
            read(fd1,buf,sizeof(buf));
            printf("鼠标事件发生%s\n",buf);
            memset(buf,0,sizeof(buf));
        }
        if(FD_ISSET(fd2,&readfds))
        {
            read(fd2,buf,sizeof(buf));
            printf("自定义设备事件发生%s\n",buf);
            memset(buf,0,sizeof(buf));
        }
    }
    
    close(fd1);
    close(fd2);

    return 0;
}
---pro2.c---应用程序(模拟自定义设备数据就绪)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
    char buf[128] = "hello world";
    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }

    write(fd, buf, sizeof(buf));

    close(fd);

    return 0;
}
---mycdev.c---驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include "head.h"
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include<linux/poll.h>

char kbuf[128] = {0};
unsigned int major;
struct class *cls;
struct device *dev;
unsigned int condition = 0;

// 定义一个等待队列头
wait_queue_head_t wq_head;

// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    // 判断IO方式
   /* if (file->f_flags & O_NONBLOCK) // 非堵塞
    {
    }
    else
    {
        // 堵塞,先检查condition再将进程休眠
        wait_event_interruptible(wq_head, condition);
    }*/
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_ user err\n");
        return -EIO;
    }
    condition = 0; // 下一次硬件数据没有就绪

    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    // 从用户拷贝数据,模拟硬件数据
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user err\n");
        return -EIO;
    }

    condition = 1;
    wake_up_interruptible(&wq_head);

    return 0;
}
//封装POLL方法
__poll_t mycdev_poll(struct file *file, struct poll_table_struct *wait)
{
    __poll_t mask = 0;
    //向上提交等待队列头
    poll_wait(file,&wq_head,wait);
    //根据事件是否发生给一个合适的返回值
    if(condition)
    {
        mask = POLLIN;
    }
    
    return mask;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .poll = mycdev_poll,
    .write = mycdev_write,
    .release = mycdev_close,
};

// 入口函数
static int __init mycdev_init(void)
{
    //初始化等待队列
    init_waitqueue_head(&wq_head);

    major = register_chrdev(0, "myled", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);

    // 向上提交目录
    cls = class_create(THIS_MODULE, "MYLED");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");

    // 向上提交设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点信息成功\n");

    return 0;
}

// 出口函数
static void __exit mycdev_exit(void)
{
    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    // 销毁目录信息
    class_destroy(cls);

    // 字符设备驱动注销
    unregister_chrdev(major, "myled");
}

// 声明
// 入口函数地址
module_init(mycdev_init);
// 出口函数地址
module_exit(mycdev_exit);
// 遵循的GPL协议
MODULE_LICENSE("GPL");

3.测试结果

        执行pro2.c,自定义事件被监听到;

        在ubuntu上动鼠标,鼠标事件被监听;

更多推荐

容器内也能运行图形化应用?Distrobox 为容器注入生命 | 开源日报 No.35

JetBrains/compose-multiplatformStars:13.3kLicense:Apache-2.0ComposeMultiplatform是一个使用Kotlin在多个平台上共享UI的声明性框架。它基于JetpackCompose,由JetBrains和开源贡献者开发。您可以选择使用ComposeM

django和celery的项目,nginx和uwsgi协议,在通过api端口进行deeplearning任务的训练和排队

问题汇总redis安装django和celery的安装nginx和uwsgi的安装一.Django的项目,有个runserver直接起了一个webserver,为什么还要Nginx包一层,起一个webserver呢?Nginx的性能比Django自带的Webserver的性能要好,python写的程序,deeplab想

怎样提高redis的命中率

要提高Redis缓存命中率,可以考虑以下几个方面:合理设置缓存过期时间:根据业务需求和数据更新频率,设置适当的缓存过期时间。过长的过期时间可能导致数据不及时更新,而过短的过期时间则可能导致频繁的缓存失效。选择合适的数据结构:根据具体业务场景选择合适的Redis数据结构。例如,使用Hash类型来存储对象,使用Sorted

DEM格式转换:转换NSDTF-DEM国标数据格式为通用格式,使用ArcGIS工具转换NSDTF-DEM国标.dem文件为通用.tif格式。

DEM格式转换:转换NSDTF-DEM国标数据格式为通用格式,使用ArcGIS工具转换NSDTF-DEM国标.dem文件为通用.tif格式。*.dem是一种比较常见的DEM数据格式,其有两种文件组织方式,即NSDTF-DEM和USGS-DEM。(1)NSDTF-DEM是一种明码的中国国家标准空间数据的交换格式,遵从国家

浅谈消防设备电源监控系统在高层民用建筑内的应用

【摘要】:当高层民用建筑内火灾发生时,各类消防设备能否正常运行、能否发挥作用是初期火灾扑救是否成功的重要条件之一,而稳定可靠的消防设备电源则是消防设备正常工作的保障。因此针对高层民用建筑内消防设备电源的监测系统至关重要。【关键词】:消防设备电源;AFPM100/B1;电压/电流传感器;高层民用建筑0前言为扎实推进高层民

【全志V3s】SPI NAND Flash 驱动开发

文章目录一、硬件介绍V3s的启动顺序二、驱动支持U-Boot驱动主线Linux驱动已经支持三、烧录工具xfel四、构建U-Boot(官方的Uboot)先编译一下开始spinandflash代码层面的适配修改menuconfig配置ARMarchitecture配置SupportforSPINandFlashonAllw

MySQL远程登录提示Access denied的场景

厂商给的某个MySQL库,通过客户端远程登录,提示这个错误,Accessdeniedforuser'用户名'@'IP'(usingpassword:YES)确认输入的账号密码都是正确的,出现这个错误说明端口是通的。此时可以检索mysql.user,如果待登录账号的记录host字段是localhost,说明仅允许本地登录

Hbuilder本地调试微信H5项目(一)

摘要通过内网穿透,访问本地Hbuilder创建的Vue项目前置准备下载并安装【HBuilder】,本文用的是HBuilder3.8.12版本,下载地址下载并安装【微信开发者工具】,本文用的是1.06版本,下载地址下载并安装【natapp】,下载地址实现逻辑本地使用Hbuilder进行开发并运行起来(配置为80端口)使用

【golang】深入理解GMP调度模型

GoroutineGo中,协程被称为goroutine,它非常轻量,一个goroutine只占几KB,并且这几KB就足够goroutine运行完,这就能在有限的内存空间内支持大量goroutine,支持了更多的并发,虽然一个goroutine的栈只占几KB(Go语言官方说明为4~5KB),但实际是可伸缩的,如果需要更多

性能测试 —— Jmeter定时器

固定定时器如果你需要让每个线程在请求之前按相同的指定时间停顿,那么可以使用这个定时器;需要注意的是,固定定时器的延时不会计入单个sampler的响应时间,但会计入事务控制器的时间1、使用固定定时器位置在http请求中;每次http请求前延迟3秒;配置路径——定时器——固定定时器;如下图:2、线程组循环3次,通过表格查看

启山智软/电商商城100%开源

介绍想要了解代码规范,学习商城解决方案,点击下方官网链接联系客服作者:启山智软官网及博客:启山智软官网、CSDN、掘金、gitee简介:启山智软目前开发了全渠道电商商城系统,本商城是基于SpringCloud的商城系统,百万真实用户沉淀并检验的商城。注意:该项目只提供学习,切勿用于商业用途电商商城是什么:电商商城指的是

热文推荐