lv14 IO模型:阻塞和非阻塞 7

2024-01-08 23:44:14

1 五种IO模型------读写外设数据的方式

阻塞: 不能操作就睡觉

非阻塞:不能操作就返回错误(通过轮询即才能实现阻塞的情况 )

多路复用:委托中介监控

信号驱动:让内核如果能操作时发信号,在信号处理函数中操作

异步IO:向内核注册操作请求,内核完成操作后发通知信号

2 阻塞与非阻塞

应用层:

open时由O_NONBLOCK指示read、write时是否阻塞

open以后可以由fcntl函数来改变是否阻塞:

flags = fcntl(fd,F_GETFL,0);  //获取当前设备中标志位
flags |= O_NONBLOCK;          //增加不阻塞标志位
fcntl(fd, F_SETFL, flags);    //设置当前标志位到设备中

驱动层:通过等待队列

wait_queue_head_t //等待队列头数据类型
?
init_waitqueue_head(wait_queue_head_t *pwq) //初始化等待队列头
 ? ?
wait_event_interruptible(wq,condition)
/*
功能:条件不成立则让任务进入浅度睡眠,直到条件成立醒来
 ? ?wq:等待队列头
 ? ?condition:C语言表达式
返回:正常唤醒返回0,信号唤醒返回非0(此时读写操作函数应返回-ERESTARTSYS)
*/
 ? ? ? ?
wait_event(wq,condition) //深度睡眠
?
wake_up_interruptible(wait_queue_head_t *pwq)
 ? ? ? ?
wake_up(wait_queue_head_t *pwq)
 ? ?
 ? ?
/*
1. 读、写用不同的等待队列头rq、wq
2. 无数据可读、可写时调用wait_event_interruptible(rq、wq,条件)
3. 写入数据成功时唤醒rq,读出数据成功唤醒wq
*/
?

2.1 示例

mychar.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>

#include "mychar.h"

#define BUF_LEN 100

#define MYCHAR_DEV_CNT 3

int major = 11;
int minor = 0;
int mychar_num  = MYCHAR_DEV_CNT;

//新建结构体类型
struct mychar_dev
{
	struct cdev mydev;
	char mydef_buf[BUF_LEN];  //相当于结构体的私有变量
	int curlen;          //相当于结构体的私有变量
	wait_queue_head_t rq; //等待读队列
	wait_queue_head_t wq; //等待写队列

};

struct mychar_dev gmydev;

int mychar_open(struct inode *pnode, struct file *pfile)
{
	//利用private_data私有变量来指向全局变量结构体地址
	pfile->private_data = (void*)(container_of(pnode->i_cdev,struct mychar_dev,mydev));
    printk("mychar_open is called\n");
    return 0;
}

int mychar_close(struct inode *pnode, struct file *pfile)
{
    printk("mychar_close is called\n");
    return 0;
}


ssize_t mychar_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos)
{
	int ret = 0;
	int size = 0;
	//获取全家变量结构体地址
	struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;

	if(pmydev->curlen <= 0)
	{
		if(filp->f_flags & O_NONBLOCK)
		{//非阻塞
			printk("O_NONBLOCK No Data Read\n");
			return -1;
		}
		else
		{//阻塞
			ret = wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
			if(ret)
			{
				printk("Wake up by signal\n");
				return -ERESTARTSYS;
			}
		}
	}

 	if(count > pmydev->curlen)
	{
		size = pmydev->curlen;
	}
	else
	{
		size = count;
	}

	//将内核空间中的数据复制到用户空间
	ret = copy_to_user(pbuf,pmydev->mydef_buf,size);
	if(ret)
	{
	
		printk("copy_to_user failed\n");
		return -1;
	}
	//读完之后把后面的内容再拷贝过来,同时更新curlen
	memcpy(pmydev->mydef_buf,pmydev->mydef_buf+size,pmydev->curlen - size);
	pmydev->curlen = pmydev->curlen - size;

	wake_up_interruptible(&pmydev->wq);

	return size;

}

ssize_t mychar_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos)
{

	int size = 0;
	int ret  = 0;
	//获取全家变量结构体地址
	struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;

	if(pmydev->curlen >= BUF_LEN)
	{
		if(filp->f_flags & O_NONBLOCK)
		{
			printk("O_NONBLOCK Can not write data\n");
			return -1;
		}
		else
		{
			ret = wait_event_interruptible(pmydev->wq,pmydev->curlen < BUF_LEN);
			if(ret)
			{
				printk("wake up by signal\n");
				return -ERESTARTSYS;
			}
		}
	}

	if(count > BUF_LEN - pmydev->curlen)
	{
		size = BUF_LEN - pmydev->curlen;
	}
	else
	{
		size = count;
	}

	//将用户空间中的数据复制到内核空间中
	ret = copy_from_user(pmydev->mydef_buf + pmydev->curlen, pbuf, size);
	if(ret)
	{
		printk("copy_from_user failed\n");
		return -1;
	}
    //更新curlen
	pmydev->curlen = pmydev->curlen + size;

	wake_up_interruptible(&pmydev->rq);

	return size;
}

long mychar_ioctl(struct file *filp, unsigned int cmd,unsigned long arg)
{
    int __user *pret = (int *)arg;
	int maxlen = BUF_LEN;
	int ret = 0;
	struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;


	switch(cmd)
	{
		case MYCHAR_IOCTL_GET_MAXLEN:
			ret = copy_to_user(pret,&maxlen,sizeof(int));
			if(ret)
			{
				printk("copy_to_user MAXLEN failed\n");
				return -1;
			}
			break;
		case MYCHAR_IOCTL_GET_CURLEN:
			ret = copy_to_user(pret,&pmydev->curlen,sizeof(int));
			if(ret)
			{
				printk("copy_to_user CURLEN failed\n");
				return -1;
			}
			break;
		default:
			printk("The cmd is unknow\n");
			return -1;
	}
	return 0;
}

//结构体初始化:部分变量赋值初始化
struct file_operations myops = {
    .owner = THIS_MODULE,
    .open = mychar_open,
    .release = mychar_close,
	.read = mychar_read,
	.write = mychar_write,
	.unlocked_ioctl = mychar_ioctl
};

int mychar_init(void)
{
    int ret = 0;
    dev_t devno = MKDEV(major, minor);

    /* 申请设备号 */
    ret = register_chrdev_region(devno, mychar_num, "mychar");
    if (ret) {
        ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
        if (ret) {
            printk("get devno failed\n");
            return -1;
        }
		major = MAJOR(devno); // 容易遗漏,注意
    }

    /* 给struct cdev对象指定操作函数集 */
    cdev_init(&gmydev.mydev, &myops);

    /* 将 struct cdev对象添加到内核对应的数据结构里 */
    gmydev.mydev.owner = THIS_MODULE;
    cdev_add(&gmydev.mydev, devno, 1);

	//初始化队列
	init_waitqueue_head(&(gmydev.rq));
	init_waitqueue_head(&(gmydev.wq));

    return 0;
}

void __exit mychar_exit(void)
{
    dev_t devno = MKDEV(major, minor);

    cdev_del(&gmydev.mydev);

    unregister_chrdev_region(devno, mychar_num);
}

//表示支持GPL的开源协议
MODULE_LICENSE("GPL");

module_init(mychar_init);
module_exit(mychar_exit);

mychar.h

#ifndef MY_CHAR_H
#define MY_CHAR_H

#include <asm/ioctl.h>

#define MY_CHAR_MAGIC 'k'

#define MYCHAR_IOCTL_GET_MAXLEN _IOR(MY_CHAR_MAGIC,1,int*)
#define MYCHAR_IOCTL_GET_CURLEN _IOR(MY_CHAR_MAGIC,2,int*)


#endif

?testmychar_nonblockread.c

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

#include "mychar.h"
#include <stdio.h>


int main(int argc,char *argv[])
{
	int fd = -1;
	char buf[8] = "";

	int ret = 0;

	if(argc < 2)
	{
		printf("The argument is too few\n");
		return 1;
	}

	fd = open(argv[1],O_RDWR);   // | O_NONBLOCK);
	if(fd < 0)
	{
		printf("open %s failed\n",argv[1]);
		return 2;
	}


	ret = read(fd,buf,8);
	if(ret < 0)
	{
		printf("read data failed\n");
	}
	else
	{
		printf("buf=%s\n",buf);
	}

	close(fd);
	fd = -1;
	return 0;
}

Makfile

ifeq ($(KERNELRELEASE),)

ifeq ($(ARCH),arm)
KERNELDIR ?= /home/linux/Linux_4412/kernel/linux-3.14
ROOTFS ?= /opt/4412/rootfs
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)


modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install

clean:
	rm -rf  *.o  *.ko  .*.cmd  *.mod.*  modules.order  Module.symvers   .tmp_versions

else
CONFIG_MODULE_SIG=n
obj-m += mychar.o


endif

?

?移除并新增内核模块

添加设备?

非阻塞读执行效果:

阻塞读效果

文章来源:https://blog.csdn.net/m0_60718520/article/details/135466461
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。