linux设备驱动中的阻塞与非阻塞I/O
阻塞:等待某种资源,如果获取不到,就会把当前任务挂起,等待资源准备好
1.
int flag = 0;
while(flag == 0);
2.等待队列
wait_event(q,flag > 0);
wakeup(q);
非阻塞:等待某种资源非阻塞,循环搭配,反复的获取
异步通知:做自己的事,当资源准备好了,系统通知它去获取资源
阻塞等待
驱动程序
/*===============================================
* 文件名称:modSpin.c
* 创 建 者: memories
* 创建日期:2023年07月11日
* 描 述:have a nice day
================================================*/
//头文件
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/kdev_t.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define MA 300
#define MI 0
#define MAX_LEN 10
static dev_t no = 0;
static unsigned count = 1;
static const char * name = "mydev";
static struct cdev mydev;
static char buf[MAX_LEN] = {0};
static spinlock_t lock;
static struct mutex my_mutex;
static wait_queue_head_t my_queue;
static int w=0,r=0 ;
int myopen(struct inode *pi, struct file *pf)
{
if( mutex_trylock(&my_mutex)==0 )
return -1;
return 0;
}
int myrelease(struct inode *pi, struct file *pf)
{
mutex_unlock(&my_mutex);
return 0;
}
static int rlen = 0;
ssize_t myread (struct file *pf, char __user *rbuf, size_t len, loff_t *poff)
{
int ret = -1;
//计算读的空间
wait_event_interruptible(my_queue, rlen >= len);
if(r<w)
ret = copy_to_user(rbuf,buf+r,len);
else
{
if(len <= MAX_LEN-r)
ret = copy_to_user(rbuf,buf+r,len);
else
{
ret = copy_to_user(rbuf,buf+r,(MAX_LEN-r));
ret = copy_to_user(rbuf+(MAX_LEN-r),buf,len-(MAX_LEN-w));
}
}
if(ret != 0)
{
printk("copy read is failed\n");
return -1;
}
r = (r+len)%MAX_LEN;
if(r <= w)
rlen = w - r;
else
rlen = w + (MAX_LEN-r);
return len;
}
ssize_t mywrite (struct file *pf, const char __user *wbuf, size_t len, loff_t *poff)
{
int ret = -1;
int wlen = 0;
//计算可写空间大小
if(w >= r)
wlen = r+(MAX_LEN-w);
else
wlen = w - r;
//判断若数据长度大于可写空间大小,则无法输出
if(len > wlen)
return -1;
//如果数据长度不大于可写空间大小
if(MAX_LEN-w >= len)//如果剩余可写长度能容纳len长度的数据则一次性输入
ret = copy_from_user(buf+w,wbuf,len);
else//如果不能则分段输入
{
ret = copy_from_user(buf+w,wbuf,MAX_LEN-w);
ret = copy_from_user(buf,wbuf+(MAX_LEN-w),len-(MAX_LEN-w));
}
if(ret != 0)
{
printk("copy write is failed\n");
return -1;
}
w = (w+len)%MAX_LEN;
if(r <= w)
rlen = w - r;
else
rlen = w + (MAX_LEN-r);
wake_up_interruptible(&my_queue);
return len;
}
static struct file_operations myfops={
.open = myopen,
.release = myrelease,
.write = mywrite,
.read = myread,
};
//初始化
int myinit(void)
{
int ret = -1;
//向上内核
no = MKDEV(MA,MI);
//1.注册设备号
ret = register_chrdev_region(no,count,name);
if(ret != 0)
{
printk("register is failed\n");
return -1;
}
//2.设备操作初始化
cdev_init(&mydev,&myfops);
//3.添加设备
ret = cdev_add(&mydev,no,count);
if(ret != 0)
{
unregister_chrdev_region(no,count);
printk("add is failed\n");
return -1;
}
//向下驱动
spin_lock_init(&lock);
mutex_init(&my_mutex);
init_waitqueue_head(&my_queue);
printk("my init is ready\n");
return 0;
}
//退出
void myexit(void)
{
//删除设备
cdev_del(&mydev);
//注销设备号
unregister_chrdev_region(no,count);
printk("my exit is ready\n");
}
module_init(myinit);
module_exit(myexit);
//信息
MODULE_LICENSE("GPL");
应用程序
/*===============================================
* 文件名称:mainSpin.c
* 创 建 者: memories
* 创建日期:2023年07月11日
* 描 述:have a nice day
================================================*/
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
char wbuf0[32] = "01234567899876543210";
char wbuf1[32] = "abcdefg";
char buf[16] = {0};
int fd = 0;
void *fun1(void* arg )
{
sleep(1);
write(fd,1234567,8);
printf("write 1 is ok\n");
sleep(1);
write(fd,"abc",2);
printf("write 2 is ok\n");
sleep(1);
write(fd,"8912",3);
printf("write 3 is ok\n");
}
int main(int argc, char *argv[])
{
int ret = 0;
fd = open("/dev/mydev",O_RDWR);
if(fd < 0)
{
printf("open fd failed\n");
return;
}
pthread_t t1,t2;
ret = pthread_create(&t1,NULL,fun1,NULL);
if(ret != 0)
{
printf("pthread is failed\n");
return -1;
}
pthread_detach(t1);
printf("read is ready\n");
read(fd,buf,5);
printf("buf1=%s\n",buf);
read(fd,buf,5);
printf("buf2=%s\n",buf);
read(fd,buf,5);
printf("buf3=%s\n",buf);
return 0;
}
异步通知 select-poll
在驱动里面会提供一个select文件操作集为poll,驱动在进行通讯时,自动的写自动的读
static wait_queue_head_t my_queue;//定义等待队列
驱动程序
/*===============================================
* 文件名称:modSpin.c
* 创 建 者: memories
* 创建日期:2023年07月11日
* 描 述:have a nice day
================================================*/
//头文件
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/kdev_t.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define MA 300
#define MI 0
#define MAX_LEN 10
static dev_t no = 0;
static unsigned count = 1;
static const char * name = "mydev";
static struct cdev mydev;
static char buf[MAX_LEN] = {0};
static spinlock_t lock;
static struct mutex my_mutex;
static wait_queue_head_t my_queue;
static int flag = 0;
int myopen(struct inode *pi, struct file *pf)
{
if( mutex_trylock(&my_mutex)==0 )
return -1;
return 0;
}
int myrelease(struct inode *pi, struct file *pf)
{
mutex_unlock(&my_mutex);
return 0;
}
ssize_t myread (struct file *pf, char __user *rbuf, size_t len, loff_t *poff)
{
int ret = -1;
ret = copy_to_user(rbuf,buf,len);
if(ret != 0)
{
printk("copy read is failed\n");
return -1;
}
flag = 0;
return len;
}
ssize_t mywrite (struct file *pf, const char __user *wbuf, size_t len, loff_t *poff)
{
int ret = -1;
ret = copy_from_user(buf,wbuf,len);
if(ret != 0)
{
printk("copy write is failed\n");
return -1;
}
flag = 1;
return len;
}
unsigned int mypoll(struct file *filp, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(filp, &my_queue, wait);//调用select、poll、epoll的进程挂到驱动程序的等待队列中
if (flag==1)//可读
{
mask |= POLLIN | POLLRDNORM; /*标示数据可获得*/
}
if (flag==0)//可写
{
mask |= POLLOUT | POLLWRNORM; /*标示数据可写入*/
}
return mask;
}
static struct file_operations myfops={
.open = myopen,
.release = myrelease,
.write = mywrite,
.read = myread,
.poll = mypoll,
};
//初始化
int myinit(void)
{
int ret = -1;
//向上内核
no = MKDEV(MA,MI);
//1.注册设备号
ret = register_chrdev_region(no,count,name);
if(ret != 0)
{
printk("register is failed\n");
return -1;
}
//2.设备操作初始化
cdev_init(&mydev,&myfops);
//3.添加设备
ret = cdev_add(&mydev,no,count);
if(ret != 0)
{
unregister_chrdev_region(no,count);
printk("add is failed\n");
return -1;
}
//向下驱动
spin_lock_init(&lock);
mutex_init(&my_mutex);
init_waitqueue_head(&my_queue);
printk("my init is ready\n");
return 0;
}
//退出
void myexit(void)
{
//删除设备
cdev_del(&mydev);
//注销设备号
unregister_chrdev_region(no,count);
printk("my exit is ready\n");
}
module_init(myinit);
module_exit(myexit);
//信息
MODULE_LICENSE("GPL");
应用程序
/*===============================================
* 文件名称:mainSpin.c
* 创 建 者: memories
* 创建日期:2023年07月11日
* 描 述:have a nice day
================================================*/
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
char wbuf[32] = "01234567899876543210";
char wbuf1[32] = "abcdefg";
char rbuf[16] = {0};
int fd = 0;
int main(int argc, char *argv[])
{
int ret = 0;
fd = open("/dev/mydev",O_RDWR);
if(fd < 0)
{
printf("open fd failed\n");
return;
}
while(1)
{
fd_set rds,wds;
FD_ZERO(&rds);
FD_ZERO(&wds);
FD_SET(fd,&rds);
FD_SET(fd,&wds);
select(fd+1,&rds,&wds,NULL,0);
if(FD_ISSET(fd,&rds))
{
read(fd,rbuf,16);
printf("rbuf is %s\n",rbuf);
}
if(FD_ISSET(fd,&wds))
{
write(fd,wbuf,16);
printf("write is ok\n");
}
}
close(fd);
return 0;
}
信号与函数
驱动函数
//1.异步结构定义
//2.异步初始化
//3.发送信号
/*===============================================
* 文件名称:modSpin.c
* 创 建 者: memories
* 创建日期:2023年07月11日
* 描 述:have a nice day
================================================*/
//头文件
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/kdev_t.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define MA 300
#define MI 0
#define MAX_LEN 10
static dev_t no = 0;
static unsigned count = 1;
static const char * name = "mydev";
static struct cdev mydev;
static char buf[MAX_LEN] = {0};
static spinlock_t lock;
static struct mutex my_mutex;
static struct fasync_struct *async_queue = NULL;
static int flag = 0;
int myopen(struct inode *pi, struct file *pf)
{
if( mutex_trylock(&my_mutex)==0 )
return -1;
return 0;
}
int myrelease(struct inode *pi, struct file *pf)
{
mutex_unlock(&my_mutex);
return 0;
}
ssize_t myread (struct file *pf, char __user *rbuf, size_t len, loff_t *poff)
{
int ret = -1;
ret = copy_to_user(rbuf,buf,len);
if(ret != 0)
{
printk("copy read is failed\n");
return -1;
}
flag = 0;
return len;
}
ssize_t mywrite (struct file *pf, const char __user *wbuf, size_t len, loff_t *poff)
{
int ret = -1;
ret = copy_from_user(buf,wbuf,len);
if(ret != 0)
{
printk("copy write is failed\n");
return -1;
}
flag = 1;
kill_fasync(&async_queue,SIGIO,POLL_IN);
return len;
}
int myfasync(int fd, struct file *filp, int mode)
{
return fasync_helper(fd,filp,mode,&async_queue);
}
static struct file_operations myfops={
.open = myopen,
.release = myrelease,
.write = mywrite,
.read = myread,
.fasync = myfasync,
};
//初始化
int myinit(void)
{
int ret = -1;
//向上内核
no = MKDEV(MA,MI);
//1.注册设备号
ret = register_chrdev_region(no,count,name);
if(ret != 0)
{
printk("register is failed\n");
return -1;
}
//2.设备操作初始化
cdev_init(&mydev,&myfops);
//3.添加设备
ret = cdev_add(&mydev,no,count);
if(ret != 0)
{
unregister_chrdev_region(no,count);
printk("add is failed\n");
return -1;
}
//向下驱动
spin_lock_init(&lock);
mutex_init(&my_mutex);
printk("my init is ready\n");
return 0;
}
//退出
void myexit(void)
{
//删除设备
cdev_del(&mydev);
//注销设备号
unregister_chrdev_region(no,count);
printk("my exit is ready\n");
}
module_init(myinit);
module_exit(myexit);
//信息
MODULE_LICENSE("GPL");
应用函数
//1.绑定signal信号
signal(SIGIO,handler);
//2.设置进程和信号绑定 fd是在进程里面工作的(file--->f_owner)
fcntl(fd,F_SETOWN,getpid());
//3.设置异步标志
int oflags = fcntl(fd,F_GETFL);
fcntl(fd,F_SETFL,oflags|FASYNC);//让fd设备具备异步通知的能力
/*===============================================
* 文件名称:mainSpin.c
* 创 建 者: memories
* 创建日期:2023年07月11日
* 描 述:have a nice day
================================================*/
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
char wbuf[32] = "01234567899876543210";
char wbuf1[32] = "abcdefg";
char rbuf[16] = {0};
int fd = 0;
void handler(int no)
{
read(fd,rbuf,16);
printf("rbuf=%s\n",rbuf);
}
int main(int argc, char *argv[])
{
int ret = 0;
fd = open("/dev/mydev",O_RDWR);
if(fd < 0)
{
printf("open fd failed\n");
return;
}
while(1)
{
signal(SIGIO,handler);
fcntl(fd,F_SETOWN,getpid());
int oflags = fcntl(fd,F_GETFL);
fcntl(fd,F_SETFL,oflags|FASYNC);
sleep(1);
write(fd,wbuf,16);
printf("write is ok\n");
}
close(fd);
return 0;
}