日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

select fd_set

發布時間:2025/3/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 select fd_set 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

fd_set 結構,是一個數字。 文件描述符fd的值 不能超過1024,而不是數量不能超過1024。

在開發打開句柄多的程序時,最好不要使用select。否則就使用多進程開發。

/usr/include/X11 Xpoll.h 文件

#ifndef _XPOLL_H_
#define _XPOLL_H_

#ifndef WIN32

#ifndef USE_POLL

#include <X11/Xos.h>

#include <sys/select.h>? /* Get the FD_* macros. */

#include <X11/Xmd.h>

#ifdef CSRG_BASED
#include <sys/param.h>
# if BSD < 199103
typedef long fd_mask;
# endif
#endif

#define XFD_SETSIZE?? ?256

#ifndef FD_SETSIZE
#define FD_SETSIZE?? ?XFD_SETSIZE
#endif

#ifndef NBBY
#define NBBY?? ?8?? ??? ?/* number of bits in a byte */
#endif

#ifndef NFDBITS
#define NFDBITS (sizeof(fd_mask) * NBBY)?? ?/* bits per mask */
#endif

#ifndef howmany
#define howmany(x,y)?? ?(((x)+((y)-1))/(y))
#endif

#if defined(BSD) && BSD < 198911
typedef struct fd_set {
?? ?fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
} fd_set;
#endif

# define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)

#define __X_FDS_BITS __fds_bits

#ifndef __FDS_BITS
# define __FDS_BITS(p)? ((p)->__X_FDS_BITS)
#endif

#define __XFDS_BITS(p, n) (__FDS_BITS(p))[n]

#ifndef FD_SET
#define FD_SET(n, p)??? (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS)))
#endif
#ifndef FD_CLR
#define FD_CLR(n, p)??? (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS)))
#endif
#ifndef FD_ISSET
#define FD_ISSET(n, p)? ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS)))
#endif
#ifndef FD_ZERO
#define FD_ZERO(p)????? bzero((char *)(p), sizeof(*(p)))
#endif

/*
?* The howmany(FD_SETSIZE, NFDBITS) computes the number of elements in the
?* array. before accessing an element in the array we check it exists.
?* If it does not exist then the compiler discards the code to access it.
?*/
#define XFD_ANYSET(p) \
??????? ((howmany(FD_SETSIZE, NFDBITS) > 0 && (__XFDS_BITS(p, 0))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 1 && (__XFDS_BITS(p, 1))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 2 && (__XFDS_BITS(p, 2))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 3 && (__XFDS_BITS(p, 3))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 4 && (__XFDS_BITS(p, 4))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 5 && (__XFDS_BITS(p, 5))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 6 && (__XFDS_BITS(p, 6))) || \
??????? (howmany(FD_SETSIZE, NFDBITS) > 7 && (__XFDS_BITS(p, 7))))

#define XFD_COPYSET(src,dst) { \
??????? int __i__; \
?? ??? ?for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
??????????? __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \
??????? }
#define XFD_ANDSET(dst,b1,b2) { \
??????? int __i__; \
??????? for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
??????????? __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \
??????? }
#define XFD_ORSET(dst,b1,b2) { \
??????? int __i__; \
??????? for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
?? ??? ?__XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \
??????? }?????? ?
#define XFD_UNSET(dst,b1) { \
??????? int __i__; \
??????? for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
?? ??? ??? ?__XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \
??????? }

#else /* USE_POLL */
#include <sys/poll.h>
#endif /* USE_POLL */

#else /* WIN32 */

#define XFD_SETSIZE?? ?256
#ifndef FD_SETSIZE
#define FD_SETSIZE?? ?XFD_SETSIZE
#endif
#include <X11/Xwinsock.h>

#define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)

#define XFD_SETCOUNT(p)?? ?(((fd_set FAR *)(p))->fd_count)
#define XFD_FD(p,i) (((fd_set FAR *)(p))->fd_array[i])
#define XFD_ANYSET(p)?? ?XFD_SETCOUNT(p)

#define XFD_COPYSET(src,dst) { \
??? u_int __i; \
??? FD_ZERO(dst); \
??? for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
??????? XFD_FD(dst,__i) = XFD_FD(src,__i); \
??? } \
??? XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
}

#define XFD_ANDSET(dst,b1,b2) { \
??? u_int __i; \
??? FD_ZERO(dst); \
??? for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
??????? if (FD_ISSET(XFD_FD(b1,__i), b2)) \
?? ??? FD_SET(XFD_FD(b1,__i), dst); \
??? } \
}

#define XFD_ORSET(dst,b1,b2) { \
??? u_int __i; \
??? if (dst != b1) XFD_COPYSET(b1,dst); \
??? for (__i = 0; __i < XFD_SETCOUNT(b2) ; __i++) { \
??????? if (!FD_ISSET(XFD_FD(b2,__i), dst)) \
?? ??? FD_SET(XFD_FD(b2,__i), dst); \
??? } \
}

/* this one is really sub-optimal */
#define XFD_UNSET(dst,b1) { \
??? u_int __i; \
??? for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
?? ?FD_CLR(XFD_FD(b1,__i), dst); \
??? } \
}

/* we have to pay the price of having an array here, unlike with bitmasks
?? calling twice FD_SET with the same fd is not transparent, so be careful */
#undef FD_SET
#define FD_SET(fd,set) do { \
??? if (XFD_SETCOUNT(set) < FD_SETSIZE && !FD_ISSET(fd,set)) \
??????? XFD_FD(set,XFD_SETCOUNT(set)++)=(fd); \
} while(0)

#define getdtablesize() FD_SETSIZE

#endif /* WIN32 */

#endif /* _XPOLL_H_ */

轉載于:https://my.oschina.net/u/1445488/blog/351502

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的select fd_set的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。