内核实验(八):实现O-NONBLOCK非阻塞读写

简介: 本文通过修改之前的内核模块代码,介绍了如何在Linux内核中实现O_NONBLOCK非阻塞读写机制,并通过在Qemu虚拟机上的测试验证了非阻塞读写操作的正确性。

一、篇头

继续使用qemu调试内核的实验。本章复习阻塞与非阻塞IO的概念和机制,然后对之前实验(七)的代码做少许修改,来实现非阻塞的IO读写。

二、系列文章

略……

三、实验环境

  • 编译服务器+NFS:ubuntu 22.04
  • Qemu 虚拟机:Linux version 5.15.102 + Buysbox 1.3.36 + ARM_32bit

Qemu 启动命令:qemu-system-arm -nographic -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -initrd …/busybox/rootfs.ext4.img.gz -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb

四、源码

4.1 非阻塞IO与阻塞IO

非阻塞:进程发起I/O系统调用后,如果设备驱动的缓冲区没有数据,那么进程返回一个错误而不会被阻塞。如果驱动缓冲区中有数据,那么设备驱动把数据直接返回给用户进程。

阻塞:进程发起I/O系统调用后,如果设备的缓冲区没有数据,那么需要到硬件I/O中重新获取新数据,进程会被阻塞,也就是睡眠等待。直到数据准备好,进程才会被唤醒,并重新把数据返回给用户空间。

4.2 驱动源码

  • 文件名:linux-stable\my_kmodules\test_7.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/kfifo.h>


#define MY_DEV_NAME "my_dev"

DEFINE_KFIFO(my_kfifo, char, 128);

static int test_6_open(struct inode *inode, struct file *file)
{
   
   
    int major = MAJOR(inode->i_rdev);
    int minor = MINOR(inode->i_rdev);

    pr_info("%s: major=%d, minor=%d\n", __func__, major, minor);
    return 0;
}

static int test_6_release(struct inode *inode, struct file *file)
{
   
   
    pr_info("%s \n", __func__);

    return 0;
}

static ssize_t test_6_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
   
   
    int ret;
    unsigned int copied_count=0;
    pr_info("%s \n", __func__);


     /*
     * (1)add for O_NONBLOCK reading
     */
    if(kfifo_is_empty(&my_kfifo)){
   
   
        if(file->f_flags & O_NONBLOCK){
   
   
            return  -EAGAIN;
        }
    }

    ret = kfifo_to_user(&my_kfifo, buf,  count, &copied_count);
    if(ret != 0) {
   
   
        return -EFAULT;
    }
    //pr_info("%s :%s, count=%d, copied_count=%d\n", __func__, my_kfifo.buf,  count, copied_count);

    return copied_count;
}

static ssize_t test_6_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
{
   
   
    int ret;
    unsigned int copied_count=0;
    pr_info("%s \n", __func__);

     /*
     * (1)add for O_NONBLOCK writing
     */
    if(kfifo_is_full(&my_kfifo)){
   
   
        if(file->f_flags & O_NONBLOCK){
   
   
            return  -EAGAIN;
        }
    }

    ret = kfifo_from_user(&my_kfifo, buf, count, &copied_count);
    if(ret != 0) {
   
   
        return -EFAULT;
    }

    //pr_info("%s , my_kfifo.buf=%s\n", __func__, my_kfifo.buf);    
    return copied_count;


}

static const struct file_operations test_fops = {
   
   
    .owner = THIS_MODULE,
    .open = test_6_open,
    .release = test_6_release,
    .read = test_6_read,
    .write = test_6_write
};

static struct miscdevice test_6_misc_device ={
   
   

    .minor = MISC_DYNAMIC_MINOR,
    .name = MY_DEV_NAME,
    .fops = &test_fops,
};


static int __init test_6_init(void)
{
   
   
    int ret;

    pr_info("test_6_init\n");

    ret = misc_register(&test_6_misc_device);
    if (ret != 0 ) {
   
   
        pr_err("failed to misc_register");
        return ret;
    }

    pr_err("Minor number = %d\n", test_6_misc_device.minor);

    return 0;
}

static void __exit test_6_exit(void)
{
   
   
    pr_info("test_6_exit\n");
    misc_deregister(&test_6_misc_device);
}

module_init(test_6_init);
module_exit(test_6_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("szhou <66176468@qq.com>");
MODULE_DESCRIPTION("test_7, 使用misc、KFIFO开发设备驱动非阻塞设备驱动。");

4.3 APP源码

  • 文件名:linux-stable\my_kmodules\app_test_7.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h> //消除 warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
#include <string.h> //消除 warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration]

#define MY_DEV_NAME "/dev/my_dev"

int main(int argc, void * argv[])
{
   
   

    int fd;
    int ret;
    size_t len;
    char message[80] = "To test reading and writing FIFO device with O_NONBLOCK.";
    char *read_buffer;

    len = sizeof(message);

    read_buffer = (char *)malloc((size_t)2*len);
    memset((void *)read_buffer, 0, 2*len);

    fd = open(MY_DEV_NAME, O_RDWR | O_NONBLOCK);
    if (fd < 0) {
   
   
        printf("open device %s failded\n", MY_DEV_NAME);
        return -1;
    }

    /*1: 先读一次,但这个时候KFIFO为空,所以会直接返回 */
    ret = read(fd, read_buffer, (size_t)2*len);
    printf("read %d bytes\n", ret);
    printf("read buffer=%s\n", read_buffer);

    /*2. 写一次数据 */
    ret = write(fd, message, len);
    if (ret != len)
        printf("have write %d bytes\n", ret);


    /*3. 再写一次数据 */
    ret = write(fd, message, len);
    if (ret != len)
        printf("have write %d bytes\n", ret);

    /*4. 读取数据,这次因为KFIFO非空,所以可以获得数据 */
    ret = read(fd, read_buffer, 2*len);
    printf("read %d bytes\n", ret);
    printf("read buffer=%s\n", read_buffer);

    close(fd);
}

4.4 Makefile

  • 文件名:linux-stable\my_kmodules\Makefile
  • 如下,继续追加 test_7.o 即可编译出test_7.ko
KDIR := /home/szhou/works/qemu_linux/linux-stable

obj-m := test_1.o test_2.o test_3.o test_4.o  test_5.o test_6.o test_7.o
all :
    $(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
    rm -f *.ko

五、编译及部署

1)执行驱动KO编译
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ make
make -C /home/szhou/works/qemu_linux/linux-stable M=/home/szhou/works/qemu_linux/linux-stable/my_kmodules modules
make[1]: Entering directory '/home/szhou/works/qemu_linux/linux-stable'
  CC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_7.o
  MODPOST /home/szhou/works/qemu_linux/linux-stable/my_kmodules/Module.symvers
  CC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_7.mod.o
  LD [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_7.ko
make[1]: Leaving directory '/home/szhou/works/qemu_linux/linux-stable'2)执行app编译
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ arm-linux-gnueabi-gcc app_test_7.c -o app_test_7 --static3)将KO和APP存放到NFS共享目录
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ cp test_7.ko app_test_7  ~/works/nfs_share/
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$

六、运行及测试

1)启动之前编译组建的QEMU虚拟机
----------------------------------------
Welcome to szhou's tiny Linux
----------------------------------------

Please press Enter to activate this console.2)挂载NFS共享目录
~ #  mount -t nfs -o nolock 192.168.3.67:/home/szhou/works/nfs_share /mnt

(3) 加载ko
~ # cd /mnt/
/mnt # insmod test_7.ko 
test_7: loading out-of-tree module taints kernel.
test_6_init
Minor number = 125
/mnt # mdev  -s

(4) 运行app测试
/mnt # ./app_test_7 
test_6_open: major=10, minor=125
test_6_read 
read -1 bytes
read buffer=
test_6_write 
test_6_write 
have write 48 bytes
test_6_read 
read 128 bytes
read buffer=To test reading and writing FIFO device with O_NONBLOCK.
test_6_release 
/mnt #

操作画面如下图:

在这里插入图片描述

相关文章
|
自然语言处理 安全 C++
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
8492 4
ubunut16.04 make install 提示 makeinfo is missing on your system;
ubunut16.04 make install 提示 makeinfo is missing on your system;
196 2
|
网络协议 Linux API
Linux网络编程:shutdown() 与 close() 函数详解:剖析 shutdown()、close() 函数的实现原理、参数说明和使用技巧
Linux网络编程:shutdown() 与 close() 函数详解:剖析 shutdown()、close() 函数的实现原理、参数说明和使用技巧
1260 0
|
安全 网络协议 物联网
物联网Wi-Fi配网方式,你知道几种?
什么是配网?有哪些配网方式?物联网配网技术为何鱼龙混杂,互不相通?本文将从原理、流程详细介绍一键配网、设备热点配网、手机热点配网、蓝牙配网、路由器配网和零配等6种配网方式,总结对比各配网方式的特点,并分享对配网技术未来发展方向的看法。
7585 0
物联网Wi-Fi配网方式,你知道几种?
|
Ubuntu Linux
内核实验(六):使用misc框架,实现简单字符设备驱动
本文介绍了如何使用Linux的misc框架来实现一个简单的字符设备驱动程序,包括编写驱动源码、编译、部署以及在Qemu虚拟机中测试驱动程序,展示了如何动态分配次设备号并手动创建设备文件以进行测试。
255 0
内核实验(六):使用misc框架,实现简单字符设备驱动
|
Ubuntu Linux 数据安全/隐私保护
内核实验(七):使用内核KFIFO环形缓冲区机制
本文通过一个内核模块实验,演示了如何在Linux内核中使用KFIFO环形缓冲区机制,包括定义KFIFO、编写驱动程序以及在Qemu虚拟机中进行编译、部署和测试,展示了KFIFO在无需额外加锁的情况下如何安全地在读者和写者线程间进行数据传输。
613 0
内核实验(七):使用内核KFIFO环形缓冲区机制
|
Ubuntu 网络协议 数据安全/隐私保护
【Ubuntu】sudo apt-get update 无法解析域名(亲测有效)
在Ubuntu 18.04系统中,用户在执行sudo apt-get update时遇到“无法解析域名‘ip’”的错误。经分析,问题源于之前设置的网络代理配置未完全清除。解决方案是找到并重命名/etc/apt/apt.conf.d下的proxy.conf文件,使其不再生效。操作后,sudo apt-get update命令恢复正常,问题得到完美解决。
3073 4
【Ubuntu】sudo apt-get update 无法解析域名(亲测有效)
|
12月前
|
编解码 监控 网络协议
如何用魔法般的步骤实现RTSP推送H.264与H.265(HEVC),打造震撼视听盛宴,让每一帧都充满魔力!
【9月更文挑战第3天】实现RTSP流媒体服务推送H.264和H.265编码视频是现代视频监控及直播平台的关键技术。本文详细介绍环境搭建、编码配置及服务器与客户端实现方法。首先,通过FFmpeg捕获视频并编码成RTSP流,接着使用VLC等工具接收播放。此外,还提供了C++示例代码,演示如何利用libv4l2和FFmpeg自定义服务器端实现。希望本文能帮助读者成功搭建RTSP视频流系统。
1674 1
|
NoSQL Linux 编译器
内核实验(一):使用QEMU+GDB断点调试Linux内核代码
如何配置环境并使用QEMU虚拟机结合GDB进行Linux内核代码的断点调试,包括安装QEMU、交叉编译工具链,编译内核以及通过GDB远程连接进行调试的详细步骤。
867 0
内核实验(一):使用QEMU+GDB断点调试Linux内核代码
|
安全 Linux 网络安全
【工具使用】几款优秀的SSH连接客户端软件工具推荐FinalShell、Xshell、MobaXterm、OpenSSH、PUTTY、Terminus、mRemoteNG、Terminals等
【工具使用】几款优秀的SSH连接客户端软件工具推荐FinalShell、Xshell、MobaXterm、OpenSSH、PUTTY、Terminus、mRemoteNG、Terminals等
112124 0