11.01.2015 Views

I/O Multiplexing: select and poll

I/O Multiplexing: select and poll

I/O Multiplexing: select and poll

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Loop<br />

for ( ; ; ) {<br />

<strong>select</strong> 的 傳 回 值 是 ready 的 裝 置 數 目<br />

rset = allset; /* structure assignment */<br />

nready = Select(maxfd+1, &rset, NULL, NULL, NULL);<br />

if (FD_ISSET(listenfd, &rset)) { /* new client connection */<br />

新<br />

連<br />

結<br />

放 入 第 一 個<br />

找 到 的 空 格<br />

如 果 沒 有 其<br />

它 ready 的<br />

descriptor<br />

}<br />

clilen = sizeof(cliaddr);<br />

connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);<br />

for (i = 0; i < FD_SETSIZE; i++)<br />

if (client[i] < 0) {<br />

client[i] = connfd; /* save descriptor */<br />

break;<br />

}<br />

if (i == FD_SETSIZE)<br />

err_quit("too many clients");<br />

tcpcliserv/tcpserv<strong>select</strong>01.c<br />

加 進 <strong>select</strong> 要 test<br />

的 descriptor set<br />

不 會 block<br />

FD_SET(connfd, &allset); /* add new descriptor to set */<br />

if (connfd > maxfd)<br />

maxfd = connfd; /* for <strong>select</strong> */<br />

if (i > maxi)<br />

maxi = i; /* max index in client[] array */<br />

if (--nready

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!