news 2026/5/16 17:32:40

阻塞 IO 实验

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
阻塞 IO 实验
什么是等待队列
在 Linux驱动程序中,阻塞进程可以使用等待队列来实现。等待队列是实现阻塞和唤醒的
内核机制,等待队列以双循环链表为基础结构,其中链表头和链表项两部分别表示等待队列头
和等待队列元素,如图所示。
等待队列使用方法
步骤一:初始化等待队列头,并将条件置成假(condition=0)
步骤二:在需要阻塞的地方调用wait_event(),使进程进入休眠状态。
步骤三:当条件满足时,需要解除休眠,先将条件设置成真(condition=1),然后调用wake_up()
函数唤醒等待队列中的休眠进程。
实验代码wq.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");
编写应用程序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); // 打开 led 驱动 if (fd < 0) { perror("open error \n"); return fd; } printf("read before \n"); read(fd, buf1, sizeof(buf1)); // 从/dev/test 文件读取数据 printf("buf is %s \n", buf1); 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); // 打开 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; }

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 += wq.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应用程序进程阻塞

然后输入./write命令运行write可执行文件,如上图所示,使用write()函数向设备写入
数据,唤醒等待队列中的休眠进程。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/10 12:23:46

pkg-config 使用指南

概述pkg-config是一个用于检索系统中已安装库的元信息的工具&#xff0c;主要用于编译和链接库。它通过读取 .pc元数据文件来获取库的编译和链接信息。基本语法pkg-config [选项] [库名...]常用选项详解1. 信息查询选项# 查看库的版本 pkg-config --modversion gtk-3.0 # 输出:…

作者头像 李华
网站建设 2026/5/10 12:24:36

Systemd 使用指南

1. Systemd 基础概念 什么是 Systemd&#xff1f; Systemd 是 Linux 系统的现代初始化系统和服务管理器&#xff0c;取代了传统的 SysVinit。它提供&#xff1a; 更快的启动时间 更好的并行处理 高级服务管理功能 依赖关系管理 2. Systemd 核心组件 单元&#xff08;Un…

作者头像 李华
网站建设 2026/5/10 12:23:02

如何低成本、快速地建立私有内测系统?

最近团队复盘&#xff0c;我们发现了一个很有意思的现象&#xff1a;团队里最耗时、最没技术含量&#xff0c;但又最容易引发混乱的环节&#xff0c;竟然是——发-内-测-包。听起来有点可笑&#xff0c;但回想一下&#xff0c;你是不是也经历过这样的场景&#xff1a;微信群里&…

作者头像 李华
网站建设 2026/5/11 16:08:49

2024年提示工程热点:生命周期管理的4个关键进化方向

2024年提示工程热点:生命周期管理的4个关键进化方向 一、引入与连接 引人入胜的开场 想象一下,在科技的浩瀚宇宙中,有一颗闪耀的星星叫做提示工程。它就像是一位神奇的魔法师,能够让计算机理解我们人类的语言,并按照我们的要求完成各种任务。从智能客服与我们流畅对话,…

作者头像 李华
网站建设 2026/5/9 17:24:53

springboot基于Java的停车场管理系统设计实现

背景与需求分析 随着城市化进程加快&#xff0c;机动车保有量激增&#xff0c;传统停车场管理方式&#xff08;如人工记录、纸质收费&#xff09;效率低下&#xff0c;存在车位利用率低、缴费混乱、安全隐患等问题。基于SpringBoot的停车场管理系统通过信息化手段解决以下痛点…

作者头像 李华