今天总结下Linux网络编程高并发的几个函数
select
int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout)
nfds 被监听描述符总数,即监听的最大描述符加1,
readfds、 writefds 、exceptfds,是指向可读、可写、异常的文件描述符集合
timeout,设置超时时间,timeout 为null时,阻塞。为0时,立即返回。
返回值:
1.正常情况下返回就绪的文件描述符个数;
2.经过了timeout时长后仍无设备准备好,返回值为0;
3.如果select被某个信号中断,它将返回-1并设置errno为EINTR。
4.如果出错,返回-1并设置相应的errno。
fd_set结构定义
#__FD_SETSIZE 是文件描述符总量
./bits/typesizes.h:#define __FD_SETSIZE 1024
typedef long int __fd_mask;
/* Some versions of <linux/posix_types.h> de