OPEN_MAX 的可移植等效项
nftw 想要一个参数来表示要使用的文件句柄的数量,并且似乎没有办法说“尽可能多”。指定 255 似乎在 Linux 上可行,但在 BSD 上失败。显然 OPEN_MAX 是 BSD 上推荐的解决方案,但我不能使用它,因为它在 Linux 上不起作用。
是否有可移植的 OPEN_MAX 等价物,可以在 Linux 和 BSD 上运行?
或者,是否有一个可移植的数字,某个数字足够大而不会减慢速度,可以出于实际目的而移植(理想情况下在 POSIX 中指定,或者至少可以在每个具有重要市场份额的类 Unix 系统上工作)?
nftw
wants a parameter for number of file handles to use, and doesn't seem to have a way to say 'as many as possible'. Specifying 255 seems to work on Linux, but fails on BSD. Apparently OPEN_MAX is the recommended solution on BSD, but I can't use this as it doesn't work on Linux.
Is there a portable equivalent of OPEN_MAX that will work on both Linux and BSD?
Alternatively, is there a portable number, some number large enough to not slow things down, that is portable for practical purposes (ideally specified in POSIX, or at least that will work on every Unix-like system with significant market share)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Unix 环境中的高级编程,第二版 为我们提供了以下代码,该代码应该可以在任何地方工作;虽然它非常聪明,但我认为有点不幸的是它没有检查进程的 rlimits,因为 rlimits 可以进一步限制进程可以使用的打开文件数量。除此之外,这是来自大师的代码:(
err_sys() 在
apue.h
标头中提供了源代码 - 应该很容易编写例程的替代代码。)Advanced Programming in the Unix Environment, 2nd Ed gives us the following code which should work everywhere; though it is pretty clever, I think it is a little unfortunate it doesn't also check the rlimits of the process, since the rlimits can further constrain how many open files a process may use. That aside, here's the code from The Master:
(
err_sys()
is provided in theapue.h
header with the sources -- should be easy to code a replacement for your routine.)请参阅
getdtablesize
。它有一个一致性注释:See
getdtablesize
. It has a conformance note: