系统编程之高级文件IO(十五)——poll,epoll简单使用和异步IO及总结

一、poll

基本原理与select一致, 也是轮询+遍历;唯一的区别就是poll没有最大文件描述符限制(使用链表的方式存储fd)

原型:int poll(struct pollfd *fds, nfds_t nfds, int time out);

POLLIN可读
POLLPUT可写
POLLERR异常
nfds:数组个数
timeout:延迟时间

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <poll.h>

int main(int argc, char const *argv[])
{
    int fd;

    if((fd = open("/dev/input/mouse0", O_RDWR | O_CREAT, 0655)) < 0)
    {
        perror("open file error!");
        exit(1);
    }

    struct pollfd fds[2];

    fds[0].fd = 0;
    fds[0].events = POLLIN;

    fds[1].fd = fd;
    fds[1].events = POLLIN;
    while(1)
    {
        int ret = poll(fds, 2, -1);

        for (size_t i = 0; i < 2; i++)
        {
            if (fds[i].events == fds[i].revents)
            {
                if (fds[i].fd == 0)
                {
                    char buffer[1024];

                    read(fds[i].fd, buffer, sizeof(buffer));

                    printf("buffer = %s\n",buffer);
                }
                else if (fds[i].fd == fd)
                {
                    int cor;

                    read(fds[i].fd, &cor, sizeof(cor));

                    printf("cor = %d\n", cor);
                }

                if (--ret == 0)
                {
                    break;
                }
                
                
                
            }
            
        }
        
        
    }

    return 0;
}

二、epoll

自学

在这里插入图片描述
select内部使用数组实现,poll是链表。他们需要做内核区到用户区的转换,还需要做数据拷贝,因此效率低。
epoll不需要做内核区到用户区的转换。因为数据存在共享内存中。epoll维护的树在共享内存中,epqJ维护的树在共享内存中,内核区和用户区去操作共享内存,因此不需要区域转换,也不需要

三、异步IO

进程和网络时再讲,select也算异步,但是有阻塞,一旦阻塞就是同步

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周末不下雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值