news 2026/4/23 15:53:12

非阻塞 IO 实验

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
非阻塞 IO 实验
非阻塞 IO 简介
在非阻塞IO模型中,内核数据还未就绪时,应用进程也可以做其他事情,应用进程需要
不断询问内核数据是否就绪,并不会被直接挂起。
如果应用程序要采用非阻塞的方式来访问驱动设备文件,在使用open()函数打开
/dev/xxx_dev”设备文件的时候需要添加参数“O_NONBLOCK”,O_NONBLOCK表示以非阻
塞方式打开设备,这样从设备中读取数据的时候就是非阻塞方式了。
首先来编写应用测试代码read.c,在此代码中使用非阻塞的方式打开设备
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { int fd; char buf1[32] = {0}; char buf2[32] = {0}; fd = open("/dev/test", O_RDWR | O_NONBLOCK); // 打开 led 驱动 if (fd < 0) { perror("open error \n"); return fd; } printf("read before \n"); while (1) { read(fd, buf1, sizeof(buf1)); // 从/dev/test 文件读取数据 printf("buf is %s \n", buf1); // 打印读取的数据 sleep(1); } printf("read after \n"); close(fd); // 关闭文件 return 0; }
接着编写应用程序write.c
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int fd; char buf1[32] = {0}; char buf2[32] = "nihao"; fd = open("/dev/test", O_RDWR | O_NONBLOCK); // 打开 led 驱动 if (fd < 0) { perror("open error \n"); return fd; } printf("write before \n"); write(fd, buf2, sizeof(buf2)); // 向/dev/test 文件写入数据 printf("write after\n"); close(fd); // 关闭文件 return 0; }

nwq.c

#include <linux/cdev.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/io.h> #include <linux/kdev_t.h> #include <linux/module.h> #include <linux/uaccess.h> #include <linux/wait.h> struct device_test { dev_t dev_num; // 设备号 int major; // 主设备号 int minor; // 次设备号 struct cdev cdev_test; // cdev struct class *class; // 类 struct device *device; // 设备 char kbuf[32]; int flag; // 标志位 }; struct device_test dev1; DECLARE_WAIT_QUEUE_HEAD(read_wq); // 声明等待队列头 /*打开设备函数*/ static int cdev_test_open(struct inode *inode, struct file *file) { file->private_data = &dev1; // 设置私有数据 printk("This is cdev_test_open\r\n"); return 0; } /*向设备写入数据函数*/ static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off) { struct device_test *test_dev = (struct device_test *)file->private_data; if (copy_from_user(test_dev->kbuf, buf, size) != 0) { printk("copy_from_user error\r\n"); return -1; } test_dev->flag = 1; // 将条件置 1 wake_up_interruptible(&read_wq); // 唤醒等待队列中的休眠进程 return size; } /*从设备读取数据*/ static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off) { struct device_test *test_dev = (struct device_test *)file->private_data; // 可中断的阻塞等待,使进程进入休眠态 wait_event_interruptible(read_wq, test_dev->flag); if (copy_to_user(buf, test_dev->kbuf, size) != 0) { printk("copy_to_user error\r\n"); return -1; } test_dev->flag = 0; // 重置标志位 return size; } static int cdev_test_release(struct inode *inode, struct file *file) { return 0; } /*设备操作函数*/ struct file_operations cdev_test_fops = { .owner = THIS_MODULE, .open = cdev_test_open, .read = cdev_test_read, .write = cdev_test_write, .release = cdev_test_release, }; static int __init chr_fops_init(void) // 驱动入口函数 { int ret; /*1 创建设备号*/ ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); if (ret < 0) { goto err_chrdev; } printk("alloc_chrdev_region is ok\n"); dev1.major = MAJOR(dev1.dev_num); // 获取主设备号 dev1.minor = MINOR(dev1.dev_num); // 获取次设备号 printk("major is %d \r\n", dev1.major); printk("minor is %d \r\n", dev1.minor); /*2 初始化 cdev*/ dev1.cdev_test.owner = THIS_MODULE; cdev_init(&dev1.cdev_test, &cdev_test_fops); /*3 添加一个 cdev,完成字符设备注册到内核*/ ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1); if (ret < 0) { goto err_chr_add; } /*4 创建类*/ dev1.class = class_create(THIS_MODULE, "test"); if (IS_ERR(dev1.class)) { ret = PTR_ERR(dev1.class); goto err_class_create; } /*5 创建设备*/ dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test"); if (IS_ERR(dev1.device)) { ret = PTR_ERR(dev1.device); goto err_device_create; } return 0; err_device_create: class_destroy(dev1.class); err_class_create: cdev_del(&dev1.cdev_test); err_chr_add: unregister_chrdev_region(dev1.dev_num, 1); err_chrdev: return ret; } static void __exit chr_fops_exit(void) // 驱动出口函数 { device_destroy(dev1.class, dev1.dev_num); class_destroy(dev1.class); cdev_del(&dev1.cdev_test); unregister_chrdev_region(dev1.dev_num, 1); } module_init(chr_fops_init); module_exit(chr_fops_exit); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("topeet");

Makefile

export ARCH=arm64#设置平台架构 export CROSS_COMPILE=/home/alientek/rk3568_linux5.10_sdk/buildroot/output/rockchip_atk_dlrk3568/host/bin/aarch64-buildroot-linux-gnu-#交叉编译器前缀 obj-m += nwq.o #此处要和你的驱动源文件同名 KDIR :=/home/alientek/rk3568_linux5.10_sdk/kernel #这里是你的内核目录 PWD ?= $(shell pwd) all: make -C $(KDIR) M=$(PWD) modules #make 操作 clean: make -C $(KDIR) M=$(PWD) clean

输入./read命令运行read可执行文件

应用程序进程非阻塞,读取不到
数据便返回,然后一直轮询查看是否有数据

在使用可执行程序write向缓冲区写入数据时,read可执行程序读取到了缓冲区的数据并
打印
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 12:45:01

干货收藏!Agent三重觉醒:Tool/Plan/Memory如何让LLM拥有灵魂

文章探讨了Agent三大核心能力如何赋予LLM真正的智能&#xff1a;Tool能力使LLM能连接外部世界并执行操作&#xff1b;Plan/Reason能力让LLM进行深度思考和复杂推理&#xff1b;Memory能力使LLM能记住过去、积累经验并持续学习。这三种能力结合&#xff0c;使LLM从封闭的"聊…

作者头像 李华
网站建设 2026/4/23 11:32:37

导师严选2026 10款一键生成论文工具测评:本科生毕业论文必备神器

导师严选2026 10款一键生成论文工具测评&#xff1a;本科生毕业论文必备神器 学术写作工具测评&#xff1a;为何需要一份2026年权威榜单 随着人工智能技术的不断发展&#xff0c;越来越多的本科生开始借助AI工具辅助论文写作。然而&#xff0c;面对市场上琳琅满目的“一键生成…

作者头像 李华
网站建设 2026/4/23 11:27:52

产教深度融合,共育数智英才——聚焦人工智能时代的教育变革,同元软控携手高校推动教育科技人才一体化发展

为深入贯彻落实《教育强国建设规划纲要&#xff08;2024-2035年&#xff09;》精神&#xff0c;服务国家“教育、科技、人才”一体化发展战略&#xff0c;1月15日-17日&#xff0c;“中国高等教育学会校际合作研究分会2026年学术年会”在郑州举行。 会议同期&#xff0c;由哈尔…

作者头像 李华
网站建设 2026/4/23 14:53:18

【vsomeip3 教程 】vSomeIP 回调体系:从“能跑”到“可控”的工程化指南

目录标题 1. 回调不是“附属品”,它是运行时契约 1.1 为什么 vSomeIP 天然需要回调 1.2 “接口层回调”到底有多少:一个事实澄清 2. 基本三件套:State / Availability / Message 的语义边界与工程写法 2.1 `register_state_handler`:你真正关心的是“运行时是否可协作” 2.…

作者头像 李华
网站建设 2026/4/23 11:27:09

千万不能忽视!运城这家品牌策划公司如何让小企业业绩翻倍增长?

千万不能忽视&#xff01;运城这家品牌策划公司如何让小企业业绩翻倍增长&#xff1f;在当今竞争激烈的市场环境中&#xff0c;小企业要想脱颖而出并实现业绩的快速增长&#xff0c;离不开有效的品牌策划。山西尚琪文化传媒有限公司作为运城地区一家专业的品牌策划公司&#xf…

作者头像 李华