歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux文化 >> 請問有沒有人使用過select這個函數以及FD_ZERO,FD_SET等等

請問有沒有人使用過select這個函數以及FD_ZERO,FD_SET等等

日期:2017/2/27 12:12:44   编辑:Linux文化

如果用過,請用通俗的語言解釋一下,謝謝咯

>>> 此貼的回復 >> 什麼是通俗的語言? Reply End-->

>>> 此貼的回復 >> Four macros are provided to manipulate the sets. FD_ZERO will clear a set. FD_SET and FD_CLR add or remove a given descriptor from a set. FD_ISSET tests to see if a descriptor is part of the set; this is useful after select returns.

>>> 此貼的回復 >> 那是幾個操作用的宏。一般用在select調用之後。樓上說得很清楚。 ===

>>> 此貼的回復 >> select函數: 系統提供select函數來實現多路復用輸入/輸出模型。原型: #include #include int select(int maxfd,fd_set *rdset,fd_set *wrset,fd_set *exset,struct timeval *timeout); 參數maxfd是需要監視的最大的文件描述符值+1;rdset,wrset,exset分別對應於需要檢測的可讀文件描述符的集合,可寫文件描述符的集合及異常文件描述符的集合。struct timeval結構用於描述一段時間長度,如果在這個時間內,需要監視的描述符沒有事件發生則函數返回,返回值為0。 FD_ZERO,FD_SET,FD_CLR,FD_ISSET: FD_ZERO(fd_set *fdset);將指定的文件描述符集清空,在對文件描述符集合進行設置前,必須對其進行初始化,如果不清空,由於在系統分配內存空間後,通常並不作清空處理,所以結果是不可知的。 FD_SET(fd_set *fdset);用於在文件描述符集合中增加一個新的文件描述符。 FD_CLR(fd_set *fdset);用於在文件描述符集合中刪除一個文件描述符。 FD_ISSET(int fd,fd_set *fdset);用於測試指定的文件描述符是否在該集合中。 struct timeval結構: struct timeval{ long tv_sec;//second long tv_usec;//minisecond } timeout設置情況: null:select將一直被阻塞,直到某個文件描述符上發生了事件。 0:僅檢測描述符集合的狀態,然後立即返回,並不等待外部事件的發生。 特定的時間值:如果在指定的時間段裡沒有事件發生,select將超時返回。 .......


Copyright © Linux教程網 All Rights Reserved