能跑的epoll

编程技术  /  houtizong 发布于 3年前   141
#include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/socket.h> #include <sys/signal.h> #include <sys/resource.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <sys/epoll.h> #define RUNTIMES 16 static int count, writes, fired; static int *pipes; static int num_pipes, num_active, num_writes; static int epfd; static struct epoll_event *events; unsigned long long getustime(void) {    struct timeval tm;    gettimeofday(&tm, NULL);    return (unsigned long long) tm.tv_sec * 1000000ULL + (unsigned long long) tm.tv_usec; } void read_cb(int fd, int idx) {    int widx = idx + num_active + 1;    u_char ch;    if (read(fd, &ch, sizeof(ch)))       count++;    else       fprintf(stderr, "false read event: fd=%d idx=%d\n", fd, idx);    if (writes) {       if (widx >= num_pipes)          widx -= num_pipes;       write(pipes[2 * widx + 1], "e", 1);       writes--;       fired++;    } } int run_once(long *work, unsigned long long *tr) {    int i, res;    unsigned long long ts, te;    fired = 0;    for (i = 0; i < num_active; i++, fired++)       write(pipes[i * 2 + 1], "e", 1);    count = 0;    writes = num_writes;    ts = getustime();    do {       res = epoll_wait(epfd, events, num_pipes, 0);       for (i = 0; i < res; i++)          read_cb(pipes[2 * events[i].data.u32], events[i].data.u32);    } while (count != fired);    te = getustime();    *tr = te - ts;    *work = count;    return (0); } int main (int argc, char **argv) {    struct rlimit rl;    int i, c;    long work;    unsigned long long tr;    int *cp;    struct epoll_event ev;    extern char *optarg;    num_pipes = 100;    num_active = 1;    num_writes = num_pipes;    while ((c = getopt(argc, argv, "n:a:w:")) != -1) {       switch (c) {       case 'n':          num_pipes = atoi(optarg);          break;       case 'a':          num_active = atoi(optarg);          break;       case 'w':          num_writes = atoi(optarg);          break;       default:          fprintf(stderr, "Illegal argument \"%c\"\n", c);          exit(1);       }    }    rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;    if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {       perror("setrlimit");       exit(1);    }    events = calloc(num_pipes, sizeof(struct epoll_event));    pipes = calloc(num_pipes * 2, sizeof(int));    if (events == NULL || pipes == NULL) {       perror("malloc");       exit(1);    }    if ((epfd = epoll_create(num_pipes)) == -1) {       perror("epoll_create");       exit(1);    }    for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {       if (pipe(cp) == -1) {          perror("pipe");          exit(1);       }       fcntl(cp[0], F_SETFL, fcntl(cp[0], F_GETFL) | O_NONBLOCK);    }    for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {       ev.events = EPOLLIN;       ev.data.u32 = i;       if (epoll_ctl(epfd, EPOLL_CTL_ADD, cp[0], &ev) < 0) {          perror("epoll_ctl");          exit(1);       }    }    for (i = 0; i < RUNTIMES; i++) {       run_once(&work, &tr);       if (!work)          exit(1);       fprintf(stdout, "%lf\n", (double) tr / (double) work);    }    exit(0); }

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客