what is the maximum of number of files c fopen
can open at the same time in Linux?

- 4,833
- 3
- 26
- 40

- 507
- 1
- 3
- 15
-
Short and sweet. It depends. – Nik Bougalis Jul 29 '13 at 18:34
-
5Depends on how many files other programs have open, and on the hard-coded kernel limit, as well as various `ulimit`s. – Drew McGowen Jul 29 '13 at 18:34
-
1RTFM: http://www.linuxhowtos.org/Tips%20and%20Tricks/ulimit.htm – Marc B Jul 29 '13 at 18:35
-
1`FOPEN_MAX` – BLUEPIXY Jul 29 '13 at 18:37
4 Answers
The implementation is required to provide FOPEN_MAX
in <stdio.h>
. This is the minimum number of files the implementation guarantees can be opened simultaneously. You might be able to open more than that, but the only way to know that is to test.
Note that the kernel limit is separate from this -- that tells you how many files you can (potentially) open with open
, creat
and other OS calls. The standard library of your C implementation can (and often will) impose its own limit (e.g., by statically allocating an array of FILE
). In theory, the largest number you can open is the minimum of the limit imposed by the kernel and by the library implementation -- but the kernel's limit is almost always (much) higher.
Generally speaking, if you care about this, you're probably doing something wrong though.

- 476,176
- 80
- 629
- 1,111
-
One use case of this limit is to implement remote file operation RPC, where you need to differentiate the fd of the local machine and the fd of the remote machine. – yeshengm Jan 24 '19 at 04:50
You can see the maximum allowed open files (kernel limit) by doing:
cat /proc/sys/fs/file-max
The value in file-max denotes the maximum number of file- handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.

- 27,144
- 16
- 62
- 82
-
4
-
3FOPEN_MAX isn't a limit. It gives you the *minimum number of streams that the implementation guarantees can be open simultaneously* – jh314 Jul 29 '13 at 18:45
This code should tell the max on your machine. Create a file "test" in the same folder and run it. It basically keeps opening the file until it cannot anymore.
# 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);
}
}

- 519
- 5
- 16
It is defined by POSIX standard. Removing it causes portability problems.Additionally, this macro is mentioned in glibc.info (at least in redhat-7.1).Please refer the below link OPEN_MAX not defined in limits.h.

- 57,103
- 20
- 141
- 208

- 1,149
- 1
- 6
- 19