简体   繁体   English

当Fd大于1024时,Select和FD_SET的行为

[英]Behavior of Select and FD_SET When Fd is Bigger than 1024

As far as I know, select only supports no more than 1024 sockets. 据我所知,select只支持不超过1024个套接字。 But a process can own 65535 sockets which means most of the socket numbers are bigger than 1024, so I have three questions: 但是一个进程可以拥有65535个套接字,这意味着大多数套接字数大于1024,所以我有三个问题:

Q1. Q1。 What will happen if passing socket numbers bigger than 1024 to FD_SET()? 如果将大于1024的套接字号传递给FD_SET()会发生什么?
Q2. Q2。 What will happen if passing fd_set whose socket numbers are all bigger than 1024 to select()? 如果将套接字号全部大于1024的fd_set传递给select()会发生什么?
Q3. Q3。 On Linux Fedora with kernel 2.6.8, x86 64bit, will exceptions be thrown in Q1 and Q2? 在具有内核2.6.8,x86 64位的Linux Fedora上,Q1和Q2会抛出异常吗?

An fd_set is an array of bits, only manipulated with FD_* macros because C doesn't have a "bit" type. fd_set是一个位数组,仅使用FD_*宏进行操作,因为C没有“位”类型。 (The type is officially opaque, and could be implemented a different way - in fact winsock does implement it differently - but all unix-like OSes use the array of bits.) (该类型是官方不透明的,并且可以以不同的方式实现 - 实际上winsock确实以不同的方式实现它 - 但是所有类似unix的操作系统都使用位数组。)

So this code: 所以这段代码:

fd_set my_fds;
....
FD_SET(1024, &my_fds);

has the same problem as this code: 与此代码有相同的问题:

char my_fds[1024];
....
my_fds[1024] = 1;

assuming FD_SETSIZE is 1024. 假设FD_SETSIZE是1024。

You will be overwriting whatever comes after the fd_set in memory, causing a segfault if you're lucky, more subtle errors if you're not. 你将覆盖内存中fd_set之后发生的任何事情,如果你是幸运的话会导致段错误,如果你不幸,会导致更微妙的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM