一、用select实现的并发服务器,能达到的并发数,受两方面限制
1、一个进程能打开的最大文件描述符限制。这可以通过调整内核参数。可以通过ulimit -n来调整或者使用setrlimit函数设置, 但一个系统所能打开的最大数也是有限的,跟内存大小有关,可以通过cat /proc/sys/fs/file-max 查看
2、select中的fd_set集合容量的限制(FD_SETSIZE,一般为1024) ,这需要重新编译内核。
可以写个测试程序,只建立连接,看看最多能够建立多少个连接,客户端程序如下:
#include #include #include #include #include #include #include #include #include #define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while(0)int main(void){ int count = 0; while(1) { int sock; if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))
需要C/C++ Linux服务器架构师学习资料私信“资料”(资料包括C/C++,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享
启动select 的服务器端程序,再启动客户端测试程序:
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./echoser_select
......................................................................
count = 1015
recv connect ip=127.0.0.1 port=51299
count = 1016
recv connect ip=127.0.0.1 port=51300
count = 1017
recv connect ip=127.0.0.1 port=51301
count = 1018
recv connect ip=127.0.0.1 port=51302
count = 1019
recv connect ip=127.0.0.1 port=51303
count = 1020
recv connect ip=127.0.0.1 port=51304
accept error: Too many open files
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./conntest
....................................................................................................
count = 1015
ip=127.0.0.1 port=51299
count = 1016
ip=127.0.0.1 port=51300
count = 1017
ip=127.0.0.1 port=51301
count = 1018
ip=127.0.0.1 port=51302
count = 1019
ip=127.0.0.1 port=51303
count = 1020
ip=127.0.0.1 port=51304
count = 1021
socket: Too many open files
输出太多条目,上面只截取最后几条,从中可以看出对于客户端,最多只能开启1021个连接套接字,因为总共是1024个,还得除去0,1,2。而服务器端只能accept 返回1020个已连接套接字,因为除了012之外还有一个监听套接字,客户端某一个套接字(不一定是最后一个)虽然已经建立了连接,在已完成连接队列中,但accept 返回时达到最大描述符限制,返回错误,打印提示信息。
也许有人会注意到上面有一行 sleep(4); 当客户端调用socket准备创建第1022个套接字时,如上所示也会提示错误,此时socket函数返回-1出错,如果没有睡眠4s后再退出进程会有什么问题呢?如果直接退出进程,会将客户端所打开的所有套接字关闭掉,即向服务器端发送了很多FIN段,而此时也许服务器端还一直在accept ,即还在从已连接队列中返回已连接套接字,此时服务器端除了关心监听套接字的可读事件,也开始关心前面已建立连接的套接字的可读事件,read 返回0,所以会有很多 client close 字段 参杂在条目的输出中,还有个问题就是,因为read 返回0,服务器端会将自身的已连接套接字关闭掉,那么也许刚才说的客户端某一个连接会被accept 返回,即测试不出服务器端真正的并发容量。
将 sleep(4); 注释掉,观察服务器端的输出如下:
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./echoser_select
...........................................................
count = 1018
recv connect ip=127.0.0.1 port=52323
client close
count = 1019
recv connect ip=127.0.0.1 port=52324
client close
count = 1020
recv connect ip=127.0.0.1 port=52325
client close
count = 1021
recv connect ip=127.0.0.1 port=52234
client close
client close
可以看到输出参杂着client close,且这次的count 达到了1021,原因就是服务器端前面已经有些套接字关闭了,所以accept 创建套接字不会出错,服务器进程也不会因为出错而退出,可以看到最后接收到的一个连接端口是52234,即不一定是客户端的最后一个连接。
二、poll 函数应用举例
#include
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
参数1:结构体数组指针,struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
结构体中的fd 即套接字描述符,events 即感兴趣的事件,如下图所示,revents 即返回的事件。
参数2:结构体数组的成员个数,即文件描述符个数。
参数3:即超时时间,若为-1,表示永不超时。
poll 跟 select 还是很相似的,比较重要的区别在于poll 所能并发的个数跟FD_SETSIZE无关,只跟一个进程所能打开的文件描述符个数有关,可以在select 程序的基础上修改成poll 程序,在运行服务器端程序之前,使用ulimit -n 2048 将限制改成2048个,注意在运行客户端进程的终端也需更改,因为客户端也会有所限制,这只是临时性的更改,因为子进程会继承这个环境参数,而我们是在bash命令行启动程序的,故在进程运行期间,文件描述符的限制为2048个。
使用poll 函数的服务器端程序如下:
/************************************************************************* > File Name: echoser.c > Author: Simba > Mail: dameng34@163.com > Created Time: Fri 01 Mar 2013 06:15:27 PM CST ************************************************************************/#include#include#include#include#include#include#include#include#include#include#include#include#include "read_write.h"#define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while (0)int main(void){ int count = 0; signal(SIGPIPE, SIG_IGN); int listenfd; //被动套接字(文件描述符),即只可以accept, 监听套接字 if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) maxi) maxi = i; break; } } if (i == 2048) { fprintf(stderr, "too many clients"); exit(EXIT_FAILURE); } printf("count = %d", ++count); printf("recv connect ip=%s port=%d", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); client[i].events = POLLIN; if (--nready <= 0) continue; } for (i = 1; i <= maxi; i++) { conn = client[i].fd; if (conn == -1) continue; if (client[i].revents & POLLIN) { char recvbuf[1024] = {0}; int ret = readline(conn, recvbuf, 1024); if (ret == -1) ERR_EXIT("readline error"); else if (ret == 0) //客户端关闭 { printf("client close "); client[i].fd = -1; close(conn); } fputs(recvbuf, stdout); writen(conn, recvbuf, strlen(recvbuf)); if (--nready <= 0) break; } } } return 0;}/* poll 只受一个进程所能打开的最大文件描述符限制,这个可以使用ulimit -n调整 */
来看一下输出:
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ulimit -n 2048
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./echoser_select
.............................................................................................
recv connect ip=127.0.0.1 port=57430
count = 2039
recv connect ip=127.0.0.1 port=57431
count = 2040
recv connect ip=127.0.0.1 port=57432
count = 2041
recv connect ip=127.0.0.1 port=57433
count = 2042
recv connect ip=127.0.0.1 port=57434
count = 2043
recv connect ip=127.0.0.1 port=57435
count = 2044
recv connect ip=127.0.0.1 port=57436
accept error: Too many open files
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ulimit -n 2048
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./conntest
................................................................................
count = 2039
ip=127.0.0.1 port=57431
count = 2040
ip=127.0.0.1 port=57432
count = 2041
ip=127.0.0.1 port=57433
count = 2042
ip=127.0.0.1 port=57434
count = 2043
ip=127.0.0.1 port=57435
count = 2044
ip=127.0.0.1 port=57436
count = 2045
socket: Too many open files
可以看到现在最大的连接数已经是2045个了,虽然服务器端有某个连接没有accept 返回。即poll 比 select 能够承受更多的并发连接,只受一个进程所能打开的最大文件描述符个数限制。可以通过ulimit -n 修改,但一个系统所能打开的文件描述符个数也是有限的,这跟系统的内存大小有关系,所以说也不是可以无限地并发,可以查看一下本机的容量:
simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ cat /proc/sys/fs/file-max
101078
本机是虚拟机,内存大约1G,能够打开的文件描述符个数大约在10w个左右