系统调用之epoll

epoll的介绍详细请看 man epoll,

   或者https://man7.org/linux/man-pages/man7/epoll.7.html

   或者有人翻译过了 https://www.jianshu.com/p/ee381d365a29

 

自己写了个例子测试, 建立两个fifo, 然后传入给epoll, 向fifo中写入字符串。

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

#define MAX_EVENTS 10

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
} while (0)


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

// 解析终端输入
    if (argc < 2) {
        fprintf(stderr, "Usage: %s file...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int nfds, num_open_fds;
    num_open_fds = nfds = argc - 1;
    int fds[num_open_fds];
    printf("size of fds = %lu, num_open_fds = %d\n", sizeof(fds), num_open_fds);
    memset(fds, 0, sizeof(fds));
    for (int j = 0; j < nfds; j++) {
        fds[j] = open(argv[j + 1], O_RDONLY|O_NONBLOCK);
        if (fds[j] == -1)
            errExit("open");

        printf("Opened \"%s\" on fd %d\n", argv[j + 1], fds[j]);
    }

// epoll 监听事件
    struct epoll_event ev[MAX_EVENTS], events[MAX_EVENTS];
    int epollfd = epoll_create1(0);
    if (epollfd == -1) {
        perror("epoll_create1");
        exit(EXIT_FAILURE);
    }
    for (int j = 0; j < nfds; j++) {
        ev[j].events = EPOLLIN|EPOLLET;
        ev[j].data.fd = fds[j];
        if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[j], &ev[j]) == -1) {
            perror("epoll_ctl: listen_sock");
            exit(EXIT_FAILURE);
        }
    }
    while (num_open_fds > 0) {
        nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        if (nfds == -1) {
            perror("epoll_wait");
            exit(EXIT_FAILURE);
        } else {
            printf("nfds = %d\n", nfds);
        }
        for (int n = 0; n < nfds; ++n) {
            if ( events[n].events & EPOLLERR || events[n].events & EPOLLHUP || (! events[n].events & EPOLLIN)) {
                printf("Epoll has error events = %d\n", events[n].events);
                close (events[n].data.fd);
                continue;
            }
            // 读取Input字符串
            char buf[100];
            ssize_t s = read(events[n].data.fd, buf, sizeof(buf));
            if (s == -1)
                errExit("read");
            printf("    read %zd bytes: %.*s\n",
                            s, (int) s, buf);
        }
    }
    exit(EXIT_SUCCESS);
}

编译运行

#编译
gcc ./test_epoll.c -o test_epoll -std=c11


#建立fifo
mkfifo myfifo1 myfifo2

#向fifo写入字符aa
echo "aa" >> myfifo1


#运行
./test_epoll myfifo1 myfifo2
size of fds = 8, num_open_fds = 2
Opened "myfifo1" on fd 3
Opened "myfifo2" on fd 4
nfds = 1
    read 3 bytes: aa

nfds = 1
Epoll has error events = 16

可见,在myfifo1接收到写入后,epoll会返回,并返回myfifo1的fd,然后read读取完成后,epoll还会收到EPOLLHUP(即错误打印16)的挂起事件,然后一次loop结束后,再次block。

错误定义如下图所示,参照链接 https://man7.org/linux/man-pages/man7/epoll.7.html

    EPOLLHUP = 0x010,
#define EPOLLHUP EPOLLHUP

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值