高级I/O操作-非阻塞型I/O

2019-07-13 03:11发布

1 参考书籍
参考书籍 《嵌入式Linux驱动开发教程》 华清远见嵌入式学院
2 简介
如果应用程序以非阻塞的方式打开设备文件,当资源不可用时,驱动就应该立刻返回并用一个错误码EAGAIN来通知应用程序此时资源不可用。
3 范例代码 /*非阻塞型I/O*/ /*vser.c*/ #include #include #include #include #include #include #include #include #include "vser.h" #define VSER_MAJOR 256 #define VSER_MINOR 0 #define VSER_DEV_CNT 1 #define VSER_DEV_NAME "vser" struct vser_dev { unsigned int baud; /*波特率*/ struct option opt; /*帧格式信息*/ struct cdev cdev; }; DEFINE_KFIFO(vsfifo, char, 32); static struct vser_dev vsdev; static int vser_open(struct inode *inode, struct file *filp) { return 0; } static int vser_release(struct inode *inode, struct file *filp) { return 0; } static ssize_t vser_read(struct file *filp, char __user *buf, size_t count, loff_t *pos) { int ret; unsigned int copied = 0; if( kfifo_is_empty(&vsfifo)) /*判断fifo是否为空*/ { /*判断设备文件是否是以非阻塞的方式打开*/ if(filp->f_flags & O_NONBLOCK) { return -EAGAIN; } } ret = kfifo_to_user(&vsfifo, buf, count, &copied); return ret == 0 ? copied : ret; } static ssize_t vser_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos) { int ret; unsigned int copied = 0; if(kfifo_is_full(&vsfifo)) { if(filp->f_flags & O_NONBLOCK) return -EAGAIN; } ret = kfifo_from_user(&vsfifo, buf, count, &copied); return ret == 0 ? copied : ret; } static long vser_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { if(_IOC_TYPE(cmd) != VS_MAGIC) { return -ENOTTY; } switch(cmd) { case VS_SET_BAUD: vsdev.baud=arg; break; case VS_GET_BAUD: arg=vsdev.baud; break; case VS_SET_FFMT: if( copy_from_user(&vsdev.opt,(struct option __user*)arg,sizeof(struct option)) ) return -EFAULT; break; case VS_GET_FFMT: if( copy_to_user((struct option __user*)arg,&vsdev.opt,sizeof(struct option)) ) return -EFAULT; break; default: return -ENOTTY; } return 0; } static struct file_operations vser_ops = { .owner = THIS_MODULE, .open = vser_open, .release = vser_release, .read = vser_read, .write = vser_write, .unlocked_ioctl = vser_ioctl, }; static int __init vser_init(void) { int ret; dev_t dev; /*设备号*/ dev = MKDEV(VSER_MAJOR, VSER_MINOR); /*注册字符设备*/ ret = register_chrdev_region(dev, VSER_DEV_CNT, VSER_DEV_NAME); if (ret) goto reg_err; cdev_init(&vsdev.cdev, &vser_ops); vsdev.cdev.owner = THIS_MODULE; vsdev.baud = 115200; vsdev.opt.datab = 8; vsdev.opt.parity = 0; /*奇偶位*/ vsdev.opt.stopb = 1; /*cdev对象初始化以后,就要添加到内核中的cdev_map散列表中 */ ret = cdev_add(&vsdev.cdev, dev, VSER_DEV_CNT); if (ret) goto add_err; return 0; add_err: unregister_chrdev_region(dev, VSER_DEV_CNT); reg_err: return ret; } /*卸载函数*/ static void __exit vser_exit(void) { dev_t dev; dev = MKDEV(VSER_MAJOR, VSER_MINOR); cdev_del(&vsdev.cdev); unregister_chrdev_region(dev, VSER_DEV_CNT); } module_init(vser_init); module_exit(vser_exit); 测试文件: /*test.c*/ #include #include #include #include #include #include #include #include "vser.h" int main() { int fd; int ret; unsigned int baud; char rbuf[32]={0}; char wbuf[32]="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; fd=open("/dev/vser0",O_RDWR|O_NONBLOCK); if(fd == -1) { goto fail; } ret=read(fd,rbuf,sizeof(rbuf)); if(ret < 0) { perror("read"); } ret=write(fd,wbuf,sizeof(wbuf)); if(ret < 0) { perror("first write "); } ret=write(fd,wbuf,sizeof(wbuf)); if(ret < 0) { perror("second write"); } close(fd); exit(EXIT_SUCCESS); fail: perror("ioctl test"); exit(EXIT_FAILURE); }