是否有必要在 select 系统调用之间重置 fd_set? [英] Is it necessary to reset the fd_set between select system call?

查看:33
本文介绍了是否有必要在 select 系统调用之间重置 fd_set?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Unix 中使用 select 函数时遇到问题.

我有一个等待连接的服务器.首先,我使用 FD_SET(listener, readfds) 将监听套接字文件描述符 listener 添加到 fd_set readfds ,然后我在 中使用它选择().

当我获得连接时,我调用 accept() 并在 select 中设置 readfds 并使用接受的文件描述符并开始从连接接收数据.但是,当我在 strace 中检查代码时,当 select() 第二次执行时,选择不会在 readfds 中显示侦听器.

在再次调用 select() 之前,我是否需要使用 FD_SET(listener, readfds) 再次设置侦听器文件描述符?

谢谢.

解决方案

是(需要在 select() 系统调用之间重置 fd_set ).

这很麻烦,但它们充当输入/输出参数;它们由系统调用读取和修改.当 select() 返回时,值已全部修改以反映准备好的文件描述符集.因此,每次调用 select() 之前,您都必须(重新)初始化 fd_set 值.

I am facing a problem using the select function in Unix.

I have a server that waits for for a connection. First I add the listening socket file descriptor listener to the fd_set readfds using FD_SET(listener, readfds) and then I use that in select().

When I get a connection, I call accept() and set the readfds in select with the accepted file descriptor and start receiving the data from connection. However, when I check the code in strace, The select doesn't show the listener in the readfds while select() is executing a second time.

Do I need to set the listener file descriptor again using FD_SET(listener, readfds) before calling select() again?

Thanks.

解决方案

Yes (it is necessary to reset the fd_set between select() system calls).

It is a nuisance, but they act as input/output parameters; they are read by and modified by the system call. When select() returns, the values have all been modified to reflect the set of file descriptors ready. So, every time before you call select(), you have to (re)initialize the fd_set values.

这篇关于是否有必要在 select 系统调用之间重置 fd_set?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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