select()和poll()的區別是什麼?
Whats thedifference between select() and poll()?
二者根本的不同是:select()的fd_set是一個位掩碼(bit mask),因此fd_set有固定的長度。核心在被編譯的時候,可以不受這個長度的限制,因為select()允許應用程式自定義FD_SETSIZE的大小(在如今的系統標頭檔案中可以看到這一點),但是這會增加額外的支出。4.4BSD的核心和Solaris的函式庫卻都有這個長度的限制。但是,我發現了BSD/OS 2.1已經沒有了這個限制,所以自定義FD_SETSIZE是可行的,不過正如剛才講的確實給程式設計帶來了小麻煩。哈哈。有些人應該在這個問題上列出Solaris的bug 報告,並且看一下這個長度是不是固定的。
然而,使用者在呼叫poll()時需要自定義pollfd結構體陣列並且需要指定陣列的大小,所以呢這裡原理上講就是沒有限制的。As Casper notes(這個不知道如何翻譯,⊙﹏⊙b汗),相對於select()而言,支援poll()的系統是很少的,所以前者有更好的相容性。而且,在poll()的原始實現(SVR3)中,你是不能通過設定pollfd 結構體陣列某個項中的fd值為-1來通知核心忽略該項,這樣的結果是從一個數組中移除一項是很困難的;SVR4解決了這個問題。就我個人而言,我經常使用select()而很少使用poll(),因為我也要把我的程式碼移植到BSD的環境。某些人可以通過呼叫select()來實現poll()來適應這些環境,但是我從沒見過這樣的實現。Select()和poll()都正在實現POSIX
1003.1g的標準。(All Rights Reserved
The basic difference is that select()'s fd_set is abit mask and
therefore has some fixed size. It would be possible for the kernel to
not limit this size when the kernel is compiled,allowing the
application to define FD_SETSIZE to whatever it wants(as the comments
in the system header imply today) but it takes morework. 4.4BSD's
kernel and the Solaris library function both have thislimit. But I
see that BSD/OS 2.1 has now been coded to avoid thislimit, so it's
doable, just a small matter of programming. :-) Someone should file a
Solaris bug report on this, and see if it ever getsfixed.
With poll(), however, the user must allocate an arrayof pollfd
structures, and pass the number of entries in thisarray, so there's
no fundamental limit. As Casper notes, fewer systems have poll() than
select, so the latter is more portable. Also, with original
implementations (SVR3) you could not set thedescriptor to -1 to tell
the kernel to ignore an entry in the pollfd structure,which made it
hard to remove entries from the array; SVR4 getsaround this.
Personally, I always use select() and rarely poll(),because I port my
code to BSD environments too. Someone could write an implementation
of poll() that uses select(), for these environments,but I've never
seen one. Both select() and poll() are beingstandardized by POSIX
1003.1g.