Notes about the Linux file descriptor

Source: Internet
Author: User

When a program opens a file, the operating system returns the corresponding file descriptor, which must be referenced by the program in order to process the file. The so-called file descriptor is a low-level positive integer. The first three file descriptors (0,1,2) correspond to standard input (stdin), standard output (stdout), and standard error (STDERR), respectively. Therefore, the function scanf () uses stdin, and the function printf () uses STDOUT. You can overwrite the default settings with different file descriptors and redirect the process's I/O to different files.

  1. First, what is a file descriptor and what does it do? The

    file descriptor is a simple integer that identifies each file and socket that is opened by the process. The first open file is 0, the second one is 1, and so on. Unix operating systems typically impose a limit on the number of files each process can open. What's more, UNIX usually has a system-level limitation.

    Because of how squid works, the limitations of file descriptors can have a significant impact on performance. When squid runs out of all file descriptors, it cannot receive new connections from the user. That is, the use of the file descriptor results in a denial of service. Until a portion of the current request is completed, the corresponding file and socket are closed and squid cannot receive new requests. When Squid discovers a shortage of file descriptors, it publishes a warning.

    Before you run the./configure, check that your system's file descriptor limits are appropriate and can help you avoid some problems. In most cases, 1024 file descriptors are sufficient. A very busy cache may take 4096 or more. When configuring file descriptor limits, it is recommended to set the number of system-level limits to twice times the limit for each process.


  2. Some description of the file descriptor.

    A file descriptor is a small positive integer that is an index value that points to the record table in which the kernel opens a file for each process maintained by the process. In programming, some of the underlying programming often revolves around file descriptors. However, the concept of file descriptors is often applied only to operating systems such as UNIX and Linux.

    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.

    advantages of using file descriptors: File descriptor-based I/O operations are compatible with POSIX standards. in UNIX, Linux system calls, a large number of system calls are dependent on the file descriptor.
    On the Linux-series operating system, Linux is designed to treat everything as a file. Therefore, file Description identifier provides a unified approach to device-related programming on this series of platforms in practice.

    Disadvantages of using file descriptors: on non-unix/linux operating systems (such as Windows NT), it is not possible to program based on this concept.
    Because the file descriptor is just an integer in form, when the amount of code increases, it makes it difficult for programmers to tell which integers mean the data and which means the file descriptor. As a result, the readability of the finished code becomes very poor.


  3. Some actions related to file descriptors

    File Descriptor Generation Open (), Open64 (), creat (), Creat64 (), socket (), Socketpair (), pipe ()

    actions related to single file descriptor read (), write (), recv (), Send (), recvmsg (), sendmsg (), Sendfile (), Lseek (), lseek64 (), Fstat (), Fstat64 (), Fchmod (), Fchown ( )

    operations related to complex file descriptors select (), Pselect (), poll ()

    actions related to file Descriptor descriptor Close (), DUP (), dup2 (), Fcntl (F_DUPFD), Fcntl (F_GETFD and F_SETFD)

    Action to change the state of the process Fchdir (), mmap ()

    Operation with file lock flock (), Fcntl (F_getlk, F_setlk and F_SETLKW), LOCKF ()

    Operations associated with sockets connect (), bind (), listen (), accept (), getsockname (), Getpeername (), getsockopt (), setsockopt (), Shutdown ()



Notes about the Linux 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.