Teach you the limitations of modifying the maximum number of connections for high concurrent sockets in Linux

Source: Internet
Author: User
Tags unix domain socket

1, modify the user process can open the number of files limit

On a Linux platform, regardless of whether you write a client or a service-side program, the highest concurrency is limited by the number of files that can be opened by the system at the same time as the user's single process (because the system creates a socket handle for each TCP connection) for high concurrent TCP connection processing. Each socket handle is also a file handle. You can use the Ulimit command to view the limit on the number of files that the system allows the current user process to open: [[email protected] ~]$ ulimit-n1024 This means that each process of the current user allows up to 1024 files simultaneously. These 1024 files also have to remove each process must open the standard input, standard output, standard error, server listening socket, inter-Process communication UNIX domain socket and other files, then the remaining number of files available to the client socket connection is only about 1024-10 = About 1014. In other words, Linux-based communication programs allow up to 1014 simultaneous TCP connections by default. For a communication handler that wants to support a higher number of TCP concurrent connections, you must modify the soft limit (soft limit) and the hard limit (hardlimit) of the number of files that Linux has open simultaneously for the current user's process. The soft limit refers to the Linux in the current system can withstand the extent to further limit the number of files opened by the user at the same time; hard limits are the number of files that can be opened at the same time based on the system's Hardware resource status (mainly system memory). The soft limit is usually less than or equal to the hard limit. The simplest way to modify the above limitation is to use the ulimit command: [[email protected] ~]$ ulimit-n The above command, specify the maximum number of files to be set for the single process that is allowed to be opened. If the system echoes similar to "Operation Notpermitted", the above limitation modification fails, in effect because the value specified in is more than the soft or hard limit of the number of open files that the Linux system has on the user. Therefore, it is necessary to modify the Linux system's soft and hard limits on the number of open files to the user. The first step is to modify the/etc/security/limits.conf file and add the following line to the file: Speng soft nofile 10240speng hard nofile 10240 where Speng specifies the limit on the number of open files to modify which user Use the ' * ' sign to modify the restrictions for all users; soft or hard Specifies whether you want to modify the soft or rigid limit; 10240 Specifies the new limit value that you want to modify, that is, the maximum number of open files (note that the soft limit value is less than or equal to the hard limit). Save the text after you finish modifyingThing The second step, modify the/etc/pam.d/login file, add the following line to the file: Session required/lib/security/pam_limits.so This is to tell Linux that after the user completes the system login, should call PAM_ The Limits.so module sets the maximum limit on the number of resources that the system can use for the user, including the maximum number of files a user can open, and the pam_limits.so module from/etc/security/ The limits.conf file reads the configuration to set these throttling values. Save this file when you are finished modifying it. The third step is to view the maximum number of open files at the Linux system level, using the following command: [[email protected] ~]$ cat/proc/sys/fs/file-max12158 This indicates that the Linux system is allowed to open at most ( That is, the total number of open files for all users) 12,158 files, is the Linux system-level hard limit, all user-level open files limit should not exceed this number. Typically, this system-level hard limit is the best maximum number of simultaneous open file limits that Linux systems will calculate at startup, based on the state of the system's hardware resources, and should not be modified unless you want to set a value that exceeds this limit for the user-level open files limit. The way to modify this hard limit is to modify the/etc/rc.local script to add the following line to the script: Echo 22158 >/proc/sys/fs/file-max This is the hard limit set to 22158 for Linux to force the number of open files at the system level after boot is complete. Save this file when you are finished modifying it. By rebooting the system after completing the above steps, it is generally possible to set the maximum number of files that the Linux system can allow to open simultaneously for a single process of a specified user to the specified number. If you use the Ulimit-n command to view the number of files that the user can open after restarting, the limit is still lower than the maximum value set in the previous steps, possibly because using the ulimit-n command in User logon script/etc/profile limits the number of files that the user can open simultaneously. Because modifying the system through Ulimit-n can limit the maximum number of files that a user can open at the same time, the newly modified value is only less than or equal to the value of the last Ulimit-n setting, so it is not possible to use this command to increase the limit value. Therefore, if there is such a problem, you can only open the/etc/profile script file, find out whether the file is used Ulimit-n limit the maximum number of files that the user can open at the same time, if found, delete this line of command, or set its value to the appropriate value, and then save the file, The user exits and logs back in to the system. Through the above steps,A system restriction on the number of open files is lifted for communication handlers that support high concurrent TCP connection processing.

Teaches you to modify the limitations of the maximum number of high concurrent socket connections under Linux

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.