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

select的限制

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

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

/**示例: getrlimit/setrlimit获取/设置进程打开文件数目**/
int main()
{
    struct rlimit rl;
    if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
        err_exit("getrlimit error");
    cout << "Soft limit: " << rl.rlim_cur << endl;
    cout << "Hard limit: " << rl.rlim_max << endl;
    cout << "------------------------->"  << endl;

    rl.rlim_cur = 2048;
    rl.rlim_max = 2048;
    if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
        err_exit("setrlimit error");

    if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
        err_exit("getrlimit error");
    cout << "Soft limit: " << rl.rlim_cur << endl;
    cout << "Hard limit: " << rl.rlim_max << endl;
}

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

/**测试: 测试服务器端最多能够建立多少个连接
server端完整源代码如下(注意使用的是<Socket编程实践(7)中的TCPServer类实现>):
**/
int main()
{
    signal(SIGPIPE, sigHandlerForSigPipe);
    try
    {
        TCPServer server(8001);
        int listenfd = server.getfd();

        struct sockaddr_in clientAddr;
        socklen_t addrLen;
        int maxfd = listenfd;
        fd_set rset;
        fd_set allset;
        FD_ZERO(&rset);
        FD_ZERO(&allset);
        FD_SET(listenfd, &allset);

        //用于保存已连接的客户端套接字
        int client[FD_SETSIZE];
        for (int i = 0; i < FD_SETSIZE; ++i)
            client[i] = -1;
        int maxi = 0;   //用于保存最大的不空闲的位置, 用于select返回之后遍历数组

        int count = 0;
        while (true)
        {
            rset = allset;
            int nReady = select(maxfd+1, &rset, NULL, NULL, NULL);
            if (nReady == -1)
            {
                if (errno == EINTR)
                    continue;
                err_exit("select error");
            }

            if (FD_ISSET(listenfd, &rset))
            {
                addrLen = sizeof(clientAddr);
                int connfd = accept(listenfd, (struct sockaddr *)&clientAddr, &addrLen);
                if (connfd == -1)
                    err_exit("accept error");

                int i;
                for (i = 0; i < FD_SETSIZE; ++i)
                {
                    if (client[i] < 0)
                    {
                        client[i] = connfd;
                        if (i > maxi)
                            maxi = i;
                        break;
                    }
                }
                if (i == FD_SETSIZE)
                {
                    cerr << "too many clients" << endl;
                    exit(EXIT_FAILURE);
                }
                //打印客户IP地址与端口号
                cout << "Client information: " << inet_ntoa(clientAddr.sin_addr)
                     << ", " << ntohs(clientAddr.sin_port) << endl;
                cout << "count = " << ++count << endl;
                //将连接套接口放入allset, 并更新maxfd
                FD_SET(connfd, &allset);
                if (connfd > maxfd)
                    maxfd = connfd;

                if (--nReady <= 0)
                    continue;
            }

            /**如果是已连接套接口发生了可读事件**/
            for (int i = 0; i <= maxi; ++i)
                if ((client[i] != -1) && FD_ISSET(client[i], &rset))
                {
                    char buf[512] = {0};
                    int readBytes = readline(client[i], buf, sizeof(buf));
                    if (readBytes == -1)
                        err_exit("readline error");
                    else if (readBytes == 0)
                    {
                        cerr << "client connect closed..." << endl;
                        FD_CLR(client[i], &allset);
                        close(client[i]);
                        client[i] = -1;
                    }
                    cout << buf;
                    if (writen(client[i], buf, readBytes) == -1)
                        err_exit("writen error");

                    if (--nReady <= 0)
                        break;
                }
        }
    }
    catch (const SocketException &e)
    {
        cerr << e.what() << endl;
        err_exit("TCPServer error");
    }
}

/**高并发测试端代码: contest完整源代码如下**/
int main()
{
    //最好不要修改: 不然会产生段溢出(stack-overflow)
//    struct rlimit rlim;
//    rlim.rlim_cur = 2048;
//    rlim.rlim_max = 2048;
//    if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
//        err_exit("setrlimit error");

    int count = 0;
    while (true)
    {
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd == -1)
        {
            sleep(5);
            err_exit("socket error");
        }

        struct sockaddr_in serverAddr;
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_port = htons(8001);
        serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        int ret = connect_timeout(sockfd, &serverAddr, 5);
        if (ret == -1 && errno == ETIMEDOUT)
        {
            cerr << "timeout..." << endl;
            err_exit("connect_timeout error");
        }
        else if (ret == -1)
            err_exit("connect_timeout error");

        //获取并打印对端信息
        struct sockaddr_in peerAddr;
        socklen_t peerLen = sizeof(peerAddr);
        if (getpeername(sockfd, (struct sockaddr *)&peerAddr, &peerLen) == -1)
            err_exit("getpeername");
        cout << "Server information: " << inet_ntoa(peerAddr.sin_addr)
             << ", " << ntohs(peerAddr.sin_port) << endl;
        cout << "count = " << ++count << endl;
    }
}

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的限制, 但是第一个限制暂时还是无法避免的;

#include <poll.h>
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,表示永不超时。

//pollfd结构体
struct pollfd
{
    int   fd;         /* file descriptor */
    short events;     /* requested events: 请求的事件 */
    short revents;    /* returned events :  返回的事件*/
};

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.)

/**poll-Server示例(将前面的select-server改造如下, 其没有了FD_SETSIZE的限制, 关于第一个限制可以使用前文中的方法更改)(client端与测试端代码如前)**/
const int SETSIZE = 2048;
int main()
{
    signal(SIGPIPE, sigHandlerForSigPipe);
    try
    {
        TCPServer server(8001);
        //用于保存已连接的客户端套接字
        struct pollfd client[SETSIZE];
        //将client置空
        for (int i = 0; i < SETSIZE; ++i)
            client[i].fd = -1;
        int maxi = 0;   //用于保存最大的已占用位置
        int count = 0;
        client[0].fd = server.getfd();
        client[0].events = POLLIN;
        while (true)
        {
            int nReady = poll(client, maxi+1, -1);
            if (nReady == -1)
            {
                if (errno == EINTR)
                    continue;
                err_exit("poll error");
            }

            //如果是监听套接口发生了可读事件
            if (client[0].revents & POLLIN)
            {
                int connfd = accept(server.getfd(), NULL, NULL);
                if (connfd == -1)
                    err_exit("accept error");

                bool flags = false;
                //略过client[0].fd(listenfd), 从1开始检测
                for (int i = 1; i < SETSIZE; ++i)
                {
                    if (client[i].fd == -1)
                    {
                        client[i].fd = connfd;
                        client[i].events = POLLIN;
                        flags = true;
                        if (i > maxi)
                            maxi = i;
                        break;
                    }
                }
                //未找到一个合适的位置
                if (!flags)
                {
                    cerr << "too many clients" << endl;
                    exit(EXIT_FAILURE);
                }
                cout << "count = " << ++count << endl;
                if (--nReady <= 0)
                    continue;
            }

            /**如果是已连接套接口发生了可读事件**/
            for (int i = 1; i <= maxi; ++i)
                if (client[i].revents & POLLIN)
                {
                    char buf[512] = {0};
                    int readBytes = readline(client[i].fd, buf, sizeof(buf));
                    if (readBytes == -1)
                        err_exit("readline error");
                    else if (readBytes == 0)
                    {
                        cerr << "client connect closed..." << endl;
                        close(client[i].fd);
                        client[i].fd = -1;
                    }
                    cout << buf;
                    if (writen(client[i].fd, buf, readBytes) == -1)
                        err_exit("writen error");
                    if (--nReady <= 0)
                        break;
                }
        }
    }
    catch (const SocketException &e)
    {
        cerr << e.what() << endl;
        err_exit("TCPServer error");
    }
}

附-getrlimit和setrlimit函数

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

#include <sys/time.h>
#include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
//rlimit结构体
struct rlimit
{
    rlim_t rlim_cur;  /* Soft limit */
    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
};

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

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

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.只有超级用户可以提高硬限制。

原文地址:https://www.cnblogs.com/itrena/p/5926933.html