OPEN_MAX 的可移植等效项

发布于 2024-12-17 07:29:32 字数 283 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2024-12-24 07:29:32

Unix 环境中的高级编程,第二版 为我们提供了以下代码,该代码应该可以在任何地方工作;虽然它非常聪明,但我认为有点不幸的是它没有检查进程的 rlimits,因为 rlimits 可以进一步限制进程可以使用的打开文件数量。除此之外,这是来自大师的代码:(

#ifdef  OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

/*
 * If OPEN_MAX is indeterminate, we're not
 * guaranteed that this is adequate.
 */
#define OPEN_MAX_GUESS  256

long
open_max(void)
{
    if (openmax == 0) {     /* first time through */
        errno = 0;
        if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) {
            if (errno == 0)
                openmax = OPEN_MAX_GUESS;   /* it's indeterminate */
            else
                err_sys("sysconf error for _SC_OPEN_MAX");
        }
    }

    return(openmax);
}

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:

#ifdef  OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

/*
 * If OPEN_MAX is indeterminate, we're not
 * guaranteed that this is adequate.
 */
#define OPEN_MAX_GUESS  256

long
open_max(void)
{
    if (openmax == 0) {     /* first time through */
        errno = 0;
        if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) {
            if (errno == 0)
                openmax = OPEN_MAX_GUESS;   /* it's indeterminate */
            else
                err_sys("sysconf error for _SC_OPEN_MAX");
        }
    }

    return(openmax);
}

(err_sys() is provided in the apue.h header with the sources -- should be easy to code a replacement for your routine.)

七婞 2024-12-24 07:29:32

请参阅getdtablesize。它有一个一致性注释:

SVr4,4.4BSD(getdtablesize()函数首次出现在4.2BSD中)。 POSIX.1-2001 中未指定;便携式应用程序应使用 sysconf(_SC_OPEN_MAX) 而不是此调用。

See getdtablesize. It has a conformance note:

SVr4, 4.4BSD (the getdtablesize() function first appeared in 4.2BSD). It is not specified in POSIX.1-2001; portable applications should employ sysconf(_SC_OPEN_MAX) instead of this call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文