linux select函数 shutdown函数

#include<sys/select.h>

#include<sys/time.h>

int select(int maxfdp1,fd_set *readset,fd_set *writeset,fd_set *exceptset,const timeval *timeout);

*timeout:  1,NULL,永远等待;2,等待固定时间(tv_sec,tc_usec);3,不等待(tv_sec=tc_usec=0)

  struct timeval{

    long tv_sec;  秒数

    long tv_usec;  微秒数

  };

*readset,*writeset,*exceptset;内核测试 读、写、异常的descriptor

  fd_set:long int 数组  fd_set集合通过以下四个宏操作

      1,void FD_ZERO(fd_set *fdset);  清空集合

      2,void FD_SET(int fd,fd_set *fd_set);  添加fd到集合

      3,void FD_CLR(int fd,fd_set *fd_set);  删除fd从集合中

      4,int FD_ISSET(int fd,fd_set *fdset);  测试fd是否在集合中

maxfdp1:最大file descriptor plus 1

  函数返回后:未就绪descriptor对应的位均清0,所以每次调用select()函数,都需要设置fd_set集合。

返回已就绪的总位数;0,都未就绪;-1,出错

descriptor就绪条件

条件 可读吗? 可写吗? 异常吗?
有数据可读 1    
读半关闭 1(EOF)    
给监听套接口准备好新连接 1    
有空间可写   1  
写半关闭   1(SIGPIPE)  
待处理错误 1(-1,errno) 1(-1,errno)  
TCP带外数据     1

可读/可写可设置低水位标记(SO_RCVLOWAT/SO_SNDLOWAT)默认(1/2048)

#include<sys/socket.h>

int shutdown(int fd,int howto)

howto:1,SHUT_RD 读半关闭;  2,SHUT_WR 写半关闭;  3,SHUT_RDWR 读写半关闭,即全关闭;

return 0/-1