当前位置:网站首页>The underlying principle of file operation (inode and hard and soft links, time attributes of files)
The underlying principle of file operation (inode and hard and soft links, time attributes of files)
2022-07-19 11:46:00 【Selling lonely little boys】
List of articles
zero . Preface
In the previous section, we learned the operation when the file has been loaded into memory , So how does the operating system find files on disk and load them into memory ? This section will cover this .
One 、 disk
Understand the storage of files when they are not opened , We need to know the hardware that stores it : disk .
In computers, disks belong to the category of peripherals , It is a mechanical device inside the computer .
Disks are divided into mechanical disks and solid-state disks , At present, most laptops are loaded with solid-state disks .
The basic unit of disk is sector , Each sector probably stores 512k The amount of data .
But when we understand disk, we should think of it as a strip structure . That is to regard the circular disk as a rectangle .
meanwhile , We also divide the long disk into areas of different sizes , It's called C disc ,D disc ,E disc .
Most computers have only one disk , Just divide it into different areas .
meanwhile , Because each disk has a large space , Therefore, we need to continue to divide management .
We will D The disk is divided into many groups for management , Its group head has a BootBlock This is related to startup .
And we have to divide and manage each group , We :
among SuperBlock Is related to the file structure and GroupDescriptorTable Is group related . But these two are not the focus of this article .
The focus of this article is to understand BlockBitmap,inodeBitmap,inodeTable and DataBlocks.
Two 、 File attributes and data storage
1.inodeTable And Datablocks
When we store a file , We should not only store its attribute information, but also store its data information . among InodeTable And Datablocks The attribute information and data information of the file are stored separately .
stay InodeTable There are many small pieces , Every small piece is one inode. It stores the attribute information of a file .
stay Datablocks neutralization inodeTable It's similar , It contains a lot of block unit , every last block The unit stores information of a certain size :
The operating system uses inode Number to find , Not by file name , The file name is only used by the user layer .Linux The system really identifies a file , It's through inode Number . Every file has a inode Number . At the same time inode The file corresponding to block Number in block .
If it's a catalog file , It's stored in DataBlock The contents in are the file name in the directory and inode The mapping relation of .
2.InodeBitmap And BlockBitmap
InodeBitmap and BlockBitmap Deposit is ,InodeTable and DataBlocks Bitmap . The position of the bit represents the number , The contents of bits indicate whether they are occupied .
such as : stay InodeBitmap The number in is 0000 1010, That means 0 Number one inode Not occupied , and 1 Number one inode It's already occupied .
Blockmap Empathy .
3. give an example
We can understand the above content by giving an example :
mkdir tmp
cd tmp
touch hello.c
echo "hello bit" >hello.c
cat hello.c
rm hello.c
touch hello.c: stay InodeBitmap Found unoccupied inode unit , Store the attribute information .
echo “hello bit”>hello.c: see BlockBitp Not used in block unit , Put the rest inode Building mapping , And store the contents of the document .
cat hello.c: adopt tmp Of inode Find and view tmp Of Datablock, Find and hello.c File names form a mapping relationship inode value , Through this inode Find the corresponding hello.c Of documents Datablock, So as to print the contents .
rm hello.c: stay tmp Of DataBlock Find the file name hello.c Corresponding inode, Only need to inodeBitmap It's time to inode The number of positions is determined by 1 Set as 0 that will do . There is no need for inodeTable And DataBlock To operate .
This is why the deletion of files is very fast , Because attributes and data are not deleted , Just delete whether it is effective . And compared with windows Recycle bin of the system , In fact, the files in the recycle bin have not been deleted , Just changed a directory . When we accidentally delete the file , The best way is to do nothing , Avoid content being overwritten .
Empathy ,mv Instructions are actually just moving file names and inode The mapping relation of .
3、 ... and 、 Hard and soft links
stay Linux Software and hardware links under the system , We can understand it as a shortcut .
Use ln Option to create hard and soft links :
ln -s// Create soft link
ln // Create a hard link
Here we create a about log.txt The soft links log_s.
Create a hard link log_h.
If it's a soft link , Soft link inode And documents inode Is different . Note the soft link creates a new file , It has its own attribute information and data blocks ( What is saved is the path of the file + file name ). Hard link inode And the original file inode It's the same , It indicates that the hard link does not create a file , Instead, a directory is added DataBlock File name and inode Correspondence of .
The previous red box indicates the number of hard links , When we create hard links , Both the hard link and the file become 2. When the value is 0 It means that the file is deleted .
When we create files , The value is 1, But when we create a directory , The value is 2.
This is because test In the catalog . It is also a hard link to the directory . When you create another directory under this directory , The value is 3, This is because … Also said test Catalog .
Four 、 Time attribute of the file
We can go through stat To view the basic properties of the file , Let's build a new one file.txt. And write something into it .
stat file.txt
among access,modify,change It represents the three time attributes of the file .
1.Access
Access Indicates the last access time of the file , But we go through cat Instruction access file discovery , Of documents access Time has not changed .
This is because opening a file is a high-frequency operation , To avoid a large number of repeated refreshes , In the newer Linux The kernel ,Access Will not be refreshed immediately , But there are certain time intervals OS Will automatically update the time .
2.Modify
Modify Indicates the latest modification time , Time is updated in real time .
echo “hello bit”>>file.txt
3.change
The last time the file properties were modified :
chomd u+x file.txt
Be careful , When modifying file attributes ,Modify Nothing has changed . But when modifying the contents of the file ,change It's possible to change . This is because modifying the contents of a file generally modifies the file properties , For example, adding content will modify the size attribute of the file .
4.Makefile Judge the way of file update
mytest:test.c
gcc $^ -o [email protected]
.PHONY:clean
clean:
rm mytest
among test.c by mytest Dependent source file . When Makefile Conduct make When , Will check the mytest and test.c Of Modify Time , Normally speaking, it must be test.c Of Modify Time ratio mytest Earlier ( because mytest By test.c To generate ). If we change test.c The content in , that test.c Of Modify Time will be better than mytest Late , here Makefile Will execute the corresponding dependent methods .
We can also verify : No change test.c In the case of touch(touch An existing file indicates the modification time ), At this point, we can do make 了 .
5、 ... and 、 summary
We use the creation of the entire file , Use and delete to summarize file operations .
1.touch file.txt
create file , First, the operating system is InodeBitmap and BlockBitmap Find not for 1 The number of , Take its subscript as the Inode and block The number of . And set it to 1, among Inode only one ,block There can be multiple .
2.echo “I am the king of Asgard”>>file.txt
Will call echo process , Turn off the standard output first (close(1)) In this process file.txt Files use system call functions open open ,
First, through the Blocktable, find file.txt And inode Mapping . Then find file.txt This file and its contents , And load it into memory , formation file The structure becomes a file of the process .
file.txt Get a file descriptor , Stored in the process files In the structure , This file descriptor is the file descriptor of the original standard output :1.
The process passes the print function to the file descriptor as 1 The contents printed in the document , At this time, it is to file.txt Print content in .
3.rm file.txt
take InodeBitmap In the said file.txt The number of the file 1 Set as 0.
边栏推荐
- TCP拥塞控制详解 | 7. 超越TCP
- Cv02 Roge matrix, rotation vector, angle
- Learning outline of the column "MySQL DBA's magic road"
- 常见分布式锁介绍
- 2022.07.13 summer training personal qualifying (VIII)
- Synchronized lock upgrade
- [PostgreSQL] PostgreSQL 15 optimizes distinct
- Leetcode 1310. Subarray XOR query
- TiDB 内存控制文档
- 02-2. Default parameters, function overloading, reference, implicit type conversion, about error reporting
猜你喜欢
[PostgreSQL] PostgreSQL 15 optimizes distinct
Configure spectrum navigation for Huawei wireless devices
Robot development -- common simulation software tools
03-1. Inline function, auto keyword, typeID, nullptr
Redis distributed cache redis cluster
Research on Wenhua commodity index
委派双亲之类加载器
02-3、指针和引用的区别
The concept of binary tree and three traversal methods (C language)
Dual machine hot standby of Huawei firewall (NGFW)
随机推荐
565. Array nesting: regular simulation questions
TCP congestion control details | 7 Surpass TCP
windows10:vscode下go语言的适配
Déléguer un chargeur tel qu'un parent
02-2、缺省参数、函数重载、引用、隐式类型转换、关于报错
Solution of connecting MySQL instance with public network
Docker安装MySQL
03-1、内联函数、auto关键字、typeid、nullptr
Ten minutes from pytorch to mxnet
[unity technology accumulation] simple timer & Co process & delay function
Redis分布式缓存-Redis集群
JVM钩子hooks函数
466-82(3、146、215)
Opencv draw a black rectangle and write the serial number
Dynamic memory allocation problem
【机器学习】多标签分类的评价指标与代码实现
Dream CMS foreground search SQL injection
【嵌入式单元测试】C语言单元测试框架搭建
[embedded unit test] construction of C language unit test framework
Mpu9250 ky9250 attitude, angle module and mpu9250 MPL DMA comparison