linux中阻塞型读写过程,Linux驱动:阻塞式读写测试

#include 

#include 

#include 

//#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#define DEVICE_NAME "test_driver"

#define T_MAJORS    800

//设备结构

staticstruct_Test_Driver_Device

{

structcdev fun_cdev;

//定义缓冲

unsignedchar*buffer,*end;

//读写指针

unsignedchar*rp,*wp;

//读信号量

structsemaphore sem_r;

//写信号量

structsemaphore sem_w;

//等待队列头

structwait_queue_head_t *wq;

};

struct_Test_Driver_Device *Test_Driver_Device;

staticdev_t dev;

staticstructclass*test_class;

//开辟缓存,用来读写

#define LEN_BUF 256

staticunsignedcharBuffer[LEN_BUF];

//功能:初始化缓存

staticvoidinit_buf(void)

{

memset(Buffer,0,LEN_BUF);

}

//功能:读取缓存

//返回:读取的字节数

ssize_t test_driver_read(structfile *filp,char__user *buf,size_tcount,loff_t *f_pos)

{

inttemp1 = 0,temp2 = 0,temp3 = 0;

//获取信号量

if(down_interruptible(&Test_Driver_Device->sem_r))

{

return-ERESTARTSYS;

}

//循环防止解除阻塞时的竞争

while(Test_Driver_Device->wp == Test_Driver_Device->rp)

{

//读不到数据

//释放信号量

up(&Test_Driver_Device->sem_r);

//判断是否是非阻塞读

if(filp->f_flags & O_NONBLOCK)

{

return-EAGAIN;

}

//如果是阻塞式读,则阻塞

if(wait_event_interruptible(Test_Driver_Device->wq,Test_Driver_Device->wp != Test_Driver_Device->rp))

{

return-ERESTARTSYS;

}

//获取信号量

if(down_interruptible(&Test_Driver_Device->sem_r))

{

return-ERESTARTSYS;

}

}

if(Test_Driver_Device->wp > Test_Driver_Device->rp)

{

count = min(count,(size_t)(Test_Driver_Device->wp - Test_Driver_Device->rp));

//拷贝数据到用户空间

if(copy_to_user(buf,Test_Driver_Device->rp,count))

{

return-EFAULT;

}

Test_Driver_Device->rp += count;

}

else

{

temp1 = Test_Driver_Device->end - Test_Driver_Device->rp + 1;

temp2 = Test_Driver_Device->wp - Test_Driver_Device->buffer;

if(count <= temp1)

{

//拷贝数据到用户空间

if(copy_to_user(buf,Test_Driver_Device->rp,count))

{

return-EFAULT;

}

Test_Driver_Device->rp += count;

}

else

{

//拷贝数据到用户空间

if(copy_to_user(buf,Test_Driver_Device->rp,temp1))

{

return-EFAULT;

}

Test_Driver_Device->rp = Test_Driver_Device->buffer;

temp3 = min(count - temp1,temp2);

count = temp1 + temp3;

if(copy_to_user(buf + temp1,Test_Driver_Device->rp,temp3))

{

return-EFAULT;

}

Test_Driver_Device->rp += temp3;

}

}

if(Test_Driver_Device->rp == Test_Driver_Device->end + 1)

{

Test_Driver_Device->rp = Test_Driver_Device->buffer;

}

//释放信号量

up(&Test_Driver_Device->sem_r);

printk (DEVICE_NAME"\tjdh:rp zhi zhen = %d\n",Test_Driver_Device->rp - Test_Driver_Device->buffer);

returncount;

}

//功能:写入缓存

//返回:写入的字节数

ssize_t test_driver_write(structfile *filp,constchar__user *buf,size_tcount,loff_t *f_pos)

{

inttemp1 = 0,temp2 = 0;;

//判断需要写入的字节数是否大于缓存

if(count > LEN_BUF)

{

return-ENOMEM;

}

//获取信号量

if(down_interruptible(&Test_Driver_Device->sem_w))

{

return-ERESTARTSYS;

}

//写入缓存

if(count <= (Test_Driver_Device->end - Test_Driver_Device->wp + 1))

{

//从用户空间拷贝数据

if(copy_from_user(Test_Driver_Device->wp,buf,count))

{

return-EFAULT;

}

Test_Driver_Device->wp += count;

}

else

{

temp1 = Test_Driver_Device->end - Test_Driver_Device->wp + 1;

temp2 = count - temp1;

//从用户空间拷贝数据

if(copy_from_user(Test_Driver_Device->wp,buf,temp1))

{

return-EFAULT;

}

Test_Driver_Device->wp = Test_Driver_Device->buffer;

//从用户空间拷贝数据

if(copy_from_user(Test_Driver_Device->wp,buf + temp1,temp2))

{

return-EFAULT;

}

Test_Driver_Device->wp += temp2;

}

if(Test_Driver_Device->wp == Test_Driver_Device->end + 1)

{

Test_Driver_Device->wp = Test_Driver_Device->buffer;

}

//唤醒阻塞进程

wake_up_interruptible(&Test_Driver_Device->wq);

//释放信号量

up(&Test_Driver_Device->sem_w);

printk (DEVICE_NAME"\tjdh:wp zhi zhen = %d\n",Test_Driver_Device->wp - Test_Driver_Device->buffer);

returncount;

}

staticstructfile_operations io_dev_fops = {

.owner = THIS_MODULE,

.write = test_driver_write,

.read = test_driver_read,

};

staticint__init dev_init(void)

{

intret;

unsigned temp;

init_buf();

//分配结构体

Test_Driver_Device = kmalloc(sizeof(struct_Test_Driver_Device),GFP_KERNEL);

if(!Test_Driver_Device)

{

unregister_chrdev_region(dev,1);

device_destroy(test_class, dev);

class_destroy(test_class);

return-ENOMEM;

}

//定义缓冲的开始和结束的指针

Test_Driver_Device->buffer = Buffer;

Test_Driver_Device->end = Buffer + LEN_BUF - 1;

Test_Driver_Device->rp = Test_Driver_Device->buffer;

Test_Driver_Device->wp = Test_Driver_Device->buffer;

//初始化读信号量

sema_init(&Test_Driver_Device->sem_r,1);

//初始化写信号量

sema_init(&Test_Driver_Device->sem_w,1);

//初始化等待队列头

init_waitqueue_head(&Test_Driver_Device->wq);

dev = MKDEV(T_MAJORS,0);

cdev_init(&Test_Driver_Device->fun_cdev,&io_dev_fops);

ret = register_chrdev_region(dev,1,DEVICE_NAME);

if(ret 

ret = cdev_add(&Test_Driver_Device->fun_cdev,dev,1);

if(ret 

printk (DEVICE_NAME"\tjdh:test_driver initialized!!\n");

test_class = class_create(THIS_MODULE,"test_class1");

if(IS_ERR(test_class))

{

printk(KERN_INFO"create class error\n");

return-1;

}

device_create(test_class, NULL, dev, NULL,"test_driver");

returnret;

}

staticvoid__exit dev_exit(void)

{

unregister_chrdev_region(dev,1);

device_destroy(test_class, dev);

class_destroy(test_class);

}

module_init(dev_init);

module_exit(dev_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("JDH");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值