8

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

Alexej Magura
  • 4,833
  • 3
  • 26
  • 40
Pavan
  • 507
  • 1
  • 3
  • 15

4 Answers4

11

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.

Jerry Coffin
  • 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
3

You can see the maximum allowed open files (kernel limit) by doing:

cat /proc/sys/fs/file-max

Quote from kernel docs:

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.

jh314
  • 27,144
  • 16
  • 62
  • 82
2

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);
  }
}
Jonath P
  • 519
  • 5
  • 16
1

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.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Raju
  • 1,149
  • 1
  • 6
  • 19