在使用select函数时,fd_set结构体是很重要的。

想正确使用select函数,理解fd_set是必不可少的。

<sys/select.h>

下面给出<sys/select.h>头文件的全部内容:

/* `fd_set' type and related macros, and `select'/`pselect' declarations.Copyright (C) 1996-2016 Free Software Foundation, Inc.This file is part of the GNU C Library.The GNU C Library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.The GNU C Library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with the GNU C Library; if not, see<http://www.gnu.org/licenses/>.  *//* POSIX 1003.1g: 6.2 Select from File Descriptor Sets <sys/select.h>  */#ifndef _SYS_SELECT_H
#define _SYS_SELECT_H   1#include <features.h>/* Get definition of needed basic types.  */
#include <bits/types.h>/* Get __FD_* definitions.  */
#include <bits/select.h>/* Get __sigset_t.  */
#include <bits/sigset.h>#ifndef __sigset_t_defined
# define __sigset_t_defined
typedef __sigset_t sigset_t;
#endif/* Get definition of timer specification structures.  */
#define __need_time_t
#ifdef __USE_XOPEN2K
# define __need_timespec
#endif
#include <time.h>
#define __need_timeval
#include <bits/time.h>#ifndef __suseconds_t_defined
typedef __suseconds_t suseconds_t;
# define __suseconds_t_defined
#endif/* The fd_set member is required to be an array of longs.  */
typedef long int __fd_mask;/* Some versions of <linux/posix_types.h> define this macros.  */
#undef  __NFDBITS
/* It's easier to assume 8-bit bytes than to get CHAR_BIT.  */
#define __NFDBITS   (8 * (int) sizeof (__fd_mask))
#define __FD_ELT(d) ((d) / __NFDBITS)
#define __FD_MASK(d)    ((__fd_mask) (1UL << ((d) % __NFDBITS)))/* fd_set for select and pselect.  */
typedef struct{/* XPG4.2 requires this member name.  Otherwise avoid the namefrom the global namespace.  */
#ifdef __USE_XOPEN__fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->fds_bits)
#else__fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->__fds_bits)
#endif} fd_set;/* Maximum number of file descriptors in `fd_set'.  */
#define FD_SETSIZE      __FD_SETSIZE#ifdef __USE_MISC
/* Sometimes the fd_set member is assumed to have this type.  */
typedef __fd_mask fd_mask;/* Number of bits per word of `fd_set' (some code assumes this is 32).  */
# define NFDBITS        __NFDBITS
#endif/* Access macros for `fd_set'.  */
#define FD_SET(fd, fdsetp)  __FD_SET (fd, fdsetp)
#define FD_CLR(fd, fdsetp)  __FD_CLR (fd, fdsetp)
#define FD_ISSET(fd, fdsetp)    __FD_ISSET (fd, fdsetp)
#define FD_ZERO(fdsetp)     __FD_ZERO (fdsetp)__BEGIN_DECLS/* Check the first NFDS descriptors each in READFDS (if not NULL) for readreadiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS(if not NULL) for exceptional conditions.  If TIMEOUT is not NULL, time outafter waiting the interval specified therein.  Returns the number of readydescriptors, or -1 for errors.This function is a cancellation point and therefore not marked with__THROW.  */
extern int select (int __nfds, fd_set *__restrict __readfds,fd_set *__restrict __writefds,fd_set *__restrict __exceptfds,struct timeval *__restrict __timeout);#ifdef __USE_XOPEN2K
/* Same as above only that the TIMEOUT value is given with higherresolution and a sigmask which is been set temporarily.  This versionshould be used.This function is a cancellation point and therefore not marked with__THROW.  */
extern int pselect (int __nfds, fd_set *__restrict __readfds,fd_set *__restrict __writefds,fd_set *__restrict __exceptfds,const struct timespec *__restrict __timeout,const __sigset_t *__restrict __sigmask);
#endif/* Define some inlines helping to catch common problems.  */
#if __USE_FORTIFY_LEVEL > 0 && defined __GNUC__
# include <bits/select2.h>
#endif__END_DECLS#endif /* sys/select.h */

fd_set结构体

由上面可以看到,fd_set结构体的定义为:

/* fd_set for select and pselect.  */
typedef struct{/* XPG4.2 requires this member name.  Otherwise avoid the namefrom the global namespace.  */
#ifdef __USE_XOPEN__fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->fds_bits)
#else__fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->__fds_bits)
#endif} fd_set;

其中 __fd_mask 为long int 类型,查看select.h文件第56行:

/* The fd_set member is required to be an array of longs.  */
typedef long int __fd_mask;

和select模型紧密相关的四个宏:

/* Access macros for `fd_set'.  */
#define FD_SET(fd, fdsetp)  __FD_SET (fd, fdsetp)
#define FD_CLR(fd, fdsetp)  __FD_CLR (fd, fdsetp)
#define FD_ISSET(fd, fdsetp)    __FD_ISSET (fd, fdsetp)
#define FD_ZERO(fdsetp)     __FD_ZERO (fdsetp)

<bits/select.h>

定义在<bits/select.h>头文件中,该文件内容如下:

/* Copyright (C) 1997-2016 Free Software Foundation, Inc.This file is part of the GNU C Library.The GNU C Library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.The GNU C Library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with the GNU C Library; if not, see<http://www.gnu.org/licenses/>.  */#ifndef _SYS_SELECT_H
# error "Never use <bits/select.h> directly; include <sys/select.h> instead."
#endif/* We don't use `memset' because this would require a prototype andthe array isn't too big.  */
#define __FD_ZERO(s) \do {                                                                        \unsigned int __i;                                                         \fd_set *__arr = (s);                                                      \for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i)          \__FDS_BITS (__arr)[__i] = 0;                                            \} while (0)
#define __FD_SET(d, s) \((void) (__FDS_BITS (s)[__FD_ELT(d)] |= __FD_MASK(d)))
#define __FD_CLR(d, s) \((void) (__FDS_BITS (s)[__FD_ELT(d)] &= ~__FD_MASK(d)))
#define __FD_ISSET(d, s) \((__FDS_BITS (s)[__FD_ELT (d)] & __FD_MASK (d)) != 0)

宏的定义如上所示。具体功能介绍,参考下面链接:

https://blog.csdn.net/starflame/article/details/7860091

Linux:fd_set 结构体定义及使用相关推荐

  1. Linux下查找结构体定义的位置

    1.首先我们要进入目录: 比如:cd /usr/include 2.查找在当前目录下的结构体: 指令:grep "struct sockaddr_in {" * -nir 形式:g ...

  2. linux c 定义结构体,Linux查看C结构体定义

    今天写程序时,用到了pthread_cond_timedwait 函数,其原型为:int pthread_cond_timedwait(pthread_cond_t  *restrict cond , ...

  3. 结构体定义变量及初始化

    当我们定义了结构体类型之后,可以使用结构体类型来定义变量,定义变量的方式与基本数据类型定义变量一样.假设我们定义了如下的结构体类型: //定义结构体类型 struct reader { //定义结构体 ...

  4. Go 学习笔记(14)— 结构体定义、实例化、初始化、匿名结构体、结构体访问、结构体作为形参、结构体指针

    Go 语言中没有 "类" 的概念,也不支持 "类" 的继承等面向对象的概念.Go 语言不仅认为结构体能拥有方法,且每种自定义类型也可以拥有自己的方法. 1. 结 ...

  5. c++结构体定义和使用_[day day go]结构体amp;给结构定义方法

    结构体 //定义 type treeNode struct {value intleft, right *treeNode }//工厂函数创建 func createNode(value int) * ...

  6. C#中结构体定义并转换字节数组

    ref: https://www.cnblogs.com/dafanjoy/p/7818126.html C#中结构体定义并转换字节数组 最近的项目在做socket通信报文解析的时候,用到了结构体与字 ...

  7. 结构体定义小的放前面_编程C语言进阶篇——自定义数据类型:结构体

    一.结构体 定义方法: 结构名 变量名 特点: 两个同类型的结构变量可以相互赋值,但是结构变量之间不能使用"<","=="等运算符,如果使用则需要对运算符 ...

  8. C/C++中struct结构体定义变量的3种方法及初始化

    本博客整理自http://blog.csdn.net/zunfo/article/details/51494631 1.struct结构体定义 1.1.先定义结构体,然后再定义结构体变量 struct ...

  9. C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例

    C++ 基础入门 之 结构体/结构体定义和使用/结构体数组/结构体指针/ 结构体嵌套结构体/结构体做函数参数/结构体中 const 使用场景/结构体案例 目录 一.简单介绍 二.结构体定义和使用 三. ...

最新文章

  1. R语言ggplot2可视化:绘制堆叠的密度图(Stacked Area Chart)
  2. ubuntu之路——day8.4 Adam自适应矩估计算法
  3. 解决AndroidManifest.xml file missing方案
  4. 如何用python把xlsx变为csv_python将excel转换为csv的代码方法总结
  5. ARC080F - Prime Flip(贪心,差分,二分图匹配)
  6. 智能可穿戴迎来长续航焕新活力 出门问问TicWatch Pro 3即将国内上市
  7. JAVA基础——Java 中必须了解的常用类
  8. 拓端tecdat|R语言解决最优化运营研究问题-线性优化(LP)问题
  9. windows10清理鼠标右键菜单
  10. matlab如何读取一个图片,怎么用Matlab读入并显示图片文件
  11. java迁移框架_Java敏捷数据库迁移框架——Flyway
  12. unity 实现流光效果
  13. 白杨SEO:新媒体如何避免侵权?这篇自媒体防侵权实用指南让你少走弯路!
  14. 电子烟市场Juul来袭,锐刻福禄们该如何做防?
  15. 中英文切换遇到的坑-总结
  16. 小熊错误_坚守好股票、寻找穿越牛熊十倍股:小熊电器、贝达药业、开立医疗!...
  17. 网关(Gateway)
  18. 2016年1月28日github 出现大面积访问故障
  19. 一图掌握PEST分析模型及案例
  20. 【C#】创建快捷方式

热门文章

  1. 医院系统集成平台和临床数据中心CDR、大数据平台之间的关系?
  2. c语言编活期储蓄银行系统,C语言编程1活期存款。活期利息每一季度结算...
  3. leetcode-55. 跳跃游戏--【DFS】【贪心】
  4. Imagination宣布推出基于RISC-V的CPU产品系列
  5. 音乐API调用以及分析(以酷狗音乐为例)
  6. oracle 创建 em
  7. 计算机硬件系统中外设,计算机及外设 计算机硬件系统
  8. 模式识别分类器评价指标之DET曲线
  9. mysql 编码错误_【分享】MySQl操作系统提示错误编码
  10. 怎样恢复计算机管理员用户,忘记了电脑系统Administrator账户的密码?如何恢复?...