poll 和 select 有什么区别? [英] What are the differences between poll and select?

查看:18
本文介绍了poll 和 select 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是 POSIX 标准 selectpoll 系统 C API 调用.

I am referring to the POSIX standard select and poll system C API calls.

推荐答案

我认为 this 回答您的问题:

I think that this answers your question:

来自理查德史蒂文斯 (rstevens@noao.edu):

From Richard Stevens (rstevens@noao.edu):

基本的区别是 select() 的 fd_set 是位掩码,而因此有一些固定的大小.内核有可能编译内核时不限制这个大小,允许将 FD_SETSIZE 定义为任何它想要的应用程序(如注释在系统标题中暗示今天),但这需要更多的工作.4.4BSD的内核和 Solaris 库函数都有这个限制.但是我看到 BSD/OS 2.1 现在已经被编码来避免这个限制,所以它是可行,只是编程的一个小问题.:-) 有人应该提交一份Solaris 对此的错误报告,看看它是否得到修复.

The basic difference is that select()'s fd_set is a bit 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 more work. 4.4BSD's kernel and the Solaris library function both have this limit. But I see that BSD/OS 2.1 has now been coded to avoid this limit, so it's doable, just a small matter of programming. :-) Someone should file a Solaris bug report on this, and see if it ever gets fixed.

然而,使用 poll(),用户必须分配一个 pollfd 数组结构,并传递这个数组中的条目数,所以有没有根本限制.正如 Casper 所指出的,具有 poll() 的系统少于选择,所以后者更便携.另外,与原实现 (SVR3) 你不能将描述符设置为 -1 来告诉内核忽略 pollfd 结构中的一个条目,这使得它很难从数组中删除条目;SVR4 解决了这个问题.就我个人而言,我总是使用 select() 而很少使用 poll(),因为我移植了我的代码也适用于 BSD 环境.有人可以写一个实现使用 select() 的 poll(),对于这些环境,但我从来没有看过一个.select() 和 poll() 都被 POSIX 标准化了1003.1g.

With poll(), however, the user must allocate an array of pollfd structures, and pass the number of entries in this array, 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 the descriptor 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 gets around 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 being standardized by POSIX 1003.1g.

2017 年 10 月更新:

上面提到的电子邮件至少是 2001 年的;poll() 命令现在 (2017) 支持所有现代操作系统 - 包括 BSD.事实上,有些人认为 select() 应该被弃用.撇开观点不谈,围绕 poll() 的可移植性问题不再是现代系统的问题.此外,epoll() 已经被开发出来(你可以 阅读手册页),并且越来越受欢迎.

October 2017 Update:

The email referenced above is at least as old as 2001; the poll() command is now (2017) supported across all modern operating systems - including BSD. In fact, some people believe that select() should be deprecated. Opinions aside, portability issues around poll() are no longer a concern on modern systems. Furthermore, epoll() has since been developed (you can read the man page), and continues to rise in popularity.

对于现代开发,您可能不想使用 select(),尽管它没有明显的错误.poll(),它是更现代的演变 epoll(),提供与 select() 相同的功能(甚至更多),而不会受到其中的限制.

For modern development you probably don't want to use select(), although there's nothing explicitly wrong with it. poll(), and it's more modern evolution epoll(), provide the same features (and more) as select() without suffering from the limitations therein.

这篇关于poll 和 select 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆