select()函数的第一个参数到底是什么 [英] What exactly is the first argument of select() function

查看:818
本文介绍了select()函数的第一个参数到底是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解select函数中第一个参数的目的. Wikipedia 将其描述为所有集合中最大的文件描述符,外加1.为什么+1,为什么select需要此信息?

I don't quite understand the purpose of the first argument in the select function. Wikipedia describes it as the maximum file descriptor across all the sets, plus 1 . Why +1 and why does select need this information ?

推荐答案

在* Nix系统中,文件描述符只是系统表的索引,而fd_set结构包含与这些索引相对应的位掩码.将描述符添加到fd_set时,将启用相应的位. select()需要知道最高的描述符值,以便它可以遍历这些位并知道从哪一位停止.

In *Nix systems, file descriptors are just indexes into a system table, and the fd_set structure contains a bitmask that corresponds to those indexes. When a descriptor is added to an fd_set, the corresponding bit is enabled. select() needs to know the highest descriptor value so it can loop through the bits and know which one to stop at.

在Windows上,套接字由内核对象的句柄而不是索引表示. fd_set结构包含一个套接字句柄数组和该数组中套接字数量的计数器.这样,select()可以循环遍历整个数组,这就是为什么select()的第一个参数在Windows上被忽略的原因.

On Windows, sockets are represented by handles to kernel objects, not by indexes. The fd_set structure contains an array of socket handles and a counter of the number of sockets in the array. This way, select() can just loop through the array, and is thus why the first parameter of select() is ignored on Windows.

这篇关于select()函数的第一个参数到底是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆