Too delimiter open files-Restrictions on Linux file descriptors

Source: Internet
Author: User

During the two days of Asterisk performance testing, we often encounter the following error: "Too program open files ".

We know that in Linux, the file descriptor is a simple integer. Traditionally, the file descriptor of the standard input is 0, and the standard output is 1, the standard error is 2. POSIX defines stdin_fileno, stdout_fileno, and stderr_fileno to replace 0, 1, and 2. The definitions of these three symbolic constants are located in the header file unistd. h.

The valid range of the file descriptor is 0 to open_max. How many file descriptors can a process open at most? The following describes the asterisk process as an example.


# Ps-Ef | grep Asterisk


Obtain the asterisk PID from the second column as 19488.

# Cat/proc/19488/limits

A red line indicates that the asterisk process can open up to 1024 file descriptors (excluding its sub-processes or created threads ). The subtasks under the/proc/19488/task/directory are listed in detail. Each subfolder also contains a limits file, which limits the subtasks.

 

How many file descriptors does a process open?

# Ll/proc/19488/FD/


Each file descriptor opened by a process is listed in detail in the subdirectory FD. Similarly, file descriptors opened by a sub-task may exist in/proc/19488/task/xxxx/FD. You need to know a few, execute

# Ll/proc/19488/FD/| WC-l

 

How do I know which files are associated with a process and its sub-processes? Lsof can do this. Note that the associated file and open file descriptor are two different concepts. The number of associated files may be much larger than the number of opened file descriptors.

# Lsof | grep asterisk | WC-l

You can also use the PID of the parent process to filter data.

# Lsof | grep 19488 | WC-l

The value I get here is 9525.

 

How can I modify the limits of file descriptors? Temporary modification. You can use ulimit.

# Ulimit-shn2048

 

However, this only affects the current Session. When the terminal reconnects or the current user exits, the configuration becomes invalid. To modify the/etc/security/limits. conf file permanently, add the following two lines:
* Hard nofile 2048
* Soft nofile 2048

 

The kernel parameters also have restrictions on file descriptors. If the value is set to be greater than the kernel limit, it is not possible:

Find the kernel parameters of file-MAX:

# Sysctl-A | grep file-max

Change the kernel parameters of file-MAX:

# Sysctl-W file-max= 65535

Sysctl is temporary. To take effect permanently, you can change the sysctl file, edit the/etc/sysctl. conf file, and add or modify the following line:

FS. File-max = 65535

 

Note that the restrictions on file descriptors are not limited to the ones described here, but may also be related to the startup parameters of the process and the user's environment settings. Of course, if the file descriptor is not closed in time due to a process bug, this increase limit is only a cure, and the bug must be fixed.

In addition, lsof lists the resources occupied by the system, but these resources do not necessarily occupy open file descriptors (such as shared memory, semaphores, message queues, and memory ing. and so on.
The/proc/sys/fs/file-max value is smaller than lsof | WC-L. Asterisk itself provides a STARTUP script named safe_asterisk, which configures the file descriptor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.