关于linux中的select

来源:百度文库 编辑:神马文学网 时间:2024/10/06 11:25:29
一、FD SET操作的宏定义
1、在文件 http://lxr.linux.no/#linux+v2.6.32.8/include/linux/time.h#L246 中有如下定义:

 243 #define NFDBITS                 __NFDBITS
244
245 #define FD_SETSIZE __FD_SETSIZE
246 #define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp)
247 #define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp)
248 #define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp)
249 #define FD_ZERO(fdsetp) __FD_ZERO(fdsetp)


2、在文件 http://lxr.linux.no/#linux+v2.6.32.8/include/asm-generic/posix_types.h#L112 中有如下定义

93#ifdef __KERNEL__
94
95#undef __FD_SET
96static inline void __FD_SET(unsigned long __fd, __kernel_fd_set *__fdsetp)
97{
98 unsigned long __tmp = __fd / __NFDBITS;
99 unsigned long __rem = __fd % __NFDBITS;
100 __fdsetp->fds_bits[__tmp] |= (1UL<<__rem);
101}
102
103#undef __FD_CLR
104static inline void __FD_CLR(unsigned long __fd, __kernel_fd_set *__fdsetp)
105{
106 unsigned long __tmp = __fd / __NFDBITS;
107 unsigned long __rem = __fd % __NFDBITS;
108 __fdsetp->fds_bits[__tmp] &= ~(1UL<<__rem);
109}
110
111#undef __FD_ISSET
112static inline int __FD_ISSET(unsigned long __fd, const __kernel_fd_set *__p)
113{
114 unsigned long __tmp = __fd / __NFDBITS;
115 unsigned long __rem = __fd % __NFDBITS;
116 return (__p->fds_bits[__tmp] & (1UL<<__rem)) != 0;
117}
118
119/*
120 * This will unroll the loop for the normal constant case (8 ints,
121 * for a 256-bit fd_set)
122 */
123#undef __FD_ZERO
124static inline void __FD_ZERO(__kernel_fd_set *__p)
125{
126 unsigned long *__tmp = __p->fds_bits;
127 int __i;
128
129 if (__builtin_constant_p(__FDSET_LONGS)) {
130 switch (__FDSET_LONGS) {
131 case 16:
132 __tmp[ 0] = 0; __tmp[ 1] = 0;
133 __tmp[ 2] = 0; __tmp[ 3] = 0;
134 __tmp[ 4] = 0; __tmp[ 5] = 0;
135 __tmp[ 6] = 0; __tmp[ 7] = 0;
136 __tmp[ 8] = 0; __tmp[ 9] = 0;
137 __tmp[10] = 0; __tmp[11] = 0;
138 __tmp[12] = 0; __tmp[13] = 0;
139 __tmp[14] = 0; __tmp[15] = 0;
140 return;
141
142 case 8:
143 __tmp[ 0] = 0; __tmp[ 1] = 0;
144 __tmp[ 2] = 0; __tmp[ 3] = 0;
145 __tmp[ 4] = 0; __tmp[ 5] = 0;
146 __tmp[ 6] = 0; __tmp[ 7] = 0;
147 return;
148
149 case 4:
150 __tmp[ 0] = 0; __tmp[ 1] = 0;
151 __tmp[ 2] = 0; __tmp[ 3] = 0;
152 return;
153 }
154 }
155 __i = __FDSET_LONGS;
156 while (__i) {
157 __i--;
158 *__tmp = 0;
159 __tmp++;
160 }
161}
162
163#endif /* __KERNEL__ */

分析可见,__FD_SET、__FD_CLR、__FD_ISSET,都只是针对参数fdset中的数组 fds_sets 进行相应位操作,而且并没有进行什么互斥保护。另外,这几个函数都是inline函数。
尤其注意的是,__FD_ISSET,也是对进行相应的位进行判断。由此,可以推断,内核中对是否可读写的操作就是对相应位的设置或者清除。

可以看一下select函数的实现过程,是怎么对这些位进行操作的。