在Linux中,fopen
可以同时打开的文件数的最大值是多少?
<stdio.h>
中提供 FOPEN_MAX
。这是实现保证能够同时打开的文件数的最小值。你可能会打开更多文件,但唯一确定的方法是进行测试。open
、creat
和其他操作系统调用(潜在地)打开多少文件。您的 C 实现标准库可以(而且通常会)强制执行其自己的限制,例如通过静态分配一个 FILE
数组。理论上,您可以打开的最大数量是内核和库实现强制执行的限制的最小值,但内核的限制几乎总是(远远)更高。您可以通过以下方式查看操作系统内核允许的最大打开文件数:
cat /proc/sys/fs/file-max
file-max 的值表示 Linux 内核将分配的最大文件句柄数。当您收到大量有关文件句柄耗尽的错误消息时,您可能需要增加此限制。
ulimit -n
- nullpotent这段代码应该能告诉你的机器最大可用内存。在相同的文件夹中创建一个名为“test”的文件并运行它。它基本上会一直打开该文件,直到无法再打开为止。
# include <assert.h>
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/wait.h>
# include <string.h>
# include <fcntl.h>
int main(){
int t;
for(;;){
t = open("test", O_RDONLY);
if (t < 0){
perror("open");
exit(1);
}
printf("%d: ok\n", t);
}
}
ulimit
设置。 - Drew McGowenFOPEN_MAX
- BLUEPIXY