相当于OPEN_MAX的便携式 [英] Portable equivalent of OPEN_MAX

查看:79
本文介绍了相当于OPEN_MAX的便携式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nftw想要一个用于使用文件句柄数量的参数,并且似乎没有办法说尽可能多".指定255似乎在Linux上有效,但在BSD上失败.显然,OPEN_MAX是BSD上的推荐解决方案,但是由于它在Linux上不起作用,因此我不能使用它.

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.

是否存在可以在Linux和BSD上同时使用的OPEN_MAX便携式版本?

Is there a portable equivalent of OPEN_MAX that will work on both Linux and BSD?

或者,是否存在一个可移植的数字,该数字足够大以至于不会降低运行速度,该数字是出于实际目的可移植的(理想情况是在POSIX中指定,或者至少可以在每个具有重要市场份额的类Unix系统上使用) ?

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)?

推荐答案

Unix环境中的高级编程,第二版为我们提供了以下代码,该代码应可在任何地方使用;尽管它很聪明,但我认为这有点遗憾,因为它没有检查进程的rlimit,因为rlimit可以进一步限制一个进程可以使用多少个打开文件.除此之外,这是大师中的代码:

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);
}

(apue.h标头中提供了err_sys()以及源代码-应该很容易为您的例程编写替换代码.)

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

这篇关于相当于OPEN_MAX的便携式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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