在剛開(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上
select
有FD_SETSIZE
的限制,但其相對(duì)效率較高