Linux - file descriptor (fd) and redirection, dup/dup2

Table of contents

1. File descriptor

(1). Meaning

(2). Use

(3).Standard input/output/error

2. Redirection and dup/dup2

(1) .dup/dup2 

(2). Redirection 


1. File descriptor

(1). Meaning

The file descriptor (file descriptor) is called fd for short, and its essence is an array subscript

When creating a process, the operating system will not only create a task_struct structure (PCB process control block), but also create a files_struct structure to represent the related files opened by the process. There is a pointer in the process control block that will point to the files_struct.

 Inside the files_struct structure, there is an array of structure pointers fd_array. fd_array is an array, which stores pointers, and each pointer points to a file structure.

The file descriptor fd is the array subscript of fd_array.

(2). Use

Take the fwrite function in C language as an example. Inside fwrite, the file descriptor fd is found through the FILE structure, and at the same time, the system interface write is called.

According to the process of calling write, the operating system finds the files_struct through the internal pointer of the task_struct structure, and then finds the corresponding file structure through the fd_array pointer array in the files_struct according to the specific fd value (subscript) passed in by write, and then finds relevant documents.

The legend is as follows:

When a process opens a file, the system will search for a pointer (null pointer) that has not yet pointed to a specific file in the fd_array starting from subscript 0 , and give the file structure of the file to this pointer. 

When a file is closed, the relevant pointer in fd_array will point to NULL. When other files are opened, find the NULL pointer and let it point to the new file by starting from 0 to find the NULL pointer.

(3).Standard input/output/error

name hardware FILE* fd
standard input keyboard stdin 0
standard output monitor stdout 1
standard error monitor stderr 2

Taking the C language as an example, three files (two hardware: keyboard and monitor) have been opened in advance in the <stdio.h> header file.

That is, standard input, standard output, and standard error. According to the allocation rules of fd_array to file descriptors, starting from subscript 0, point to specific files in turn. This is why the fd of standard input, standard output, and standard error are 0, 1, and 2 by default.

Therefore, the following code is easy to understand:

int main()
{
	const char* str = "hello world\n";
	fwrite(str, strlen(str), 1, stdout);
	return 0;
}

stdout is the FILE pointer defined by the <stdio.h> header file to represent the display. The internal fd value of the FILE structure pointed to by the pointer is 1, so fwrite directly obtains the relevant fd through the stdout pointer, and then uses the display.

2. Redirection and dup/dup2

With an understanding of file descriptors, the concept of redirection is well understood.

(1) .dup/dup2 

This replacement can be done through the system interface dup2.

Regardless of whether it is dup or dup2, because it is a system interface, its parameters are all file descriptors fd. 

dup can return a new fd to represent the file represented by the incoming oldfd, that is, give the file another fd.

dup2 can give the file represented by oldfd to newfd. If the files represented by the two fds are already equal, no changes will be made.

When using dup2, it is often easy to mistake the direction of copying. It can be understood as follows: dup2(copy from, copy to)

 If the call fails, both return -1.

(2). Redirection 

The essence of redirection is to make the pointer to the original file in fd_array point to the target file

 Take standard output redirection as an example:

[cdl@VM-16-9-centos ~]$ echo "hello world" > file.txt

The essence is to point the pointer to the display file in fd_array to the file structure of file.txt, namely:

FILE* file = fopen("./file.txt", ...);
dup2(file._fileno, 1);

echo still gives the data to the file pointed to by the pointer of fd=1, but because the pointer of fd=1 points to file.txt (the file structure), echo gives the data to file.txt. 

The diagram is as follows:

Adding redirection is to give the file additional attributes when opening the file.txt file, which is equivalent to this:

//系统open接口,O_TRUNC:按追加方式打开文件
int fd = open("./file.txt", O_CREAT | O_RDWR | O_TRUNC);

Fools confuse, wise men ask questions - Benjamin Disraeli


Please correct me if there is any mistake

Guess you like

Origin blog.csdn.net/weixin_61857742/article/details/127835592