select 與 I O多路轉接

2022-09-13 07:09:09 字數 900 閱讀 5893

參考部落格:

i/0多路轉接

描述符表示某個i/o。構造一張有關描述符的資料表,呼叫select函式,返回準備就緒的描述符,如果沒有描述符準備好,則一直阻塞,超時返回0。

int select( int maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, sturct timeval *tvptr);

返回:準備就緒的描述符,若超時則為0,若出錯則為-1

maxfdp1為最大描述符值,設定為乙個常數;

readfds、writefds、exceptfds是指向描述符集的指標,表示我們關心的可讀、可寫和處異常條件的描述符;

tvptr表示願意等待的時間(秒、微秒),tvptr=null在捕捉到訊號前永遠等待。

描述符集

fd_set rset; //描述符集

int fd; //描述符集的位,表示乙個描述符

fd_zero(fd_set *fdset); //清空fdset中的所有位

fd_set(int fd, fd_set *fdset); //在fdset中開啟fd所對應的位

fd_clr(int fd, fd_set *fdset); //在fdset中關閉fd所對應的位

fd_isset(int fd, fd_set *fdset); //測試fd是否在fdset中

socket單執行緒併發程式select**:

int main(int argc,char*ar**)

if(fd_isset(stdin_fileno,&nset))

nset=oset;

}}

I O多路轉接(select

一.基本概念 二.函式原型 includeint select int nfds,fd set readfds,fd set writefds,fd set exceptions,struct timeval timeout fd set 介面 void fd clr int fd,fd set s...

select多路IO轉接

void fd zero fd set set 清空乙個檔案描述符集合。fd set rset fd zero rset void fd set int fd,fd set set 將待監聽的檔案描述符,新增到監聽集合中 fd set 3,rset fd set 5,rset fd set 6,rs...

IO 多路轉接 select

select多路io轉接 原理 借助核心,select來監聽,客戶端連線 資料通訊事件 void fd clr int fd,fd set set 將乙個檔案描述符從監聽集合中 移除 fd clr 4,rset int fd isset int fd,fd set set 判斷乙個檔案描述符是否在監...