驱动学习(八)字符设备的阻塞操作
1. 什么是阻塞操作呢?
在执行设备操作时,若不能获得资源则挂起进程直到满足可操作的条件后再进行操作。
2. 驱动中如何实现读阻塞?
1 定义一个休眠等待队列
wait_queue_head_t q;//定义休眠等待对列头
2 在初始化函数中初始化休眠等待队列
init_waitqueue_head(&q);//初始化休眠等待对列
3 在驱动读函数中,使请求读的进程休眠(将读进程放休眠等待队列中)
wait_event_interruptible(q,con)//使当前读进程休眠--》休眠期可被中断
wait_event(q,con);//使当前读进程休眠--》休眠期不可被中断
q:休眠等待对列头
con:休眠唤醒条件。1:唤醒 0:休眠
4 在驱动写函数中,一旦有数据可读,唤醒休眠进程
con=1;
wake_up_interruptible(&q);//唤醒可被中断的休眠进程
wake_up(&q);//唤醒不可被中断的休眠进程
3.注意
当进程正常运行时,进程在运行队列中等待被运行
当进程休眠时,进程在休眠等待队列中等待被唤醒运行
4.测试
只是测试了可以被中断的读阻塞,不可被中断的未测
Makefile 可以参考上一节
环境:ubuntu 20.04
#####代码
#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 = "wait";//设备名
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 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");
wait_event_interruptible(q,con);
if(count > BUF_SIZE-1)
{
count = BUF_SIZE - 1;
}
res = copy_to_user(buf,buff,count);
if(res)
{
printk("copy_to_user error\n");
return -EFAULT;
}
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;
}
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,"wait");
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,"wait%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/wait0",O_RDWR);
if(fd < 0)
{
printf("open wait0 err\n");
return -1;
}
printf("open wait0 ok\n");
char buf[BUF_SIZE] = {0};
//测试读
read(fd,buf,BUF_SIZE-1);
printf("read data:%s\n",buf);
bzero(buf,sizeof(buf));
//测试写
printf(">> input:");
scanf("%s",buf);
write(fd,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/wait1",O_RDWR);
if(fd < 0)
{
printf("open wait1 err\n");
return -1;
}
printf("open wait1 ok\n");
char buf[BUF_SIZE] = {0};
//测试写
printf(">> input:");
bzero(buf,sizeof(buf));
scanf("%s",buf);
write(fd,buf,sizeof(buf));
//测试读
bzero(buf,sizeof(buf));
read(fd,buf,BUF_SIZE-1);
printf("read data:%s\n",buf);
bzero(buf,sizeof(buf));
close(fd);
return 0;
}
现象:
安装驱动并查看
test先读再写,test1先写再读,此时在test1未写入之前,test处于读堵塞状态
test1输入之后test的read被唤醒了
结论:
好使!老九他没毛病!
下一节是字符设备的非阻塞操作