1. 程式人生 > >高階I/O操作-阻塞型I/O

高階I/O操作-阻塞型I/O

1 參考書籍

參考書籍 《嵌入式Linux驅動開發教程》 華清遠見嵌入式學院

2 休眠程式的喚醒通常有兩種
(1) 在中斷處理程式負責喚醒
(2) 指定程序的最長休眠時間,超時後程序自動甦醒

3 範例程式碼

/*阻塞型I/O*/
/*vser.c*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kfifo.h>
#include <linux/ioctl.h> #include <linux/uaccess.h> #include <linux/wait.h> #include <linux/sched.h> #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; wait_queue_head_t rwqh; wait_queue_head_t wwqh; /*讀和寫的等待佇列頭*/ }; 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; } /*fifo不是空的,並且是以阻塞的方式開啟*/ /*在fifo為空的情況下放入等待佇列,直到fifo不為空或接收到訊號才被喚醒*/ if(wait_event_interruptible(vsdev.rwqh,!kfifo_is_empty(&vsfifo))) { return -ERESTARTSYS; } } /*在fifo不是滿的情況下,喚醒所有的寫程序*/ if(!kfifo_is_empty(&vsfifo)) { wake_up_interruptible(&vsdev.wwqh); } 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; /*在fifo不為空的條件下進入休眠*/ if(wait_event_interruptible(vsdev.wwqh,!kfifo_is_full(&vsfifo))) { return -ERESTARTSYS; } } ret = kfifo_from_user(&vsfifo, buf, count, &copied); /*在fifo不是滿的情況下,喚醒所有的寫程序*/ if(!kfifo_is_empty(&vsfifo)) { wake_up_interruptible(&vsdev.rwqh); } ret = kfifo_to_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); /*初始化兩個等待佇列頭*/ init_waitqueue_head(&vsdev.rwqh); init_waitqueue_head(&vsdev.wwqh); 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);