驱动开发5 阻塞IO实例、IO多路复用

news2024/10/1 19:35:59

1 阻塞IO

进程1

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int a,b;
    int fd = open("/dev/myled0",O_RDWR);
    if(fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        memset(buf,0,sizeof(buf));
        read(fd,buf,sizeof(buf));
        printf("buf:%s\n",buf);
    }
}

进程2

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

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

驱动程序

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/uaccess.h>

int major;
char kbuf[128] = {0};

unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//定义等待队列
wait_queue_head_t wq_head;
unsigned int condition=0;
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)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    if(file->f_flags&O_NONBLOCK)//用户程序以非阻塞读写数据
    {}
    else{
        wait_event_interruptible(wq_head,condition);//判断condition的值,为假则进程休眠
    }
    //将获取到的硬件数据拷贝到用户
    int ret = copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    condition = 0; //表示下一次数据没有准备就绪
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    ret = copy_from_user(kbuf,ubuf,size);
    if (ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    condition = 1;//表示硬件数据就绪
    wake_up_interruptible(&wq_head);//唤醒休眠的进程
    return 0;
}
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,
    .write = mycdev_write,
    .release = mycdev_close,
};
static int __init mycdev_init(void)
{
    //初始化等待队列
    init_waitqueue_head(&wq_head);
    //字符设备驱动
    major = register_chrdev(0,"mychrdev",&fops);
    if(major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n",major);

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

    //向上提交设备节点信息 device_create
    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)
{
    //1.销毁设备节点信息
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //2.销毁目录
    class_destroy(cls);
    //3.注销字符设备驱动
    unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

Makefile

modname ?= demo
arch ?= arm
ifeq ($(arch),arm)  #通过命令行传过来的架构决定怎么编译
#KERBELDIR保存开发板内核源码路径
KERNELDIR := /home/ubuntu/FSMP1A/linux-stm32mp-5.10.61-stm32mp-r2-r0/linux-5.10.61
else
#保存UBUNTU内核源码路径
KERNELDIR := /lib/modules/$(shell uname -r)/build
endif

#PWD保存当前内核模块的路径
PWD := $(shell pwd)
all:
#make modules是模块化编译命令
#make -C $(KERNLEDIR) 执行make之前先切换到KERNELDIR对应的路径
#M=$(PWD)表示进行模块化编译的路径是PWD保存的路径
	make -C $(KERNELDIR) M=$(PWD) modules
clean:
#编译清除
	make -C $(KERNELDIR) M=$(PWD) clean
#将obj-m保存的文件单独链接为内核模块
obj-m :=  $(modname).o

效果演示

2 IO多路复用

驱动程序

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/poll.h>

int major;
char kbuf[128] = {0};

unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//定义等待队列
wait_queue_head_t wq_head;
unsigned int condition=0;
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)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    if(file->f_flags&O_NONBLOCK)//用户程序以非阻塞读写数据
    {}
    else{
        // wait_event_interruptible(wq_head,condition);//判断condition的值,为假则进程休眠
    }
    //将获取到的硬件数据拷贝到用户
    int ret = copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    condition = 0; //表示下一次数据没有准备就绪
    return 0;
}

__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;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret = copy_from_user(kbuf,ubuf,size);
    if (ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    condition = 1;//表示硬件数据就绪
    wake_up_interruptible(&wq_head);//唤醒休眠的进程
    return 0;
}
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,
    .write = mycdev_write,
    .poll = mycdev_poll,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    //初始化等待队列
    init_waitqueue_head(&wq_head);
    //字符设备驱动
    major = register_chrdev(0,"mychrdev",&fops);
    if(major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n",major);

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

    //向上提交设备节点信息 device_create
    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)
{
    //1.销毁设备节点信息
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //2.销毁目录
    class_destroy(cls);
    //3.注销字符设备驱动
    unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

进程1:监听多个事件

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd1,fd2;
    fd1 = open("/dev/input/mouse0",O_RDWR);
    if(fd1 < 0)
    {
        printf("打开鼠标设备失败\n");
        return -1;
    }
    fd2 = open("/dev/myled0",O_RDWR);
    if(fd2 < 0)
    {
        printf("打开设备失败\n");
        return -1;
    }
    // 定义读事件集合
    fd_set readfds;
    while(1)
    {
        // 清空集合
        FD_ZERO(&readfds);
        // 将要监听的事件添加进集合
        FD_SET(fd1, &readfds);
        FD_SET(fd2, &readfds); 
        // 阻塞监听事件
        int ret;
        ret = select(fd2 + 1, &readfds, NULL, NULL, NULL);
        if(ret < 0)
        {
            printf("select 调用失败\n");
            return -1;
        }
        // 判断事件是否发生
        if(FD_ISSET(fd1, &readfds));
        {
            memset(buf, 0, sizeof(buf));
            read(fd1, buf, sizeof(buf));
            printf("鼠标事件发生buf:%s\n", buf);
        }
        if(FD_ISSET(fd2, &readfds))
        {
            memset(buf,0,sizeof(buf));
            read(fd2,buf,sizeof(buf));
            printf("事件发生buf:%s\n",buf);
        }
    }
    // 关闭文件描述符
    close(fd1);
    close(fd2);
    return 0;
}

进程2:模拟自定义事件就绪

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

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

效果演示

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1129037.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

真空室的内表面加工

真空室和部件的内表面是在高真空和超高真空下实现工作压力的重要因素。必须在该条件下进行加工&#xff0c;以最小化有效表面&#xff0c;并产生具有最小解吸率的表面。 真空室和部件的表面往往是在焊接和机械加工后经过精细玻璃珠喷砂的。具有限定直径的高压玻璃珠被吹到表面…

Python创建条形图加点重叠

目录 代码效果图 要使用Python的Seaborn库创建一个条形图加点重叠的统计图&#xff0c;可以使用 seaborn.barplot和 seaborn.stripplot函数。以下是一个论文级别的简单示例代码&#xff0c;演示如何创建这种效果的图 代码 import seaborn as sns import matplotlib.pyplot a…

05 MIT线性代数-转置,置换,向量空间Transposes, permutations, spaces

1. Permutations P: execute row exchanges becomes PA LU for any invertible A Permutations P identity matrix with reordered rows mn (n-1) ... (3) (2) (1) counts recordings, counts all nxn permuations 对于nxn矩阵存在着n!个置换矩阵 , 2. Transpose: 2.…

如何将 huggingface上的模型文件下载到本地

写在前面 缘由&#xff1a;国内的GPU服务器直接调取 huggingface 上模型经常会失败&#xff0c;因此下载到本地就能免去许多麻烦。 方法三基于知乎上一位博主所提出方法的基础上进行改进&#xff0c;可以将huggingface上模型由 Colab 存进 谷歌云盘 或者 百度云盘。特别是有些…

Appium+Python+pytest自动化测试框架的实战

本文主要介绍了AppiumPythonpytest自动化测试框架的实战&#xff0c;文中通过示例代码介绍的非常详细&#xff0c;具有一定的参考价值&#xff0c;感兴趣的小伙伴们可以参考一下 先简单介绍一下目录&#xff0c;再贴一些代码&#xff0c;代码里有注释 Basic目录下写的是一些公…

【文章学习系列之模型】Koopa

本章内容 文章概况模型结构主要结构实验结果消融实验模型效率分解效果定性分解效果定量算子稳定性 总结 文章概况 《Koopa: Learning Non-stationary Time Series Dynamics with Koopman Predictors》是2023年发表于NeurIPS的一篇论文。考虑到时序预测中训练和推理数据之间甚至…

网工内推 | 网络工程师,大专以上、HCIA认证即可,最高14薪

01 湖南口味王集团 招聘岗位&#xff1a;网络工程师 职责描述&#xff1a; 1、负责园区内电脑日常维护&#xff1b; 2、负责园区内办公周边设备的日常维护&#xff0c;如打印机、投影仪等&#xff1b; 3、负责园区内电话日常维护&#xff1b; 4、负责园区内信息资产管理&#…

vcomp100.dll丢失的解决方法,一键修复vcomp100.dll丢失问题

在计算机使用过程中&#xff0c;我们经常会遇到一些错误提示&#xff0c;其中之一就是“vcomp100.dll丢失”。这个错误通常会导致某些程序无法正常运行&#xff0c;给用户带来困扰。为了解决这个问题&#xff0c;我整理了以下五个解决方法&#xff0c;希望能对大家有所帮助。 一…

Python-----for循环基本语法及其应用---对序列进行遍历循环

for循环基本语法 for循环结构主要用于&#xff08;序列 &#xff1a;包括 字符串、列表、元组、集合以及字典&#xff09;类型数据的遍历&#xff08;循环&#xff09;操作。 遍历(Traversal)&#xff0c;是指沿着某条搜索路线&#xff0c;依次对树&#xff08;或图&#…

《深入理解java虚拟机 第三版》学习笔记三

第 8 章 虚拟机字节码执行引擎 代码编译的结果从本地机器码转变为字节码&#xff0c;是存储格式发展的一小步&#xff0c;却是编程语言发展的一大步。 8.1 概述 执行引擎是 Java 虚拟机核心的组成部分之一。“虚拟机”是一个相对于“物理机”的概念&#xff0c;这两种机器都…

Python3 + Appium + 安卓模拟器实现APP自动化测试并生成测试报告

这篇文章主要介绍了Python3 Appium 安卓模拟器实现APP自动化测试并生成测试报告,本文给大家介绍的非常详细&#xff0c;对大家的学习或工作具有一定的参考借鉴价值&#xff0c;需要的朋友可以参考下 正文 一、安装Python3 直接登录Python官网https://www.python.org/&…

C++数据结构X篇_21_插入排序(稳定的排序)

文章目录 1. 插入排序原理2. 算法图解3. 核心代码&#xff1a;4. 插入排序整体代码实现 1. 插入排序原理 插入排序是一种最简单直观的排序算法&#xff0c;它的工作原理是通过构建有序序列&#xff0c;对于未排序数据&#xff0c;在已排序序列中从后向前扫描&#xff0c;找到相…

Unity 自定义小地图

最近工作做了个小地图&#xff0c;再此记录下思路。 1、准备所需素材 显示为地图&#xff08;我们取顶视图&#xff09;。创建一个Cube&#xff0c;缩放到可以把实际地图包住。实际地图的尺寸和偏移量 。我这里长宽都是25&#xff0c;偏移量&#xff08;1&#xff0c;0&…

MySQL进阶(数据库引擎)——MyISAM和InnoDB引擎的区别

1.是否支持行级锁 MyISAM 只有表级锁&#xff0c;而InnoDB 支持行级锁和表级锁&#xff0c;默认为行级锁。 &#xff08;1&#xff09;MySQL大致可以归纳为以下3种锁&#xff1a; 表级锁&#xff1a;开销小&#xff0c;加锁快&#xff1b;不会出现死锁&#xff1b;锁的粒度大…

C# FileInfo类的使用方法及常用操作(附代码示例)

在C#编程中&#xff0c;处理文件操作是一项常见而重要的任务。为了更好地管理和操作文件&#xff0c;C#提供了一个强大且灵活的FileInfo类。本文将深入探讨C# FileInfo类的使用方法&#xff0c;并为您提供一些实用的代码示例。 目录 一、什么是FileInfo类&#xff1f;二、使用F…

CLion使用SSH远程连接Linux服务器

最近要一直用实验室的服务器写Linux下的C代码, 本来一直用VScode(SSH)连接服务器, 但是我以前还是用JetBrains的IDE用的多, 毕竟他家的IDE代码提示和功能在某些细节上更加丰富。所以这次我使用了Clion里的远程连接(同样也是SSH工具)连接上了我的服务器, 实现了和VScode上同样的…

NOIP2023模拟1联测22 黑暗料理

NOIP2023模拟1联测22 黑暗料理 题目大意 自己看 思路 两个数相加能够产生质数的情况就是&#xff1a;11 或者 偶数质数 那么 1 1 1 不能保留超过一个 建一个图&#xff0c;原点连向所有奇数点&#xff0c;所有偶数点连向汇点&#xff0c;奇数点和偶数点的和为奇数的就相连 …

分布式事务 学习

分布式事务 关系型数据库事务&#xff08;本地事务&#xff09; 原子性&#xff1a;构成事务的所有操作&#xff0c;要么都执行完成&#xff0c;要么都不执行/一致性&#xff1a;在事务执行前后&#xff0c;数据库的一致性约束没有被破坏。隔离性&#xff1a;并发的两个事务的…

『第一章』命运的齿轮开始转动:雨燕(Swift)诞生!

在本篇博文中,您将学到如下内容: 1. 破茧成“燕”2. 持续进化&#xff01;3. Swift 5.0&#xff1a;ABI 稳定性4. Swift 5.1&#xff1a;模块稳定性和库进化5. Swift 5.9 来了6. 登高望远&#xff1a;Swift 6.0总结 雨燕翻新幕&#xff0c;风鹃绕旧枝 金鹊徒为滞&#xff0c;雨…

程序员的新去处?国内新能源公司大汇总!

近几年来&#xff0c;传统互联网企业哀鸿遍野&#xff0c;而新能源车企却在悄然崛起&#xff1a;HC逐年增加&#xff0c;薪资逐渐起飞&#xff0c;年终分红也让人眼红…… 聪明的程序员们已经把目光瞄准了新时代新能源车企&#xff0c;今天就带大家横向对比一下国内比较火热的…