当前位置:网站首页>File descriptorfile description

File descriptorfile description

2022-06-13 05:00:00 Clown clown clown

File descriptor

File descriptor concept

When we want to open the file , The file is loaded into memory , At this point, the file is managed by the process . The process of file management is also using data structure management . This data structure is called files_struct


task_struct There is a pointer in it , Point to files_struct.
 Insert picture description here
and files_struct There is one called fd_arrays Things that are , The index of this array is the file descriptor fd. This array contains file pointers file*, You can point to a file , Management document .
 Insert picture description here

So in general :fd Is the subscript of an array .

How to use the process fd Management document

It looks something like this :
pcb There's a struct files_struct* files The pointer , Point to files_struct( Structure of management file ),files_struct There's a fd_arrays There's... In it files The file pointer , Point to a file in memory .
 Insert picture description here
among fd_arrays[0] Point to stdin ,fd_arrays[1] Point to stdout, fd_arrays[2] Point to stderr. These are the three standard stream files automatically opened by the system
They are directly related to the principle of redirection

File descriptor allocation rules

fd The allocation rules for are simple , Is linear .
Idle ones with small subscripts are assigned to make a file fd.

for instance :0,1,2 It's occupied ,3 Is free , So the next file to be managed fd Namely 3.

Again for instance 0,2,3, It's occupied ,1 Is free , So the next file to be managed fd Namely 1

Redirect

1 Number fd The original point is stdout, I.e. display screen . To implement redirection , Just put 1 Number fd Point to the file you want to output to .

The following code can redirect . Turn off first stdout( The so-called turn off just makes fd_arrays[1] = nullptr nothing more ), And then open the file , according to fd Distribution rules , here 1 Number fd Yes tmp This file , Therefore, the goal of redirection is achieved .
 Insert picture description here
notes : Finally, we must fflush once .

 Buffer refresh rule for output to display : Row refresh / Program end refresh /fflush
 Buffer flush rule for output to file : Full refresh ( Refresh when full )/fflush

If not fflush, The string is still in the buffer , It has not been refreshed into the file .

Some questions about redirection

Look at this code :
It's shutting down 1 Number fd when , Use at the same time write System call interface ,fprintf and printf such c Library function , Redirection can also be implemented , Why? ?
 Insert picture description here
Say first conclusion :stdout In essence c It seems , Namely fd = 1.

reason : We know ,stdout when FILE* Type of ,FILE yes c The structure in the library , It's a language thing .(fd It's a system level thing ).

We can have a look c How libraries are defined FILE Of
stay /usr/include/libio.h Next
 Insert picture description here
therefore stdout The redirection is still implemented with fd To achieve .( After all, it just encapsulates the system call interface )

原网站

版权声明
本文为[Clown clown clown]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280516403628.html