内核轮询式IO(poll-select)
来源:互联网 发布:网易养猪场 知乎 编辑:程序博客网 时间:2024/06/05 10:54
驱动层
struct fifo{dev_t num;struct cdev cdev;wait_queue_head_t readqueue;//queue propertychar *qbuf;int start; //posint end; //posint cursize;int maxsize;}FIFO;
static ssize_t fifo_read (struct file *f, char __user *p, size_t n, loff_t *off){int size;if(FIFO.cursize == 0)//let this procss go to sleepwait_event_interruptible(FIFO.readqueue, FIFO.cursize != 0);size = n>FIFO.cursize?FIFO.cursize:n;//return 0;int len1, len2;if(FIFO.start <= FIFO.end)// copy once{if(copy_to_user(p, FIFO.qbuf, size)){printk("copy to user fail1\n");return -EINVAL;}}else{len1 = FIFO.maxsize-FIFO.start;if(copy_to_user(p, &FIFO.qbuf[FIFO.start], len1)){printk("copy to user fail2\n");return -EINVAL;}len2 = size-len1;if(copy_to_user(p+len1, FIFO.qbuf, len2)){printk("copy to user fail3\n");return -EINVAL;}}FIFO.start = (FIFO.start+size)%FIFO.maxsize;FIFO.cursize -= size;return size;}static ssize_t fifo_write (struct file *f, const char __user *p, size_t n, loff_t *off){int space = FIFO.maxsize - FIFO.cursize;int size = n>space?space:n;int len1 = (FIFO.maxsize-1 - FIFO.end);int len2;if(!space)return 0;//copy_from_user(&FIFO.qbuf[end+1], p, size);if(len1 >= size)//copy once{if(copy_from_user(&FIFO.qbuf[FIFO.end+1], p, size)){printk("copy from user fail1\n");return -EINVAL;}}else{if(copy_from_user(&FIFO.qbuf[FIFO.end+1], p, len1)){printk("copy from user fail2\n");return -EINVAL;}len2 = size-len1;if(copy_from_user(FIFO.qbuf, p+len1, len2)){printk("copy from user fail3\n");return -EINVAL;}}FIFO.end = (FIFO.end+size)%FIFO.maxsize;FIFO.cursize += size;//woke up processeswake_up_interruptible(&FIFO.readqueue);//wake_up(&FIFO.readqueue);return size;}static long fifo_ioctl(struct file *f, unsigned int cmd, unsigned long arg){return 0;}static int fifo_release(struct inode *inodep, struct file *filep){//printk("fifo closed\n");return 0;}int fifo_mmap(struct file *f, struct vm_area_struct *vma){return remap_pfn_range(vma, vma->vm_start, virt_to_phys(FIFO.qbuf)>>PAGE_SHIFT, vma->vm_end-vma->vm_start, vma->vm_page_prot);}unsigned int fifo_poll(struct file *filep, struct poll_table_struct *wait){int mask = 0;poll_wait(filep, &FIFO.readqueue, wait);if(FIFO.cursize)mask |= POLLIN | POLLRDNORM; /* readable */return mask;}
应用层:
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(int argc, char **argv){if(4 != argc){printf("Usage: %s <file1> <file2> <file3>\n", argv[0]);return -1;}char buf[100] = {0};int fd1 = open(argv[1], O_RDWR);int fd2 = open(argv[2], O_RDWR);int fd3 = open(argv[3], O_RDWR);fd_set myfds;FD_ZERO(&myfds);FD_SET(fd1, &myfds);FD_SET(fd2, &myfds);FD_SET(fd3, &myfds);while(1){fd_set tem = myfds;select(fd3+1, &tem, NULL, NULL, NULL);if(FD_ISSET(fd1, &tem)){read(fd1, buf, 100);printf("read1: %s\n", buf);}if(FD_ISSET(fd2, &tem)){read(fd2, buf, 100);printf("read2: %s\n", buf);}if(FD_ISSET(fd3, &tem)){read(fd3, buf, 100);printf("read3: %s\n", buf);}}}
0 0
- 内核轮询式IO(poll-select)
- IO复用三种形式(select/poll/epoll)
- 多路复用IO&select&poll
- IO模型及多路复用IO(select、poll、epoll)
- IO多路复用 select与poll
- IO多路复用(select poll epoll)
- IO多路复用:select、poll、epoll
- linux中IO复用(select,poll,epoll)
- linux 内核poll/select/epoll实现剖析(原理经典)
- 内核poll和select系统调用的图解(1)-poll系统调用的整体结构图
- 内核poll和select系统调用的图解(1)-poll系统调用的整体结构图
- IO复用: select 和poll 到epoll
- select、poll、epoll三组IO复用
- IO复用,select、poll、epoll综述
- IO多路复用之select poll epoll
- Linux IO及 select、poll、epoll详解
- select poll epoll的io模型
- IO多路复用之select、poll以及epoll
- Android Greendao3.0框架的使用
- centos 6.5配置epel源
- SDN實驗(一) Mininet使用
- HBse中Shell
- CPU卡程序设计实例(三)命令格式
- 内核轮询式IO(poll-select)
- 百度地图开发平台范例
- 博为峰Java技术文章 ——JavaSE 如何使用窗口组件Ⅰ
- 第七章 7-5 子窗口和键盘
- Repeat Number
- vue实现webapp
- 设计模式-构建者模式
- 二叉树前序,中序,后序遍历详解
- linux与window文件共享——Samba配置详细过程