当前位置:网站首页>File descriptorfile description
File descriptorfile description
2022-06-13 05:00:00 【Clown clown clown】
List of articles
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.
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 .
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 .
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 .
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? ?
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
therefore stdout The redirection is still implemented with fd To achieve .( After all, it just encapsulates the system call interface )
边栏推荐
猜你喜欢
Reductive elimination
使用EasyDarwin+FFmpeg实现rtsp推流
Several methods of identifying equivalent circuit of circuit drawing
Chapter 18 pagination: Introduction
Hidden implementation and decoupling, knowing Pimpl mode
[LeetCode]-二分查找
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
Mind mapping series - Database
Section 5 - Operator details
metaRTC4.0集成ffmpeg编译
随机推荐
C language learning log 10.11
LeetCode第297场周赛(20220612)
[JS solution] leedcode 200 Number of islands
Advantages of win8.1 and win10
[leetcode]- binary search
Clause 32: moving objects into closures using initialization capture objects
On switch() case statement in C language
135. distribute candy
Design system based on MVC using javeswingjdbc
How to lay copper in AD (aluminum designer)
Solution to sudden font change in word document editing
关于匿名内部类
C language learning log 10.10
Trust programming - linked lists: use struct to implement linked lists, use heap to merge K ascending linked lists, and customize display
Chapter 17 free space management
Analysis of scoped attribute principle and depth action selector
QT realizes message sending and file transmission between client and server
Analysis on the similarities and differences of MVC, MVP and mvvc
Cesium:cesiumlab makes image slices and loads slices
Chapter 18 pagination: Introduction