Detailed explanation of file descriptor will give you a thorough understanding of the computer system!

Detailed explanation of file descriptor will give you a thorough understanding of the computer system!

File descriptor is one of the most basic and commonly used concepts in computer systems, but it may not be easy for beginners to understand. In this article, we will comprehensively analyze file descriptors for you, and deeply explore how file descriptors work from the perspectives of principles, applications, and practices.

What is a file descriptor?

A file descriptor is an integer related to an input/output resource, and may also be called a file handle, a file pointer, or a file reference. Simply put, it is an index in a table maintained by the operating system to manage I/O operations, and represents a "house number" of a file opened in the system.

This table is often called a file descriptor table, where each entry describes an open file and contains the file type, read and write locations, and other status information. We can reference these open files through file descriptors and perform file read and write operations.

Classification of file descriptors

In Unix/Linux systems, file descriptors are generally divided into three categories:

  • Standard input (STDIN_FILENO): The default is 0, which represents the standard input stream of the program.
  • Standard output (STDOUT_FILENO): The default is 1, which represents the standard output stream of the program.
  • Standard error (STDERR_FILENO): The default is 2, which represents the standard error output stream of the program.

When a program is running, file descriptors are inherited when the process starts, so they are usually used to perform I/O operations, including reading, writing files, and printing output.

Application of file descriptors

  • File operations:

In Unix/Linux systems, we use the open() function to open a file and get the file descriptor. Files can be read and written through the read() and write() functions, the file read and write pointer can be moved using the lseek() function, and the fcntl() function is used to control file attributes, etc.

  • Process control:

Communication between processes requires the use of the inter-process communication mechanism (IPC), most of which are based on file descriptors. For example, pipe can be used for unnamed pipe communication between processes, and socketpair() can create a pair of connected sockets so that processes can communicate.

  • network programming:

In network programming, the socket descriptor returned by the socket function is a file descriptor. We can write data to or read data from the socket file through the file descriptor, and through the read() and write() functions.

File descriptor practices

Consider the following code:

#include <stdio.h>
#include <fcntl.h>

int main()
{
    int fd; 
    char c;
    fd = open("file.txt", O_RDONLY); // 打开文件
    while(read(fd, &c, 1) == 1) // 读文件内容到字符数组中
    {
        printf("%c", c); // 打印字符数组
    }
    close(fd); // 关闭文件
    return 0;
}

In this program, we open the file "file.txt", get the file descriptor and save it in fd. Then iterate through each character of the file through a while loop, read the characters into a character array using the read() function, and print them out using the printf() function. Finally, use the close() function to close the file.

Running the above code can print out the contents of the file "file.txt" to prove the correctness of the file descriptor.

Summarize

In this article, we introduce the basic concepts of file descriptors, their classification, applications, and practices. File descriptors are one of the most basic and commonly used concepts in computer systems and are of great significance for understanding I/O operations and process communication. At the same time, in actual programming, mastering the operation of file descriptors is also a very important skill.

Guess you like

Origin blog.csdn.net/m0_72410588/article/details/133001278