Linux系统的OPEN_MAX定义在哪里?

Linux系统的OPEN_MAX定义在哪里?,linux,gcc,system-calls,Linux,Gcc,System Calls,OPEN_MAX是定义单个程序允许打开的最大文件数的常量 根据《Linux编程入门》第4版第101页: 极限通常由极限.h中的常数OPEN_MAX定义,因系统而异 在我的系统中,目录/usr/lib/gcc/x86\u 64-linux-gnu/4.6/include fixed中的文件limits.h没有此常量。我看的是错误的限制.h还是自2008年以来更改了OPEN\u MAX的位置?我建议使用grep的魔力在/usr/include上找到该常数: grep -rn --col OPEN_M

OPEN_MAX
是定义单个程序允许打开的最大文件数的常量

根据《Linux编程入门》第4版第101页:

极限通常由极限.h中的常数OPEN_MAX定义,因系统而异


在我的系统中,目录
/usr/lib/gcc/x86\u 64-linux-gnu/4.6/include fixed中的文件
limits.h
没有此常量。我看的是错误的
限制.h
还是自2008年以来更改了
OPEN\u MAX
的位置?

我建议使用
grep
的魔力在
/usr/include
上找到该常数:

grep -rn --col OPEN_MAX /usr/include

...
...
/usr/include/stdio.h:159:   FOPEN_MAX   Minimum number of files that can be open at once.
...
...

希望它能帮助您

除了cste提供的链接之外,我想指出的是,
/proc/sys/fs/file max
条目提供了系统在任何给定时间可以打开的文件数量

以下是一些文档:

请注意,这并不是说您可以保证打开那么多文件-如果系统耗尽某些资源(例如“没有更多可用内存”),那么它很可能会失败

FOPEN_MAX表示C库允许打开这么多的文件(至少如上所述),但首先可能会出现其他限制。例如,系统限制为4000个文件,一些已经运行的应用程序打开了3990个文件。那么您将无法打开超过7个文件[因为stdin、stdout和stderr也占用了三个插槽]。如果
rlimit
设置为5,则只能打开自己的2个文件


在我看来,知道你是否能打开一个文件的最好方法就是打开它。如果失败了,你必须做些别的事情。如果您有一些进程需要打开多个文件[例如,在一台拥有256个内核和每个内核8个线程的机器上进行多线程搜索/比较,并且每个线程使用三个文件(文件“a”、“B”和“diff”)],那么您可能需要确保您的FOPEN_MAX允许在开始创建线程之前打开3*8*256个文件,作为一个线程,无法打开它的文件将是毫无意义的。但对于大多数普通应用程序,只要尝试打开文件,如果失败,请告诉用户(日志或其他),然后/或重试

值得一提的是,2007年出版了第四版;部分内容可能有点过时。(这不是对这本书的批评,我还没有读过。)

至少在Linux系统上,
OPEN\u MAX
似乎已被弃用。原因似乎是可以同时打开的最大文件数不是固定的,因此扩展为整数文本的宏不是获取该信息的好方法

还有另一个宏
FOPEN_MAX
应该是类似的;我想不出为什么
OPEN_MAX
FOPEN_MAX
(如果两者都定义了)应该有不同的值。但是
FOPEN_MAX
是由C语言标准强制执行的,所以系统不能不定义它。C标准规定
FOPEN\u MAX

展开为整数常量表达式,该表达式是所需的最小文件数 实现保证可以同时开放

(如果“minimum”一词令人困惑,则可以保证一个程序一次至少可以打开这么多文件。)

如果您想要当前可打开的最大文件数,请查看该函数;在我的系统上,
sysconf(\u SC\u OPEN\u MAX)
返回1024。(
sysconf()
手册页引用了一个符号
OPEN\u MAX
。这不是一个计数,而是一个由
sysconf()
识别的值。我的系统上没有定义它。)

我在我的Ubuntu系统上搜索了
OPEN\u MAX
(单词匹配,因此排除了
FOPEN\u MAX
),发现了以下内容(这些显然只是简短的摘录):

/usr/include/X11/Xos.h

# ifdef __GNU__
#  define PATH_MAX 4096
#  define MAXPATHLEN 4096
#  define OPEN_MAX 256 /* We define a reasonable limit.  */
# endif
/* The kernel header pollutes the namespace with the NR_OPEN symbol
   and defines LINK_MAX although filesystems have different maxima.  A
   similar thing is true for OPEN_MAX: the limit can be changed at
   runtime and therefore the macro must not be defined.  Remove this
   after including the header if necessary.  */  
#ifndef NR_OPEN
# define __undef_NR_OPEN
#endif
#ifndef LINK_MAX
# define __undef_LINK_MAX
#endif
#ifndef OPEN_MAX
# define __undef_OPEN_MAX
#endif
#ifndef ARG_MAX
# define __undef_ARG_MAX
#endif
/* We do not provide fixed values for 

   ARG_MAX      Maximum length of argument to the `exec' function
                including environment data.

   ATEXIT_MAX   Maximum number of functions that may be registered
                with `atexit'.

   CHILD_MAX    Maximum number of simultaneous processes per real
                user ID. 

   OPEN_MAX     Maximum number of files that one process can have open
                at anyone time.

   PAGESIZE
   PAGE_SIZE    Size of bytes of a page.

   PASS_MAX     Maximum number of significant bytes in a password.

   We only provide a fixed limit for

   IOV_MAX      Maximum number of `iovec' structures that one process has
                available for use with `readv' or writev'.

   if this is indeed fixed by the underlying system.
*/
/usr/include/i386 linux gnu/bits/local_lim.h

# ifdef __GNU__
#  define PATH_MAX 4096
#  define MAXPATHLEN 4096
#  define OPEN_MAX 256 /* We define a reasonable limit.  */
# endif
/* The kernel header pollutes the namespace with the NR_OPEN symbol
   and defines LINK_MAX although filesystems have different maxima.  A
   similar thing is true for OPEN_MAX: the limit can be changed at
   runtime and therefore the macro must not be defined.  Remove this
   after including the header if necessary.  */  
#ifndef NR_OPEN
# define __undef_NR_OPEN
#endif
#ifndef LINK_MAX
# define __undef_LINK_MAX
#endif
#ifndef OPEN_MAX
# define __undef_OPEN_MAX
#endif
#ifndef ARG_MAX
# define __undef_ARG_MAX
#endif
/* We do not provide fixed values for 

   ARG_MAX      Maximum length of argument to the `exec' function
                including environment data.

   ATEXIT_MAX   Maximum number of functions that may be registered
                with `atexit'.

   CHILD_MAX    Maximum number of simultaneous processes per real
                user ID. 

   OPEN_MAX     Maximum number of files that one process can have open
                at anyone time.

   PAGESIZE
   PAGE_SIZE    Size of bytes of a page.

   PASS_MAX     Maximum number of significant bytes in a password.

   We only provide a fixed limit for

   IOV_MAX      Maximum number of `iovec' structures that one process has
                available for use with `readv' or writev'.

   if this is indeed fixed by the underlying system.
*/
/usr/include/i386 linux gnu/bits/xopen\u lim.h

# ifdef __GNU__
#  define PATH_MAX 4096
#  define MAXPATHLEN 4096
#  define OPEN_MAX 256 /* We define a reasonable limit.  */
# endif
/* The kernel header pollutes the namespace with the NR_OPEN symbol
   and defines LINK_MAX although filesystems have different maxima.  A
   similar thing is true for OPEN_MAX: the limit can be changed at
   runtime and therefore the macro must not be defined.  Remove this
   after including the header if necessary.  */  
#ifndef NR_OPEN
# define __undef_NR_OPEN
#endif
#ifndef LINK_MAX
# define __undef_LINK_MAX
#endif
#ifndef OPEN_MAX
# define __undef_OPEN_MAX
#endif
#ifndef ARG_MAX
# define __undef_ARG_MAX
#endif
/* We do not provide fixed values for 

   ARG_MAX      Maximum length of argument to the `exec' function
                including environment data.

   ATEXIT_MAX   Maximum number of functions that may be registered
                with `atexit'.

   CHILD_MAX    Maximum number of simultaneous processes per real
                user ID. 

   OPEN_MAX     Maximum number of files that one process can have open
                at anyone time.

   PAGESIZE
   PAGE_SIZE    Size of bytes of a page.

   PASS_MAX     Maximum number of significant bytes in a password.

   We only provide a fixed limit for

   IOV_MAX      Maximum number of `iovec' structures that one process has
                available for use with `readv' or writev'.

   if this is indeed fixed by the underlying system.
*/

另请参见
中定义的
FOPEN_MAX
——尽管@cste评论中链接的问题可能有更有用的信息。一个进程可以打开的文件数量的限制不一定是固定的。@KeithThompson我现在要求更多的是学习而不是应用。因此,没有常数
打开\u MAX
?@AshRj:取决于系统。在我的Ubuntu系统上,有评论在
/usr/include/i386 linux gnu/bits/local_lim.h
/usr/include/i386 linux gnu/bits/xopen_lim.h
@AshRj:
FOPEN_MAX
中讨论(缺少)OPEN_MAX
??它不应该是最大值吗?除非它指的是最大值的最小可能值。@AshRj:
FOPEN_MAX
承诺您至少可以打开那么多文件。@AshRj我不知道,这只是这个文件的内容。可能在您的系统上不同。@josegura在我的系统上是一样的。Keith所说的是正确的。当你强调系统时,系统与程序的运行实例有不同的限制吗?@AshRj:是的,每个进程也有限制,请参阅“ulimit-n”shell命令,或{get,set}RLIMIT()的RLIMIT_NOFILE。编辑以涵盖其他限制。