When deploying applications under Linux, there are times when you encounter socket/File:can ' t open so many files problems, such as squid do agent, when the file opened to more than 900 speed can be very fast decline, it is possible to open the page. Linux is a file handle limit, and Linux default is not very high, generally 1024, the production server is actually very easy to reach this number. View method we can use Ulimit.-A to view all limit values [email protected]~]# Ulimit-acore File Size (blocks,-c)0Data seg Size (Kbytes,-d) Unlimitedmax Nice (-e)0file Size (blocks,-f) unlimitedpending signals (-I.)4096Max locked Memory (Kbytes,-L) +max memory Size (Kbytes,-m) unlimitedopen files (-N)1024x768Pipe Size ( +Bytes,-p)8POSIX message queues (bytes,-Q)819200Max RT Priority (-R)0stack size (Kbytes,-s)10240CPU time (seconds,-t) Unlimitedmax user processes (-u)4096VirtualMemory (Kbytes,-v) unlimitedfile locks (-X) unlimited| | <which"open Files (-N) 1024x768"is the limit on the number of file handles opened by the Linux operating system to a process (also includes the number of open sockets, which can affect the number of concurrent connections to MySQL). This value can be modified with the Ulimit command, However, the value modified by the Ulimit command is valid only for the currently logged-on user's current environment, the system restarts, or the user exits. The total system limit is here,/proc/sys/fs/file-max. You can view the current value by cat and modify the/etc/sysctl.conf can also be controlled. There's another one,/proc/sys/fs/file-NR, you can see the total number of file handles currently used by the system to find file handle problems, there is a very useful program lsof. It is convenient to see that a process has opened those handles. You can also see a file/the directory is occupied by what process. Modify method to make the value of the modified Ulimits permanent, you must modify the configuration document to put the Ulimit modify command into/etc/profile inside, this method is really inconvenient, there is a way to modify the/etc/sysctl.conf. I have modified, tested, but the user's ulimits-a will not change. Just/proc/sys/fs/file-.Max's value has changed. (I failed to put the command in rc.local) I think the right approach should be to modify/etc/security/There are very detailed comments in the limits.conf, such as:* Soft Nofile32768* Hard Nofile65536You can also run the following shell directly. Just copy it to your terminal and run it. Echo-ne"* Soft Nofile65536* Hard Nofile65536">>/etc/security/limits.confYou can change the file handle limit to soft 32768, Hard 65536. The front of the configuration file refers to domain, which is set to the asterisk to represent the global, and you can also make different restrictions for different users. The hard limit in this is the actual limit, and the soft limit, which is the warnning limit, will only make warning. In fact, the Ulimit command itself has a soft and hard setting, Plus-H is hard, plus-S is the soft default display is the soft limit, if you run the Ulimit command changes when not added, it is two parameters together. You can see it when you're done with the changes and re-login. (My system is Centos5. )1. Modified, re-login to take effect immediately. You can use Ulimit-a view confirmation.) How limits.conf works: The back end of limits.conf works like this: Limits.conf is the pam_limits.so configuration file, and thenThe pam_***.so module is called by the application under/etc/pam.d/. For example, when a user accesses a server, the service program sends the request to the PAM module, and the PAM module is/etc/based on the service nameSelect a corresponding service file under the PAM.D directory, and then select the specific PAM module to process according to the contents of the service file. Example: Limit the Admin user login to sshd service cannot exceed 2/etc/pam.d/Add session required pam_limits.so in sshdAdd Admin-maxlogins in/etc/security/limits.conf2to see if an application can be supported by Pam, the parameters of the LDD limits.conf file limits.conf file may be set to one of the following two forms: temporarily, for use with the Ulimit command to log in to the shell session. Permanently, by adding a corresponding Ulimit statement to one of the files read by the login shell (for example ,~/.profile), which is a shell-specific user resource file, or by editing the/etc/security/limits.conf. 1. Core-limit the size of the kernel file what is a core file, and when a program crashes, the stored image of the process is copied in the core file of the current working directory of the process. The core file is simply a memory image (plus debugging information), which is used primarily for debugging purposes. The core file is a binary file and requires a tool to parse the memory image of the program when it crashes. The system default core file size is 0, so it is not created. You can use the Ulimit command to view and modify the size of the core file. $ulimit-C0$ ulimit-C +$ ulimit-C +-c Specifies the size of the core file to be modified, and 1000 specifies the core file size. You can also make no restrictions on the size of the core file, such as: # Ulimit-C Unlimited #ulimit-C Unlimited If you want the modification to take effect permanently, you need to modify the configuration file, such as. Bash_profile,/etc/profile or/etc/security/limits.conf. 2. Nofile-Maximum number of open files for applications that require many socket connections and leave them open, it is best to set the number of file descriptors higher than the default value by using Ulimit–n or by setting the Nofile parameter.
Linux---Process handle limit Summary
Influenced by many posts on the Internet, has been that UNIX, Linux under a single process open handle number limit is 65535, can not exceed this value, recently from the Internet to see another description, in SuSE, Redhat have tested, a single process can fully support to more than 1 million of the handle.
Several restrictions on system file handles are arranged from large to small:
/proc/sys/fs/nr_open
The system file system supports a maximum number of file handles, the default value is 1048576 (1M), and Linux2.6.25 begins to increase the kernel parameter to replace the kernel macro nr_open (1048576), which is limited by system memory.
"Man 5 proc" does not see "/proc/sys/fs/nr_open" on the Linux 2.26.32 platform, only nr_open parameter limit/proc/sys/fs/file-max is seen.
/proc/sys/fs/file-max
The system file system supports the maximum total number of file handles, which must be less than/proc/sys/fs/nr_open or nr_open, and when this value is added, you must synchronize the modification/proc/sys/fs/inode-max = 4*/proc/sys/fs/file-max.
Ulimit-hn
A single process handle number hard upper limit, the default value of 1024, cannot be greater than/proc/sys/fs/file-max, modify the method not to speak more.
Ulimit-hn
The soft upper limit of a single process handle, the default value of 1024, can not be greater than the "ULIMIT-HN" hard upper limit, modify the method not much to say.
Fd_setsize
Linux environment a C-language macro definition, in/usr/include/bits/typesize.h or has a definition, the default value of 1024, so far, I only confirm that the macro defines the limit fd_set (), FD_CLR () operation of the file descriptor value, Guessing should be a limitation left over from earlier versions of Linux. Since the Select () operation is obsolete, this macro definition should also be useless. If you want to use the Select () series operation, you can define the macro "-dfd_setsize=65535" at GCC compile time.
To manually modify your operating system handle, modify the configuration as follows:
Operating system |
View handle Number method |
To modify the handle Count method |
linux |
ulimit-sn P align= "left"---view handle number soft limit ulimit-hn |
check/etc/security/limits.conf file, modify or add the following configuration: * hard nofile 65536 * soft nofile 65535 session required pam_limits.so |
Hp |
Kctune-q Maxfiles ---View handle number soft limit Kctune-q Maxfiles_lim ---View handle number hard limit |
To perform a command modification: Kctune maxfiles>=xxx XXX range "32,maxfiles_lim", Maxfiles_lim value Range "32,1048576", but more than 65535, some older versions of file functions are incompatible. After the Maxfiles is modified, the operating system must be restarted. |
Aix |
Ulimit-sn ---View handle number soft limit Ulimit-hn ---View handle number hard limit |
Check the/etc/security/limits file and add the following configuration: Nofiles =-1 Nofiles_hard=-1 In AIX6 test validation: Even if configured as ulimited, a single process can only open 65,535 handles. |
Solaris |
Ulimit-sn ---View handle number soft limit Ulimit-hn ---View handle number hard limit |
Check the/etc/system file and add the following configuration: rlim_fd_max=65535 rlim_fd_cur=65535 After modification, the machine needs to be restarted to take effect. |
limit-Process Handle Limit