select()

<PAGE546>

#include <sys/select.h >

int select(int maxfdp1, fd_set *restrictreadfds,

fd_set *restrict writefds, fd_set *restrict exceptfds,

struct timeval *restrict tvptr)

select():

do I/O multiplexing (用于检测多个文件句柄的变化)

tvptr:

       struct timeval {

long tv_sec;

longtv_usec;

}

       tvptr==NULL

              Wait forever. This infinite wait can be interruptedifwe catch a signal. Return is made when one of the specified descriptors isready or when a signal is caught. Ifa signal is caught, selectreturns –1 witherrnoset to EINTR. (永远等待,直到文件描述符就绪或信号被捕获,selcet处于堵塞状态)

       tvptr->tv_sec==0 && typtr->tv_usec==0

              Don't wait at all.All the specified descriptors are tested, and return is made immediately. Thisis away to poll the system to find out the status of multiple descriptors,without blocking in the select function. (用于测试文件描述符的状态,select并不堵塞)

tvptr->tv_sec!=0|| tv->tv_usec!=0

       Wait the specified number of seconds andmicroseconds. Return is made when one of the specified descriptors is ready orwhen the timeout value expires. can also be interrupted by a caught signal(等待指定的时间,当文件描述符就绪或者时间超时,则返回)

readfdswritefds exceptfds:

       These three sets specify which descriptors we're interested inand for which conditions.

can benull pointers if we're not interested in that condition (fd_set 的变量)

maxfdp1:

       stands for "maximum file descriptor plus1.

       calculate the highest descriptor that we're interested in,considering all three of the descriptor sets, add 1. (输入最大的文件描述符+1,内核会根据这个变量遍历查找fd_set的文件描述符,看是否有就绪状态的文件描述符,返回给user app)

return:

       count of ready descriptors, 0 on timeout, –1 on error

remarks:

       A descriptor in the read set (readfds) is consideredready if a read from that descriptor won't block

Adescriptor in the write set (writefds) is considered ready if a writeto thatdescriptor won't block

Adescriptor in the exception set (exceptfds) is considered ready if an exceptioncondition is pendingon that descriptor.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值