select()和poll()的区别是什么?

Whats thedifference between select() and poll()?

原文地址:http://www.unixguide.net/network/socketfaq/2.14.shtml

From Richard Stevens ([email protected]):

二者根本的不同是: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.



你可能感兴趣的:(Solaris,report,header,basic,library,structure)