Socket编程实践(10) --select的限制与poll的使用

select的限制

用select实现的并发服务器,能达到的并发数一般受两方面限制:

1)一个进程能打开的最大文件描述符限制。这可以通过调整内核参数。可以通过ulimit -n(number)来调整或者使用setrlimit函数设置,但一个系统所能打开的最大数也是有限的,跟内存大小有关,可以通过cat /proc/sys/fs/file-max 查看

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**示例: getrlimit/setrlimit获取/设置进程打开文件数目**/  
  2. int main()  
  3. {  
  4.     struct rlimit rl;  
  5.     if (getrlimit(RLIMIT_NOFILE, &rl) == -1)  
  6.         err_exit("getrlimit error");  
  7.     cout << "Soft limit: " << rl.rlim_cur << endl;  
  8.     cout << "Hard limit: " << rl.rlim_max << endl;  
  9.     cout << "------------------------->"  << endl;  
  10.   
  11.     rl.rlim_cur = 2048;  
  12.     rl.rlim_max = 2048;  
  13.     if (setrlimit(RLIMIT_NOFILE, &rl) == -1)  
  14.         err_exit("setrlimit error");  
  15.   
  16.     if (getrlimit(RLIMIT_NOFILE, &rl) == -1)  
  17.         err_exit("getrlimit error");  
  18.     cout << "Soft limit: " << rl.rlim_cur << endl;  
  19.     cout << "Hard limit: " << rl.rlim_max << endl;  
  20. }  

2)select中的fd_set集合容量的限制(FD_SETSIZE,1024),这需要重新编译内核才能改变。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**测试: 测试服务器端最多能够建立多少个连接 
  2. server端完整源代码如下(注意使用的是<Socket编程实践(7)中的TCPServer类实现>): 
  3. **/  
  4. int main()  
  5. {  
  6.     signal(SIGPIPE, sigHandlerForSigPipe);  
  7.     try  
  8.     {  
  9.         TCPServer server(8001);  
  10.         int listenfd = server.getfd();  
  11.   
  12.         struct sockaddr_in clientAddr;  
  13.         socklen_t addrLen;  
  14.         int maxfd = listenfd;  
  15.         fd_set rset;  
  16.         fd_set allset;  
  17.         FD_ZERO(&rset);  
  18.         FD_ZERO(&allset);  
  19.         FD_SET(listenfd, &allset);  
  20.   
  21.         //用于保存已连接的客户端套接字  
  22.         int client[FD_SETSIZE];  
  23.         for (int i = 0; i < FD_SETSIZE; ++i)  
  24.             client[i] = -1;  
  25.         int maxi = 0;   //用于保存最大的不空闲的位置, 用于select返回之后遍历数组  
  26.   
  27.         int count = 0;  
  28.         while (true)  
  29.         {  
  30.             rset = allset;  
  31.             int nReady = select(maxfd+1, &rset, NULL, NULL, NULL);  
  32.             if (nReady == -1)  
  33.             {  
  34.                 if (errno == EINTR)  
  35.                     continue;  
  36.                 err_exit("select error");  
  37.             }  
  38.   
  39.             if (FD_ISSET(listenfd, &rset))  
  40.             {  
  41.                 addrLen = sizeof(clientAddr);  
  42.                 int connfd = accept(listenfd, (struct sockaddr *)&clientAddr, &addrLen);  
  43.                 if (connfd == -1)  
  44.                     err_exit("accept error");  
  45.   
  46.                 int i;  
  47.                 for (i = 0; i < FD_SETSIZE; ++i)  
  48.                 {  
  49.                     if (client[i] < 0)  
  50.                     {  
  51.                         client[i] = connfd;  
  52.                         if (i > maxi)  
  53.                             maxi = i;  
  54.                         break;  
  55.                     }  
  56.                 }  
  57.                 if (i == FD_SETSIZE)  
  58.                 {  
  59.                     cerr << "too many clients" << endl;  
  60.                     exit(EXIT_FAILURE);  
  61.                 }  
  62.                 //打印客户IP地址与端口号  
  63.                 cout << "Client information: " << inet_ntoa(clientAddr.sin_addr)  
  64.                      << ", " << ntohs(clientAddr.sin_port) << endl;  
  65.                 cout << "count = " << ++count << endl;  
  66.                 //将连接套接口放入allset, 并更新maxfd  
  67.                 FD_SET(connfd, &allset);  
  68.                 if (connfd > maxfd)  
  69.                     maxfd = connfd;  
  70.   
  71.                 if (--nReady <= 0)  
  72.                     continue;  
  73.             }  
  74.   
  75.             /**如果是已连接套接口发生了可读事件**/  
  76.             for (int i = 0; i <= maxi; ++i)  
  77.                 if ((client[i] != -1) && FD_ISSET(client[i], &rset))  
  78.                 {  
  79.                     char buf[512] = {0};  
  80.                     int readBytes = readline(client[i], buf, sizeof(buf));  
  81.                     if (readBytes == -1)  
  82.                         err_exit("readline error");  
  83.                     else if (readBytes == 0)  
  84.                     {  
  85.                         cerr << "client connect closed..." << endl;  
  86.                         FD_CLR(client[i], &allset);  
  87.                         close(client[i]);  
  88.                         client[i] = -1;  
  89.                     }  
  90.                     cout << buf;  
  91.                     if (writen(client[i], buf, readBytes) == -1)  
  92.                         err_exit("writen error");  
  93.   
  94.                     if (--nReady <= 0)  
  95.                         break;  
  96.                 }  
  97.         }  
  98.     }  
  99.     catch (const SocketException &e)  
  100.     {  
  101.         cerr << e.what() << endl;  
  102.         err_exit("TCPServer error");  
  103.     }  
  104. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**高并发测试端代码: contest完整源代码如下**/  
  2. int main()  
  3. {  
  4.     //最好不要修改: 不然会产生段溢出(stack-overflow)  
  5. //    struct rlimit rlim;  
  6. //    rlim.rlim_cur = 2048;  
  7. //    rlim.rlim_max = 2048;  
  8. //    if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)  
  9. //        err_exit("setrlimit error");  
  10.   
  11.     int count = 0;  
  12.     while (true)  
  13.     {  
  14.         int sockfd = socket(AF_INET, SOCK_STREAM, 0);  
  15.         if (sockfd == -1)  
  16.         {  
  17.             sleep(5);  
  18.             err_exit("socket error");  
  19.         }  
  20.   
  21.         struct sockaddr_in serverAddr;  
  22.         serverAddr.sin_family = AF_INET;  
  23.         serverAddr.sin_port = htons(8001);  
  24.         serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
  25.         int ret = connect_timeout(sockfd, &serverAddr, 5);  
  26.         if (ret == -1 && errno == ETIMEDOUT)  
  27.         {  
  28.             cerr << "timeout..." << endl;  
  29.             err_exit("connect_timeout error");  
  30.         }  
  31.         else if (ret == -1)  
  32.             err_exit("connect_timeout error");  
  33.   
  34.         //获取并打印对端信息  
  35.         struct sockaddr_in peerAddr;  
  36.         socklen_t peerLen = sizeof(peerAddr);  
  37.         if (getpeername(sockfd, (struct sockaddr *)&peerAddr, &peerLen) == -1)  
  38.             err_exit("getpeername");  
  39.         cout << "Server information: " << inet_ntoa(peerAddr.sin_addr)  
  40.              << ", " << ntohs(peerAddr.sin_port) << endl;  
  41.         cout << "count = " << ++count << endl;  
  42.     }  
  43. }  

Server端运行截图如图所示:


解析:对于客户端,最多只能开启1021个连接套接字,因为总共是在Linux中最多可以打开1024个文件描述如,其中还得除去0,1,2。而服务器端只能accept 返回1020个已连接套接字,因为除了0,1,2之外还有一个监听套接字listenfd,客户端某一个套接字(不一定是最后一个)虽然已经建立了连接,在已完成连接队列中,但accept返回时达到最大描述符限制,返回错误,打印提示信息。

 

client在socket()返回-1是调用sleep(5)解析

   当客户端调用socket准备创建第1022个套接字时,如上所示也会提示错误,此时socket函数返回-1出错,如果没有睡眠4s后再退出进程会有什么问题呢?如果直接退出进程,会将客户端所打开的所有套接字关闭掉,即向服务器端发送了很多FIN段,而此时也许服务器端还一直在accept ,即还在从已连接队列中返回已连接套接字,此时服务器端除了关心监听套接字的可读事件,也开始关心前面已建立连接的套接字的可读事件,read 返回0,所以会有很多 client close 字段参杂在条目的输出中,还有个问题就是,因为read 返回0,服务器端会将自身的已连接套接字关闭掉,那么也许刚才说的客户端某一个连接会被accept 返回,即测试不出服务器端真正的并发容量;

 

poll调用

poll没有select第二个限制, 即FD_SETSIZE的限制, 但是第一个限制暂时还是无法避免的;

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <poll.h>  
  2. int poll(struct pollfd *fds, nfds_t nfds, int timeout);  

   参数nfds: 需要检测事件的个数, 结构体数组大小(也可表示为文件描述符个数)(The caller should specify the number of items in the fds array in nfds.)

   参数timeout: 超时时间(单位milliseconds, 毫秒),若为-1,表示永不超时。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //pollfd结构体  
  2. struct pollfd  
  3. {  
  4.     int   fd;         /* file descriptor */  
  5.     short events;     /* requested events: 请求的事件 */  
  6.     short revents;    /* returned events :  返回的事件*/  
  7. };  

events与revents取值(前3个最常用):


返回值:

   成功: 返回一个正整数(this is the number of  structures  which  have nonzero  revents  

fields  (in  other  words,  those  descriptors  with  events  or errors reported).  

   超时:  返回0(A value of 0 indicates that the call timed out and no file  descriptors  

were ready)  

   失败: 返回-1(On error, -1 is returned, and errno is set appropriately.)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**poll-Server示例(将前面的select-server改造如下, 其没有了FD_SETSIZE的限制, 关于第一个限制可以使用前文中的方法更改)(client端与测试端代码如前)**/  
  2. const int SETSIZE = 2048;  
  3. int main()  
  4. {  
  5.     signal(SIGPIPE, sigHandlerForSigPipe);  
  6.     try  
  7.     {  
  8.         TCPServer server(8001);  
  9.         //用于保存已连接的客户端套接字  
  10.         struct pollfd client[SETSIZE];  
  11.         //将client置空  
  12.         for (int i = 0; i < SETSIZE; ++i)  
  13.             client[i].fd = -1;  
  14.         int maxi = 0;   //用于保存最大的已占用位置  
  15.         int count = 0;  
  16.         client[0].fd = server.getfd();  
  17.         client[0].events = POLLIN;  
  18.         while (true)  
  19.         {  
  20.             int nReady = poll(client, maxi+1, -1);  
  21.             if (nReady == -1)  
  22.             {  
  23.                 if (errno == EINTR)  
  24.                     continue;  
  25.                 err_exit("poll error");  
  26.             }  
  27.   
  28.             //如果是监听套接口发生了可读事件  
  29.             if (client[0].revents & POLLIN)  
  30.             {  
  31.                 int connfd = accept(server.getfd(), NULL, NULL);  
  32.                 if (connfd == -1)  
  33.                     err_exit("accept error");  
  34.   
  35.                 bool flags = false;  
  36.                 //略过client[0].fd(listenfd), 从1开始检测  
  37.                 for (int i = 1; i < SETSIZE; ++i)  
  38.                 {  
  39.                     if (client[i].fd == -1)  
  40.                     {  
  41.                         client[i].fd = connfd;  
  42.                         client[i].events = POLLIN;  
  43.                         flags = true;  
  44.                         if (i > maxi)  
  45.                             maxi = i;  
  46.                         break;  
  47.                     }  
  48.                 }  
  49.                 //未找到一个合适的位置  
  50.                 if (!flags)  
  51.                 {  
  52.                     cerr << "too many clients" << endl;  
  53.                     exit(EXIT_FAILURE);  
  54.                 }  
  55.                 cout << "count = " << ++count << endl;  
  56.                 if (--nReady <= 0)  
  57.                     continue;  
  58.             }  
  59.   
  60.             /**如果是已连接套接口发生了可读事件**/  
  61.             for (int i = 1; i <= maxi; ++i)  
  62.                 if (client[i].revents & POLLIN)  
  63.                 {  
  64.                     char buf[512] = {0};  
  65.                     int readBytes = readline(client[i].fd, buf, sizeof(buf));  
  66.                     if (readBytes == -1)  
  67.                         err_exit("readline error");  
  68.                     else if (readBytes == 0)  
  69.                     {  
  70.                         cerr << "client connect closed..." << endl;  
  71.                         close(client[i].fd);  
  72.                         client[i].fd = -1;  
  73.                     }  
  74.                     cout << buf;  
  75.                     if (writen(client[i].fd, buf, readBytes) == -1)  
  76.                         err_exit("writen error");  
  77.                     if (--nReady <= 0)  
  78.                         break;  
  79.                 }  
  80.         }  
  81.     }  
  82.     catch (const SocketException &e)  
  83.     {  
  84.         cerr << e.what() << endl;  
  85.         err_exit("TCPServer error");  
  86.     }  
  87. }  

附-getrlimit和setrlimit函数

每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <sys/time.h>  
  2. #include <sys/resource.h>  
  3. int getrlimit(int resource, struct rlimit *rlim);  
  4. int setrlimit(int resource, const struct rlimit *rlim);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //rlimit结构体  
  2. struct rlimit  
  3. {  
  4.     rlim_t rlim_cur;  /* Soft limit */  
  5.     rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */  
  6. };  

软限制是一个建议性的, 最好不要超越的限制, 如果超越的话, 系统可能向进程发送信号以终止其运行.

而硬限制一般是软限制的上限;

resource可用值

RLIMIT_AS

进程可用的最大虚拟内存空间长度,包括堆栈、全局变量、动态内存

RLIMIT_CORE

内核生成的core文件的最大大小

RLIMIT_CPU

所用的全部cpu时间,以秒计算

RLIMIT_DATA

进程数据段(初始化DATA段, 未初始化BSS段和堆)限制(以B为单位)

RLIMIT_FSIZE

文件大小限制

RLIMIT_SIGPENDING

用户能够挂起的信号数量限制

RLIMIT_NOFILE

打开文件的最大数目

RLIMIT_NPROC

用户能够创建的进程数限制

RLIMIT_STACK

进程栈内存限制, 超过会产生SIGSEGV信号

进程的资源限制通常是在系统初启时由0#进程建立的,在更改资源限制时,须遵循下列三条规则:

  1.任何一个进程都可将一个软限制更改为小于或等于其硬限制。

  2.任何一个进程都可降低其硬限制值,但它必须大于或等于其软限制值。这种降低,对普通用户而言是不可逆反的。

  3.只有超级用户可以提高硬限制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值