代码改变世界

操作fd_set的三个函数的实现小记

2010-02-03 16:26  Iron  阅读(627)  评论(0编辑  收藏  举报
#undef __FD_SET
static __inline__ void __FD_SET(unsigned long fd, __kernel_fd_set *fdsetp)
{
    unsigned long _tmp = fd / __NFDBITS;
    unsigned long _rem = fd % __NFDBITS;
    fdsetp->fds_bits[_tmp] |= (1UL<<_rem);
}

#undef __FD_CLR
static __inline__ void __FD_CLR(unsigned long fd, __kernel_fd_set *fdsetp)
{
    unsigned long _tmp = fd / __NFDBITS;
    unsigned long _rem = fd % __NFDBITS;
    fdsetp->fds_bits[_tmp] &= ~(1UL<<_rem);
}

#undef __FD_ISSET
static __inline__ int __FD_ISSET(unsigned long fd, const __kernel_fd_set *p) 
{ 
    unsigned long _tmp = fd / __NFDBITS;
    unsigned long _rem = fd % __NFDBITS;
    return (p->fds_bits[_tmp] & (1UL<<_rem)) != 0;
}

这两天在研究unix网络编程,在看select的时候看到了这三个函数,想必高手都是知道的,我本来以为这三个函数的效率不怎么样,不过看了源码才知道,真实坐井观天啊,原来这些经常在面试题里面出现的技巧便真是可实战的,呵呵~惭愧阿,看来功夫还是不到家~