标签:fd-set
fd_set的实现详细原理
define FD_SETSIZE 1024
typedef unsigned long fd_mask;
#define NBBY 8 /* number of bits in a byte */
#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
#define howmany(x, y) (((x) + ((y) - 1)) / (y))
typedef struct fd_set {
fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
} fd_set;
#define _fdset_mask(n) ((fd_mask)1 << ((n) % NFDBITS))
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= _fdset_mask(n))
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~_fdset_mask(n))
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & _fdset_mask(n))
#define FD_COPY(f, t) bcopy(f, t, sizeof(*(f)))
#define FD_ZERO(p) bzero(p, sizeof(*(p)))
假如有如下定义:
fd_set read_set;
FD_SET(600,read_set);
那么最终的推导如下:
=> read_set->fds_bits[(600 + 32 - 1)/32] |= (1<<(600%32));
标签:fd-set
原文地址:http://blog.csdn.net/jxnu_xiaobing/article/details/44961733