IO 多路复用 — select 和 poll
本文为 高性能网络编程学习 系列文章的第2篇 下面我们使用select, poll, epoll来进行IO multiplexing 本文我们演示 select 和 poll做IO多路复用 select的用法查看man page就可以得到,这里只列出遇到的问题 nfds参数,表明要watch的最大fd的数字 举例说明,如果你要watch的文件描述符为24 — 31,那么你应该将nfds设置为32 为了防止SIGPIPE信号终止程序运行,要讲SIGPIPE信号忽略掉 代码如下 /************************************************************************* > File Name: server.c > Author: VOID_133 > Mail: ################### > Created Time: Wed 21 Dec 2016 04:03:25 PM HKT ************************************************************************/ #define _GNU_SOURCE #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<fcntl.h> #include<pthread.h> #include<arpa/inet.h> #include<sys/socket.h> #include<sys/select.h> #include<string.h> #include<sys/errno.h> #define MAX_BUF_SIZE 1000 void usage(char *proc_name) { printf(“%s [port]\n”, proc_name); exit(1); } void setnonblock(int fd) { int old_flag = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, old_flag | O_NONBLOCK); return ; }…