epoll实现IO多路复用

pro1.c

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/epoll.h>
#include <sys/time.h>

int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int fd1,fd2,epfd;
    struct epoll_event event;  //用于操作epoll
    struct epoll_event events[10];  //存放就绪事件描述符的数组
    //创建epoll句柄
    epfd = epoll_create(1);
    if(epfd < 0)
    {
        printf("epoll_create filed\n");
        exit(-1);
    }
    //打开设备文件
    fd1 = open("/dev/input/mouse0",O_RDWR);
    if(fd1 < 0)
    {
        printf("打开鼠标设备失败\n");
        return -1;;
    }
    fd2 = open("/dev/myled0",O_RDWR);
    if(fd1 < 0)
    {
        printf("打开设备失败\n");
        return -1;;
    }
    //添加准备就绪事件进去epoll
    event.events = EPOLLIN;//读事件
    event.data.fd = fd1;
    if(epoll_ctl(epfd,EPOLL_CTL_ADD,fd1,&event) < 0)
    {
        printf("epoll_ctl add filed\n");
    }
    event.events = EPOLLIN;//读事件
    event.data.fd = fd2;
    if(epoll_ctl(epfd,EPOLL_CTL_ADD,fd2,&event) < 0)
    {
        printf("epoll_ctl add filed\n");
    }
    //监听事件是否发生
    while(1)
    {
       //如果成功,ret接受返回的事件个数,把就绪事件放在events数组中
       int ret = epoll_wait(epfd,events,10,-1);
       if(ret < 0)
       {
        printf("epoll_wait filed\n");
        exit(-1);
       }
       int i;
       //循环遍历数组,做事件的处理
       for(i=0;i<ret;i++)
       {
            if(events[i].events & EPOLLIN);
            {
                memset(buf,0,sizeof(buf));
                read(events[i].data.fd,buf,sizeof(buf));
                printf("buf:%s\n",buf);
            }   
       }
        
    }
    //关闭文件描述符
    close(fd1);
    close(fd2);
    return 0;
}

pro2.c

#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>

int main(int argc, char const *argv[])
{
    char buf[128]="hello world";
    int a,b;
    int fd=open("/dev/myled0",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
   write(fd,buf,sizeof(buf));//模拟硬件就绪事件
}

mycdev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/poll.h>


int major;
char kbuf[128] = {0};
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//定义等待队列
wait_queue_head_t wq_head;
unsigned int condition=0;
int mycdev_open(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if(file->f_flags&O_NONBLOCK)//用户程序以非阻塞读写数据
    {}
    else
    {
       wait_event_interruptible(wq_head,condition);//判断condition的值,为假则进程休眠
    }
    //将获取到的硬件数据拷贝到用户
    int ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    condition=0;//表示下一次数据没有准备就绪
    return 0;
}
//封装POLL方法
__poll_t mycdev_poll(struct file *file,struct poll_table_struct *wait)
{
    __poll_t mask = 0;
    //向上提交等待队列头
    poll_wait(file,&wq_head,wait);
    //根据数据是否就绪给定一个合适的返回值
    if(condition)
        mask = POLLIN;
        return mask;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    int ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    condition=1;//表示硬件数据就绪
    wake_up_interruptible(&wq_head);//唤醒休眠的进程
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .poll = mycdev_poll,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{

    //初始化等待队列
    init_waitqueue_head(&wq_head);
    // 字符设备驱动注册
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    int i; // 向上提交三次设备节点信息
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点成功\n");

    return 0;
}
static void __exit mycdev_exit(void)
{

    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值