linux socket编程---select

一、函数原型

 DESCRIPTION
       select() and  pselect()  allow  a  program  to  monitor  multiple  file
       descriptors,  waiting  until one or more of the file descriptors become
       "ready" for some class of I/O operation (e.g., input possible).  A file
       descriptor  is  considered  ready if it is possible to perform a corre‐
       sponding I/O operation (e.g., read(2) without  blocking,  or  a  suffi‐
       ciently small write(2)).
       
 int select(int nfds,					//需要检查的号码最高的文件描述符加 1 
 		    fd_set *readfds, 			//由 select()监视的读文件描述符集合
 		    fd_set *writefds,			//由 select()监视的写文件描述符集合 
            fd_set *exceptfds,			//由 select()监视的异常处理文件描述符集合 
            struct timeval *timeout);	//超时设定

  selecth和pselect允许程序监控一个或多个文件的状态变化,以下分别介绍每个参数的意义。

二、struct fd_set

  在linux下任何设备、管道、FIFO等都是文件形式,这里也包括socket。所以struct fd_set实际上就是一个文件描述符的集合。
  在实现上通过32bits实现对不同fd的状态记录。

/* We will use a 32-bit bitsets to represent the set of descriptors.  How
 * many uint32_t's do we need to span all descriptors?
 */
struct fd_set_s
{
  uint32_t arr[__SELECT_NUINT32];
};

  接下来要介绍几个接口函数:

  • FD_ZERO(fd_set *);清空集合
  • FD_SET(int, fd_set *);将一个给定的文件描述符加入集合之中
  • FD_CLR(int, fd_set*);将一个给定的文件描述符从集合中删除
  • FD_ISSET(int ,fd_set* );检查集合中指定的文件描述符是否可以读写。

三、struct timeval

  struct timeval 是一个常用的结构,用来代表时间值,有两个成员,一个是秒数,另一个是毫秒。

/* struct timeval represents time as seconds plus microseconds */
struct timeval
{
  time_t tv_sec;         /* Seconds */
  long tv_usec;          /* Microseconds */
};

四、举个例子

EXAMPLE:
       #include <stdio.h>
       #include <stdlib.h>
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int
       main(void)
       {
           fd_set rfds;
           struct timeval tv;
           int retval;

           /* Watch stdin (fd 0) to see when it has input. */
           FD_ZERO(&rfds);
           FD_SET(0, &rfds);

           /* Wait up to five seconds. */
           tv.tv_sec = 5;
           tv.tv_usec = 0;

           retval = select(1, &rfds, NULL, NULL, &tv);
           /* Don't rely on the value of tv now! */

           if (retval == -1)
               perror("select()");
           else if (retval)
               printf("Data is available now.\n");
               /* FD_ISSET(0, &rfds) will be true. */
           else
               printf("No data within five seconds.\n");

           exit(EXIT_SUCCESS);
       }

  这个例子是通过终端> man select拿到的,就以这个例子来做讲解。

retval = select(1, &rfds, NULL, NULL, &tv);
  • int nfds:

  是一个整数值,是指集合中所有文件描述符的范围,即所有文件描述符的最大值加1。

  • fd_set* readfds:

  是指向fd_set结构的指针,这个集合中应该包括可读文件描述符,是否可以从这些文件中读取数据了。
如果这个集合中有一个文件可读,select就会返回一个大于0的值,表示有文件可读;
如果没有可读的文件,则根据timeout参数再判断是否超时,若超出timeout的时间,select返回0;
如果发生错误,则返回负值。
如果传入的readfds的值是NULL值,则表示不关心任何文件的读变化。

  • d_set* writefds:

  是指向fd_set结构的指针,这个集合中应该包括可写文件描述符,是否可以向这些文件中写入数据了。
如果这个集合中有一个文件可写,select就会返回一个大于0的值,表示有文件可写;
如果没有可写的文件,则根据timeout参数再判断是否超时,若超出timeout的时间,select返回0;
如果发生错误,则返回负值。
如果传入的writefds的值是NULL值,则表示不关心任何文件的读变化。

  • fd_set * errorfds:

用来监视文件错误异常,和以上的两个指针是相同用法,这里不再赘述。

  • struct timeval* timeout:

  是设置select的超时时间
NULL,即不传入时间结构,就是将select置于阻塞状态,一定等到监视文件描述符集合中某个文件描述符发生变化为止;
0秒0毫秒,(selcet变为非阻塞函数),不管文件描述符是否有变化,都立刻返回继续执行;
大于0,这就是等待的超时时间,即select在timeout时间内阻塞,超时时间之内有事件到来就返回了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值