[Profiling]linux File descriptor

Source: Internet
Author: User

1 , what are file and file descriptors

Linux files can be divided into 4 types: normal files, directory files, linked files and device files.
1. Common documents
Are the most frequently used files for users, including text files, shell scripts, binary executables, and various types of data.
LS-LH to see the properties of a file, you can see a similar-rw-r--r--, it is worth noting that the first symbol is-, such a file in Linux is a normal file. These files are typically created with some related applications, like tools, document tools, archive tools .... or CP tools. This type of file is deleted using the RM command;
2. Catalog files
In Linux, directories are also files, which are pointers to file names and subdirectories, and to those files and subdirectories
When we execute in a directory, see similar drwxr-xr-x, such files are directories, directories in Linux is a more special file. Note that its first character is d. The command to create a directory can be used with the mkdir command, or the CP command, where CP can copy a directory to another directory. Remove the RM or RMDIR command.
3. Link file
A linked file is similar to a "shortcut" in Windows.
is created by ln-s a new file name for the source file name.
4. Equipment files
Includes two types of block device files, and the other is a character device file. Character devices (unbuffered and only sequential access), block devices (buffered and can be accessed randomly).
A block device file refers to the reading and writing of data, which is a block-based device, such as a hard drive.
The character device mainly refers to the serial port interface device, such as network card.

The kernel (kernel) accesses files using file descriptors (descriptor). The file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor. Read-write files also need to use file descriptors to specify which files to read and write.

Traditionally, standard input has a file descriptor of 0, standard output is 1, and standard error is 2. Although this habit is not characteristic of the Unix kernel, it is used by some shell and many applications, so if the kernel does not follow this habit, many applications will not be able to use it. The valid range of file descriptors is 0 to Open_max.

On the Windows operating system, file descriptors are referred to as file handles.

2 , the effect of file descriptors on performance

Linux server kernel has a limit on the number of file descriptors, the default is around 1024 (depending on the server type, the default value is not the same), this is not too busy programs, generally enough, but if it is a Web server or performance testing with a pressurized server, this limit is not enough, The program opens a large number of files, causing the file descriptor to be insufficient to allow access to the new file.

3 , how to tell if there is a problem with file descriptor settings

By viewing the file descriptor used by the yourselves system, you can determine if the number of file descriptors is limited

Using CAT/PROC/SYS/FS/FILE-NR

[Email protected] bin]# CAT/PROC/SYS/FS/FILE-NR

5664 0 186405

The first number indicates the number of open file descriptors that the current system has been assigned to use, the second number is freed after allocation (no longer used), and the third number equals File-max.

, when a large number of concurrent requests, the current system has been allocated to use the number of open file descriptors will change greatly, the default of 1024 is not enough.

4 , how to modify Linux file descriptor for server kernel

(1) Temporary modification, can be modified by Ulimit command

Displays the current file descriptor

Ulimit-n

Modify the current user environment with a file descriptor of 65536

ULIMIT-HSN 65536

Disadvantages of using the Ulimit command:

1, can only modify the current login user environment of the file descriptor, if this user to open a separate connection, this link environment file descriptor is still not changed before

2, if the system restarts, the previous modifications are no longer valid

(2) Permanent modification, by modifying the NF file

Edit the/etc/security/nf file and add the following two lines at the end

* Soft Nofile 65536

* Hard Nofile 65536

Save exit, do not need to restart the server, directly re-login with ulimit-n can see the effect

This way, no matter which user you use, no restart will fail.

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.