驱动学习(九)字符设备的非阻塞操作

驱动学习(九)字符设备的非阻塞操作

1. 什么是非阻塞操作?

是指在不能进行设备操作时,并不挂起或休眠该进程,而是给请求进程一个非正确的返回值,底层设备驱动通过不停的查询操作是否可进行,直到操作可进行后给请求进程返回一个正确的结果。

(进程在不能进行设备的操作时,并不休眠,而是立即返回)

2. 操作
应用层:
	fd=open("/dev/设备文件名",O_RDWR|O_NONBLOCK);非阻塞方式	   	   read(fd,buf,count);
驱动层:
	1 需要应用层传入一个O_NONBLOCK标志(在打开文件时传)
	2 在驱动读文件中去判断有没有非阻塞标志?
		若有,驱动代码返回;
		若没有,阻塞进程
	if(若设备中没有数据可读)
    {
		if(若应用层传入O_NONBLOCK标志)
		{
			返回一个非正确的值;
		}
		else
		{
			阻塞请求读数据的进程(使请求读的进程休眠)
		}
     }

	注意:CharCount表示实际写入到内核中的字符个数
3. 测试

代码

###### nowait.c

#include <linux/module.h>           //模块驱动的头文件
#include <linux/cdev.h>             //设备信息描述的头文件
#include <linux/fs.h>               //静态申请设备号头文件
#include <linux/kdev_t.h>           //设备号用到的头文件和宏函数
#include <linux/uaccess.h>
#include <linux/device.h>
#include "common.h"

#define BUF_SIZE 100

int major = 0;                      //主设备号
int min = 0;                        //次设备号
int deviceNum = 0;                  //完整设备号
struct cdev* pCdev = NULL;          //描述设备信息的结构体
char *deviceName = "nowait";//设备名
char buff[BUF_SIZE] = "chrdev-test-2022-7-13";
struct class *pClass = NULL;        //设备文件类指针
int devNum = 2;		    	    //设备文件数量
wait_queue_head_t q;                //定义休眠等待队列头
int con = 0;                        //休眠唤醒条件 1唤醒 0休眠
int charCount = 0;                  //实际写入字符数量
int testOpen(struct inode *pNode,struct file *pFile)
{
	printk("------into test open------\n");
	con = 0;
	printk("------leave test open------\n");
	return 0;
}
int testClose(struct inode *pNode,struct file *pFile)
{
	printk("------into test close------\n");
	printk("------leave test close------\n");
	return 0;
}
ssize_t testRead(struct file *pFile,char __user *buf,size_t count,loff_t *pOffset)
{
	int res = -1;
	printk("------into testRead------\n");
	if(charCount <= 0)
	{
		if(pFile->f_flags&O_NONBLOCK)
		{
			printk("O_NONBLOCK is setted\n");
			return -EAGAIN;
		}
		else
		{
			wait_event_interruptible(q,con);
		}
	}
	if(count > BUF_SIZE-1)
	{
		count = BUF_SIZE - 1;
	}
	if(count > charCount)
	{
		count = charCount;
	}
	res = copy_to_user(buf,buff,count);
	if(res)
	{
		printk("copy_to_user error\n");
		return -EFAULT;
	}
	charCount -= count;
	printk("copy_to_user ok\n");
	printk("\t buff = %s\t\n",buff);
	printk("------leave testRead------\n");
	return count;
}

ssize_t testWrite(struct file *pFile,const char __user *buf,size_t count,loff_t *pOffset)
{
	int res = -1;
	printk("------into testWrite------\n");
	con = 1;
	wake_up_interruptible(&q);
	if(count > BUF_SIZE-1)
	{
		count = BUF_SIZE - 1;
	}
	res = copy_from_user(buff,buf,count);
	if(res)
	{
		printk("copy_from_user error\n");
		return -EFAULT;
	}
	charCount = count;
	printk("copy_from_user ok\n");
	printk("\t buff = %s\t\n",buff);
	printk("------leave testWrite------\n");
	return count;
}

long testIoctl(struct file *pFile,unsigned int cmd,unsigned long arg)
{
	switch(cmd)
	{
		case TEST_CMD:
			{
				printk("test cmd-------arg = %ld \n",arg);
			}
			break;
		case TEST_CMD1:
			{
				printk("test cmd1-------arg = %ld \n",arg);
			}
			break;
		case TEST_CMD2:
			{
				printk("test cmd2-------arg = %ld \n",arg);
			}
			break;
		default:
			printk("error cmd \n");
	}
	return 0;
}

struct file_operations fp_arr =
{
	.owner = THIS_MODULE,
	.open = testOpen,
	.read = testRead,
	.write = testWrite,
	.release = testClose,
	.unlocked_ioctl = testIoctl
};

int driverr_init(void)               //模块初始化函数
{
	int res = 0;
	int i = 0;
	struct device *pDevTmp = NULL;
	printk("*********into driver init\n");
	//动态申请设备号
	res = alloc_chrdev_region(&deviceNum,min,devNum,deviceName);
	if(res)
	{
		printk("alloc_chrdev_region error\n");
		return res;
	}
	printk("alloc_chrdev_region OK!\n");
	printk("major = %d minor = %d \n",MAJOR(deviceNum),MINOR(deviceNum));
	major = MAJOR(deviceNum);
	//创建设备
	pCdev = cdev_alloc();
	if(NULL == pCdev)
	{
		printk("cdev_alloc error\n");
		unregister_chrdev_region(deviceNum,devNum);
		return -1;
	}
	printk("cdev_alloc ok\n");
	//设备初始化
	cdev_init(pCdev,&fp_arr);
	printk("cdev_init ok\n");
	//设备与设备号关联
	res = cdev_add(pCdev,deviceNum,devNum);
	if(res)
	{
		printk("cdev_add error\n");
		cdev_del(pCdev);
	}
	printk("cdev_add ok\n");
	//创建设备文件类
	pClass = class_create(THIS_MODULE,"nowait");
	if(NULL == pClass)
	{
		printk("class_create error\n");
		cdev_del(pCdev);
	}
	printk("class_create ok\n");
	//创建设备文件
	for(;i < devNum;i++)
	{
		pDevTmp = device_create(pClass,NULL,MKDEV(major,i),NULL,"nowait%d",i);
		if(IS_ERR(pDevTmp))
		{
			printk("device_create error\n");
			for(i = 0;i < devNum;i++)
			{
				device_destroy(pClass,MKDEV(major,i));
			}
			class_destroy(pClass);
			return -2;
		}
	}
	printk("device_create ok\n");
	//初始化休眠等待队列
	init_waitqueue_head(&q);
	printk("init_waitqueue_head ok\n");
	printk("*********leave driver init\n");
	return 0;
}

void driver_clear(void)             //模块清除函数
{
	int i = 0;
	printk("*********into driver clear\n");
	for(;i < devNum; i++)
	{
		device_destroy(pClass,MKDEV(major,i));
	}
	class_destroy(pClass);
	cdev_del(pCdev);
	unregister_chrdev_region(deviceNum,devNum);
	printk("*********leave driver clear\n");
}

module_init(driverr_init);           //模块加载函数
module_exit(driver_clear);          //模块卸载函数


MODULE_LICENSE("GPL");
MODULE_AUTHOR("cfy");
MODULE_ALIAS("liangzai");
MODULE_DESCRIPTION("2022-7-13");
###### test.c

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

#define BUF_SIZE 100

int main()
{
	int fd = open("/dev/nowait0",O_RDWR|O_NONBLOCK);
	if(fd < 0)
	{
		printf("open nowait0 err\n");
		return -1;
	}
	printf("open nowait0 ok\n");

	char buf[BUF_SIZE] = {0};
	//测试读
	read(fd,buf,BUF_SIZE-1);
	printf("read data:%s\n",buf);
	bzero(buf,sizeof(buf));

	close(fd);
	return 0;

}
###### test1.c

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

#define BUF_SIZE 100

int main()
{
	int fd = open("/dev/nowait1",O_RDWR|O_NONBLOCK);
	if(fd < 0)
	{
		printf("open nowait1 err\n");
		return -1;
	}
	printf("open nowait1 ok\n");

	char buf[BUF_SIZE] = {0};

	//测试写
	printf(">> input:");
	bzero(buf,sizeof(buf));
	scanf("%s",buf);
	write(fd,buf,sizeof(buf));
	
	close(fd);
	return 0;

}

现象

编译安装驱动
在这里插入图片描述

test在没有读到数据的时候返回了,此时test1还未写入
在这里插入图片描述

此时让test1写入,再次执行test,test读到test1写入的数据返回了

在这里插入图片描述

结论

在字符设备驱动框架基础上对字符设备读操作的非阻塞测试没毛病!

下一节是在字符设备驱动框架基础上,实现对字符设备的poll操作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值