linux select poll epoll IO多路复用简单使用

简介

select,poll,epoll都是IO多路复用的机制。I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪,能够通知程序进行相应的读写操作。select,poll,epoll本质上都是同步I/O


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


int main()
{
    int fd;
    int ret;
    int length;
    fd_set r_fds;
    char buffer[100];
    struct timeval timeout;
    // 标准输入流
    fd = 0;

    FD_ZERO(&r_fds);
    FD_SET(fd, &r_fds);
    timeout.tv_sec = 0;
    timeout.tv_usec = 500 * 1000;

    while (1) {
        // 每次循环都要清空集合,否则不能检测描述符变化
        FD_ZERO(&r_fds);
        FD_SET(fd, &r_fds);
        ret = select(fd + 1, &r_fds, NULL, NULL, &timeout);
        if (ret <= 0) {
            continue;
        } else if (ret > 0 && FD_ISSET(fd, &r_fds)) {
            memset(buffer, 0, sizeof(buffer));
            length =  read(fd, buffer, sizeof(buffer));
            if (length > 0) {
                printf("read data = %s\n", buffer);
            }
        }
    }
    return 0;
}

poll

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    int fd;
    int ret;
    int length;
    char buffer[100];
    struct timeval timeout;
    struct pollfd pfd;
    // 标准输入流
    pfd.fd = 0;
    pfd.events = POLLIN | POLLERR;
    pfd.revents = 0;


    while (1) {
        ret = poll(&pfd, 1, 1000);
        // 超时
        if (ret == 0) {
            continue;
        } else if (ret > 0) {
            memset(buffer, 0, sizeof(buffer));
            length =  read(pfd.fd, buffer, sizeof(buffer));
            if (length > 0) {
                printf("poll read data = %s\n", buffer);
            }
        }
    }
    return 0;
}

epoll

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/epoll.h>

 #define MAX_EVENTS 10

int main()
{
    int fd;
    int i;
    int epollfd;
    int ret;
    int length;
    fd_set r_fds;
    char buffer[100];

    struct epoll_event event, events[MAX_EVENTS];

    // 标准输入流
    fd = 0;
    epollfd = epoll_create(MAX_EVENTS);
    if (epollfd < 0) {
        perror("epoll_create");
        return -1;
    }

    // 设置事件描述符和要处理的事件类型
    event.data.fd = fd;
    event.events = EPOLLIN | EPOLLET;
    epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);

    while (1) {
        ret = epoll_wait(epollfd, events, MAX_EVENTS, 1000);
        //printf("ret = %d\n", ret);
        if (ret == 0) {
            // timeout
            continue;
        } else if (ret > 0) {
            for (i = 0 ; i < ret ; i++) {
                if (events[i].data.fd == fd) {
                    memset(buffer, 0, sizeof(buffer));
                    length =  read(events[i].data.fd, buffer, sizeof(buffer));
                    if (length > 0) {
                        printf("epoll read data = %s\n", buffer);
                    }
                }
            }
        }
    }
    return 0;
}

推荐
https://www.cnblogs.com/aspirant/p/9166944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值