Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Right now, I know that:

  • Find open files limit per process: ulimit -n
  • Count all opened files by all process: lsof | wc -l
  • Get maximum open files count allowd: cat /proc/sys/fs/file-max

My question here is that: Why there would be a limit of open files in Linux?

share|improve this question
6  
Because of fork bombs –  Rob Apr 19 '12 at 14:19
    
@Rob Googled a little and find that it's a fork bomb, can it be used to explain open file limit? –  xanpeng Apr 19 '12 at 17:02
    
Well, process limits and file limits are important so things like fork bombs don't break a server/computer for all users, only the user that does it and only temporarily. Otherwise, someone on a shared server could set off a forkbomb and completely knock it down for all users, not just themselves. –  Rob Apr 20 '12 at 23:30

3 Answers 3

up vote 21 down vote accepted

The reason is that the operating system needs memory to manage each open file, and memory is a limited resource - especially on embedded systems.

As root user you can change the maximum of the open files count per process (via ulimit -n) and per system (e.g. echo 800000 > /proc/sys/fs/file-max).

share|improve this answer
6  
There is also a security reason : if there were no limits, a userland software would be able to create files endlessly until the server goes down. –  Coren Apr 19 '12 at 14:44
1  
@Coren The here discussed limits are only for the count of open file handlers. As a program can also close file handlers, it could create as many files and as big as it want, until all available disk space is full. To prevent this, you can use disk quotas or separated partitions. You are true in the sense, that one aspect of security is preventing resource exhaustion - and for this there are limits. –  jofel Apr 19 '12 at 15:23
    
@jofel Thanks. I guess that opened file handles are represented by instances of struct file, and size of this struct is quite small (bytes level), so can I set /.../file-max with a quite big value as long as memory is not used up? –  xanpeng Apr 19 '12 at 16:12
2  
@xanpeng I am not a kernel expert, but as far as I can see, the default for file-max seems to be RAM size divided by 10k. As the real memory used per file handler should be much smaller (size of struct file plus some driver dependent memory), this seems a quite conservative limit. –  jofel Apr 19 '12 at 16:43

I think it's largely for historical reasons.

A Unix file descriptor is a small int value, returned by functions like open and creat, and passed to read, write, close, and so forth.

At least in early versions of Unix, a file descriptor was simply an index into a fixed-size per-process array of structures, where each structure contains information about an open file. If I recall correctly, some early systems limited the size of this table to 20 or so.

More modern systems have higher limits, but have kept the same general scheme, largely out of inertia.

share|improve this answer
    
20 was the Solaris limit for C language FILE data structures. The file handle count was always larger. –  Lothar Sep 14 at 3:29
    
@Lothar: Interesting. I wonder why the limits would differ. Given the fileno and fdopen functions I'd expect them to be nearly interchangeable. –  Keith Thompson Sep 14 at 3:37

Please note that lsof | wc -l sums up a lot of duplicated entries (forked processes can share file handles etc). That number could be much higher than the limit set in /proc/sys/fs/file-max.

To get the current number of open files from the Linux kernel point of view, do this: cat /proc/sys/file/file-nr

Example: This server has 40096 out of max 65536 open files, although lsof reports a much larger number:

# cat /proc/sys/fs/file-max
65536
# cat /proc/sys/fs/file-nr 
40096   0       65536
# lsof | wc -l
521504
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.