Select()系统以及文件描述符
来源:互联网 发布:vpn mac 编辑:程序博客网 时间:2024/06/04 18:07
在网络程序中,一个进程同时处理多个文件描述符是很常见的情况。select()系统调用可以使进程检测同时等待的多个I/O设备,当没有设备准备好时,select()阻塞,其中任一设备准备好时,select()就返回。
select()的调用形式为: #include <sys/select.h> #include <sys/time.h> int select(int maxfd, fd_set *readfds, fd_set *writefds, fe_set *exceptfds, const struct timeval *timeout);
select的第一个参数是文件描述符集中要被检测的比特数,这个值必须至少比待检测的最大文件描述符大1;
参数readfds指定了被读监控的文件描述符集;
参数writefds指定了被写监控的文件描述符集;
而参数exceptfds指定了被例外条件监控的文件描述符集。 参数timeout起了定时器的作用:到了指定的时间,无论是否有设备准备好,都返回调用。
timeval的结构定义如下: struct timeval{ long tv_sec; //表示几秒 long tv_usec; //表示几微妙 } timeout取不同的值,该调用就表现不同的性质: 1.timeout为0,调用立即返回; 2.timeout为NULL,select()调用就阻塞,直到知道有文件描述符就绪; // 这个比较常用。3.timeout为正整数,就是一般的定时器。
#include <sys/select.h> #include <sys/time.h> void FD_SET(int fd, fd_set *fdset); void FD_CLR(int fd, fd_set *fdset); void FD_ISSET(int fd, fd_set *fdset); void FD_ZERO(fd_set *fdset);
宏FD_SET设置文件描述符集fdset中对应于文件描述符fd的位(设置为1),宏FD_CLR清除文件描述符集fdset中对应于文件描述符fd的位(设置为0),宏FD_ZERO清除文件描述符集fdset中的所有位(既把所有位都设置为0)。使用这3个宏在调用select前设置描述符屏蔽位,在调用select后使用FD_ISSET来检测文件描述符集fdset中对应于文件描述符fd的位是否被设置。
- Select()系统以及文件描述符
- select系统调用 -- 文件描述符就绪条件
- 文件描述符——select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- Linux select 文件描述符1024限制
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- Select()系统调用及文件描述符集fd_set的应用
- DataSourceMode 属性的用途
- 今日杂谈---离职自述(6.17)
- 在给定的数据窗口对象添加适当的条件字句(转)
- 身份证由15位到18位的转换
- portal
- Select()系统以及文件描述符
- 设置子窗体相对于父窗体显示于中间
- Symbian Thread and Process
- linux jpeg视频传输四部曲之二
- PowerDesigner导入java类生成类图
- fgets和freads的区别
- s3c2410全线移植linux2.6.14.1 u盘 cs8900a busybox 详细过程
- CODE
- 验证身份证