Linux高级文件操作 -6
2016-10-16 11:48
197 查看
比较poll()和epoll()
poll()已经很好地标准化,但是不具有很好的扩展性,但是epoll仅仅在Linux上扩展得比较好。当有少量的文件描述符,并且移植性重要的时候,应该使用poll()。
当有大量的文件描述符,epoll是更好的选择。
/* poll-vs-epoll.c - Compares the performance of \codefn{poll()} and \codefn{epoll} */ #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <sys/epoll.h> #include <sys/poll.h> #include <sys/signal.h> #include <unistd.h> #include <sys/resource.h> #include <string.h> #include <stdlib.h> #include <sys/select.h> int gotAlarm; void catch(int sig) { gotAlarm = 1; } #define OFFSET 10 int main(int argc, const char ** argv) { int pipeFds[2]; int count; int numFds; struct pollfd * pollFds; struct epoll_event event; int epfd; int i; struct rlimit lim; char * end; if (!argv[1]) { fprintf(stderr, "number expected\n"); return 1; } numFds = strtol(argv[1], &end, 0); if (*end) { fprintf(stderr, "number expected\n"); return 1; } printf("Running test on %d file descriptors.\n", numFds); lim.rlim_cur = numFds + OFFSET; lim.rlim_max = numFds + OFFSET; if (setrlimit(RLIMIT_NOFILE, &lim)) { perror("setrlimit"); exit(1); } pipe(pipeFds); pollFds = malloc(sizeof(*pollFds) * numFds); epfd = epoll_create(numFds); event.events = EPOLLIN; for (i = OFFSET; i < OFFSET + numFds; i++) { if (dup2(pipeFds[0], i) != i) { printf("failed at %d: %s\n", i, strerror(errno)); exit(1); } pollFds[i - OFFSET].fd = i; pollFds[i - OFFSET].events = POLLIN; event.data.fd = i; epoll_ctl(epfd, EPOLL_CTL_ADD, i, &event); } /* use a signal to know when time's up */ signal(SIGALRM, catch); count = 0; gotAlarm = 0; alarm(1); while (!gotAlarm) { poll(pollFds, numFds, 0); count++; } printf("poll() calls per second: %d\n", count); alarm(1); count = 0; gotAlarm = 0; alarm(1); while (!gotAlarm) { epoll_wait(epfd, &event, 1, 0); count++; } printf("epoll() calls per second: %d\n", count); return 0; }
相关文章推荐
- Linux-C高级编程(1)文件操作(一)
- Linux高级文件操作 -2
- 小鸟chacha初学linux 高级文件操作
- Linux高级文件操作 -3
- Linux高级文件操作 -5
- Linux常用高级文件操作命令
- Linux常用高级文件操作命令
- Linux设备驱动程序学习(6) -高级字符驱动程序操作[(3)设备文件的访问控制]
- Linux设备驱动程序学习(6)-高级字符驱动程序操作[(3)设备文件的访问控制]
- Linux高级文件操作 -4
- Linux-C高级编程(1)文件操作(二)
- Linux常用高级文件操作命令
- Linux高级文件操作 -1
- linux 下常用的文件操作指令
- linux下操作远程主机文件的几种方法
- Linux文件和目录的操作(七) 目录树 tree
- linux磁盘 分区 物理卷 卷组 逻辑卷 文件系统加载点操作案例
- linux 下日志文件操作的基本命令(tail,grep)
- 【Linux命令】文件和目录操作命令
- Linux文件操作-1