select()

From <<Linux programming unleash>>

int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);

FD_ZERO(fd_set *set);


The first parameter is the number of file descriptors in the file descriptor sets (so the kernel
doesn’t have to waste time checking a bunch of unused bits). The second, third, and
fourth parameters are pointers to file descriptor sets (one bit per possible file descriptor)
that indicate which file descriptors you would like to be able to read, write, or receive

exception notifications on, respectively. The last parameter is a timeout value. All but the

first parameter may be null. On return the file descriptor sets will be modified to indicate
which descriptors are ready for immediate I/O operations. The timeout will also be modified
on return, although that is not the case on most systems other than Linux. The return
value itself will indicate a count of how many descriptors are included in the descriptor
sets. If it is zero, that indicates a timeout. If the return value is -1, errno will be set to
indicate the error (which may include EINTR if a signal was caught).
The macros FD_ZERO(), FD_SET(), FD_CLEAR, and FD_ISSET() help manipulate file
descriptor sets by erasing the whole set, setting the bit corresponding to a file descriptor,
clearing the bit, or querying the bit. All but FD_ZERO() take a file descriptor as the first

parameter. The remaining parameter for each is a pointer to a file descriptor set.


Application:

1) Call macro FD_ZERO clear fd_set; 

2) Call macro FD_SET to add specified fd into fd_set;

3) Call function select to test all fds in teh fd_set. (note: set timeout)

4) Call macro FD_ISSET to test if the fd is still in fd_set after calling select。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值