在选择系统调用查询 [英] Query on Select System Call

查看:94
本文介绍了在选择系统调用查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择()的定义是:

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);

NFDs的的再presents在所有已知集合加上一个最高的文件描述符。我想知道这是为什么数据需要选择()时FD_SET资料。

nfds represents the highest file descriptor in all given sets plus one. I would like to know why is this data required for select() when the fd_set information is available.

如果该集合中的文件描述符是说,4,8,9,值的 NFDs的的将是10.将选择()箴言报FDS 9,8,7,6,5,4 ?

If the FDs in the set are say, 4, 8, 9 ,the value of nfds would be 10. Would select() moniter fds 9,8,7,6,5,4 ?

推荐答案

美中不足的是,FD_SET是不是一个真正的设置的方式,你在想。在后面的幕后细节是一个的fd_set的实现只是用作位域的整数。换句话说,执行

The catch is that fd_set is not really a "set" in the way you're thinking. The behind-the-scenes detail is that the implementation of an fd_set is just an integer that is used as a bitfield. In other words, executing

fd_set foo;
FD_CLEAR(&foo);
FD_SET(&foo, 3);

设定为foo的十进制值8 - 它设置的第四最低singificant位为1(记住,0是一个有效的描述符)

Sets foo to decimal value 8 - it sets the fourth-least-singificant bit to 1 (remember that 0 is a valid descriptor).

FD_SET(&foo, 3);

等同于

foo |= (1 << 3);

因此​​,为了使选择工作的权利,它需要知道其中的fd_set的位是你关心位。否则就没有办法为它告诉一个零位即是中的集合,但是从零位是不在集合设置为false。

So in order for select to work right, it needs to know which bits of the fd_set are bits that you care about. Otherwise there would be no way for it to tell a zero bit that is "in" the set but set to false from a zero bit that is "not in" the set.

在您的例子中,有4个,8个和9套和n = 10的fd_set是PTED为一组10个条目(FDS 0-9)间$ P $。参赛作品4,8和9是真(监视它们)。参赛作品1,2,3,5,6,7-都是假的(不监视它们)。任何FD值大于9,根本就不是在设定的时间。

In your example, a fd_set with 4, 8, and 9 set and n = 10 is interpreted as "A set with 10 entries (fds 0-9). Entries 4, 8, and 9 are true (monitor them). Entries 1,2,3,5,6,7 are false (don't monitor them). Any fd value greater than 9 is simply not in the set period."

这篇关于在选择系统调用查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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