• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            loop_in_codes

            低調(diào)做技術(shù)__歡迎移步我的獨(dú)立博客 codemaro.com 微博 kevinlynx

            select真的有限制嗎

            在剛開(kāi)始學(xué)習(xí)網(wǎng)絡(luò)編程時(shí),似乎莫名其妙地就會(huì)被某人/某資料告訴select函數(shù)是有fd(file descriptor)數(shù)量限制的。在最近的一次記憶里還有個(gè)人笑說(shuō)select只支持64個(gè)fd。我甚至還寫(xiě)過(guò)一篇不負(fù)責(zé)任甚至錯(cuò)誤的博客(突破select的FD_SETSIZE限制)。有人說(shuō),直接重新定義FD_SETSIZE就可以突破這個(gè)select的限制,也有人說(shuō)除了重定義這個(gè)宏之外還的重新編譯內(nèi)核。

            事實(shí)具體是怎樣的?實(shí)際上,造成這些混亂的原因恰好是不同平臺(tái)對(duì)select的實(shí)現(xiàn)不一樣。

            Windows的實(shí)現(xiàn)

            MSDN.aspx)上對(duì)select的說(shuō)明:

            int select(
              _In_     int nfds,
              _Inout_  fd_set *readfds,
              _Inout_  fd_set *writefds,
              _Inout_  fd_set *exceptfds,
              _In_     const struct timeval *timeout
            );
            
            nfds [in] Ignored. The nfds parameter is included only for compatibility with Berkeley sockets.
            

            第一個(gè)參數(shù)MSDN只說(shuō)沒(méi)有使用,其存在僅僅是為了保持與Berkeley Socket的兼容。

            The variable FD_SETSIZE determines the maximum number of descriptors in a set. (The default value of FD_SETSIZE is 64, which can be modified by defining FD_SETSIZE to another value before including Winsock2.h.) Internally, socket handles in an fd_set structure are not represented as bit flags as in Berkeley Unix.

            Windows上select的實(shí)現(xiàn)不同于Berkeley Unix,后者使用位標(biāo)志來(lái)表示socket

            在MSDN的評(píng)論中有人提到:

            Unlike the Linux versions of these macros which use a single calculation to set/check the fd, the Winsock versions use a loop which goes through the entire set of fds each time you call FD_SET or FD_ISSET (check out winsock2.h and you’ll see). So you might want to consider an alternative if you have thousands of sockets!

            不同于Linux下處理fd_set的那些宏(FD_CLR/FD_SET之類),Windows上這些宏的實(shí)現(xiàn)都使用了一個(gè)循環(huán),看看這些宏的大致實(shí)現(xiàn)(Winsock2.h):

            #define FD_SET(fd, set) do { \
                u_int __i; \
                for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
                    if (((fd_set FAR *)(set))->fd_array[__i] == (fd)) { \
                        break; \
                    } \
                } \
                if (__i == ((fd_set FAR *)(set))->fd_count) { \
                    if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
                        ((fd_set FAR *)(set))->fd_array[__i] = (fd); \
                        ((fd_set FAR *)(set))->fd_count++; \
                    } \
                } \
            } while(0)
            

            看下Winsock2.h中關(guān)于fd_set的定義:

            typedef struct fd_set {
                u_int fd_count;
                SOCKET fd_array[FD_SETSIZE];
            } fd_set;
            

            再看一篇更重要的MSDN Maximum Number of Sockets Supported.aspx):

            The Microsoft Winsock provider limits the maximum number of sockets supported only by available memory on the local computer. The maximum number of sockets that a Windows Sockets application can use is not affected by the manifest constant FD_SETSIZE. If an application is designed to be capable of working with more than 64 sockets using the select and WSAPoll functions, the implementor should define the manifest FD_SETSIZE in every source file before including the Winsock2.h header file.

            Windows上select支持的socket數(shù)量并不受宏FD_SETSIZE的影響,而僅僅受內(nèi)存的影響。如果應(yīng)用程序想使用超過(guò)FD_SETSIZE的socket,僅需要重新定義FD_SETSIZE即可。

            實(shí)際上稍微想想就可以明白,既然fd_set里面已經(jīng)有一個(gè)socket的數(shù)量計(jì)數(shù),那么select的實(shí)現(xiàn)完全可以使用這個(gè)計(jì)數(shù),而不是FD_SETSIZE這個(gè)宏。那么結(jié)論是,select至少在Windows上并沒(méi)有socket支持?jǐn)?shù)量的限制。當(dāng)然效率問(wèn)題這里不談。

            這看起來(lái)推翻了我們一直以來(lái)沒(méi)有深究的一個(gè)事實(shí)。

            Linux的實(shí)現(xiàn)

            在上面提到的MSDN中,其實(shí)已經(jīng)提到了Windows與Berkeley Unix實(shí)現(xiàn)的不同。在select的API文檔中也看到了第一個(gè)參數(shù)并沒(méi)有說(shuō)明其作用。看下Linux的man

            nfds is the highest-numbered file descriptor in any of the three sets, plus 1.

            第一個(gè)參數(shù)簡(jiǎn)單來(lái)說(shuō)就是最大描述符+1。

            An fd_set is a fixed size buffer. Executing FD_CLR() or FD_SET() with a value of fd that is negative or is equal to or larger than FD_SETSIZE will result in undefined behavior.

            明確說(shuō)了,如果調(diào)用FD_SET之類的宏fd超過(guò)了FD_SETSIZE將導(dǎo)致undefined behavior。也有人專門做了測(cè)試:select system call limitation in Linux。也有現(xiàn)實(shí)遇到的問(wèn)題:socket file descriptor (1063) is larger than FD_SETSIZE (1024), you probably need to rebuild Apache with a larger FD_SETSIZE

            看起來(lái)在Linux上使用select確實(shí)有FD_SETSIZE的限制。有必要看下相關(guān)的實(shí)現(xiàn) fd_set.h

            typedef __uint32_t      __fd_mask;
            
            /* 32 = 2 ^ 5 */
            #define __NFDBITS       (32)
            #define __NFDSHIFT      (5)
            #define __NFDMASK       (__NFDBITS - 1)
            
            /*
             * Select uses bit fields of file descriptors.  These macros manipulate
             * such bit fields.  Note: FD_SETSIZE may be defined by the user.
             */
            
            #ifndef FD_SETSIZE
            #define FD_SETSIZE      256
            #endif
            
            #define __NFD_SIZE      (((FD_SETSIZE) + (__NFDBITS - 1)) / __NFDBITS)
            
            typedef struct fd_set {
                __fd_mask       fds_bits[__NFD_SIZE];
            } fd_set;
            

            在這份實(shí)現(xiàn)中不同于Windows實(shí)現(xiàn),它使用了位來(lái)表示fd。看下FD_SET系列宏的大致實(shí)現(xiàn):

            #define FD_SET(n, p)    \
               ((p)->fds_bits[(unsigned)(n) >> __NFDSHIFT] |= (1 << ((n) & __NFDMASK)))
            

            添加一個(gè)fd到fd_set中也不是Windows的遍歷,而是直接位運(yùn)算。這里也有人對(duì)另一份類似實(shí)現(xiàn)做了剖析:linux的I/O多路轉(zhuǎn)接select的fd_set數(shù)據(jù)結(jié)構(gòu)和相應(yīng)FD_宏的實(shí)現(xiàn)分析。在APUE中也提到fd_set

            這種數(shù)據(jù)類型(fd_set)為每一可能的描述符保持了一位。

            既然fd_set中不包含其保存了多少個(gè)fd的計(jì)數(shù),那么select的實(shí)現(xiàn)里要知道自己要處理多少個(gè)fd,那只能使用FD_SETSIZE宏去做判定,但Linux的實(shí)現(xiàn)選用了更好的方式,即通過(guò)第一個(gè)參數(shù)讓?xiě)?yīng)用層告訴select需要處理的最大fd(這里不是數(shù)量)。那么其實(shí)現(xiàn)大概為:

            for (int i = 0; i < nfds; ++i) {
                if (FD_ISSET...
                   ...
            }
            

            如此看來(lái),Linux的select實(shí)現(xiàn)則是受限于FD_SETSIZE的大小。這里也看到,fd_set使用位數(shù)組來(lái)保存fd,那么fd本身作為一個(gè)int數(shù),其值就不能超過(guò)FD_SETSIZE這不僅僅是數(shù)量的限制,還是其取值的限制。實(shí)際上,Linux上fd的取值是保證了小于FD_SETSIZE的(但不是不變的)Is the value of a Linux file descriptor always smaller than the open file limits?

            Each process is further limited via the setrlimit(2) RLIMIT_NOFILE per-process limit on the number of open files. 1024 is a common RLIMIT_NOFILE limit. (It’s very easy to change this limit via /etc/security/limits.conf.)

            fd的取值會(huì)小于RLIMIT_NOFILE,有很多方法可以改變這個(gè)值。這個(gè)值默認(rèn)情況下和FD_SETSIZE應(yīng)該是一樣的。這個(gè)信息告訴我們,Linux下fd的取值應(yīng)該是從0開(kāi)始遞增的(理論上,實(shí)際上還有stdin/stdout/stderr之類的fd)。這才能保證select的那些宏可以工作。

            應(yīng)用層使用

            標(biāo)準(zhǔn)的select用法應(yīng)該大致如下:

            while (true) {
                ...
                select(...)
                for-each socket {
                    if (FD_ISSET(fd, set))
                        ...
                }
            
                ...
            }
            

            即遍歷目前管理的fd,通過(guò)FD_ISSET去判定當(dāng)前fd是否有IO事件。因?yàn)閃indows的實(shí)現(xiàn)FD_ISSET都是一個(gè)循環(huán),所以有了另一種不跨平臺(tái)的用法:

            while (true) {
                ...
                select(. &read_sockets, &write_sockets..)
                for-each read_socket {
                    use fd.fd_array[i)
                }
                ...
            }
            

            總結(jié)

            • Windows上select沒(méi)有fd數(shù)量的限制,但因?yàn)槭褂昧搜h(huán)來(lái)檢查,所以效率相對(duì)較低
            • Linux上selectFD_SETSIZE的限制,但其相對(duì)效率較高

            posted on 2014-06-01 23:45 Kevin Lynx 閱讀(4946) 評(píng)論(1)  編輯 收藏 引用 所屬分類: network

            評(píng)論

            # re: select真的有限制嗎 2014-06-06 13:23 liyou

            不錯(cuò),樓主做了自己的研究。  回復(fù)  更多評(píng)論   

            色妞色综合久久夜夜| 久久精品桃花综合| 国产精品gz久久久| 久久国产成人精品麻豆| 久久久久久一区国产精品| 伊人久久大香线蕉av不卡 | 国产精品美女久久久久 | 无码国产69精品久久久久网站| 97精品伊人久久大香线蕉app| 久久久久亚洲av成人无码电影 | 久久久噜噜噜久久中文字幕色伊伊| 久久久噜噜噜久久中文福利| 久久伊人色| 国产精品久久久久影院色| 久久成人国产精品免费软件| 精品无码久久久久久国产| 国产精品99久久免费观看| 亚洲人成无码久久电影网站| 老司机国内精品久久久久| 中文字幕乱码久久午夜| 久久久WWW成人免费毛片| 99久久国产综合精品五月天喷水| 天天躁日日躁狠狠久久| 欧美亚洲国产精品久久高清| 久久免费香蕉视频| 久久精品国产只有精品66| 久久精品男人影院| 97精品久久天干天天天按摩| 精品免费久久久久久久| 中文精品久久久久人妻不卡| 久久天天躁狠狠躁夜夜2020老熟妇| 国产精品99久久精品爆乳| 国产精品狼人久久久久影院| 99久久久久| 日本高清无卡码一区二区久久| 国产精品久久久久乳精品爆| 国产精品九九久久免费视频 | 亚洲精品乱码久久久久久久久久久久| 91久久精品国产成人久久| 97精品伊人久久久大香线蕉| 狠狠精品干练久久久无码中文字幕|