Linux教程

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

本文主要是介绍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

这篇关于linux select poll epoll IO多路复用简单使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!