当前位置:网站首页>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

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 .
 Insert picture description here
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 .
 Insert picture description here
meanwhile , We also divide the long disk into areas of different sizes , It's called C disc ,D disc ,E disc .
 Insert picture description here

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 .
 Insert picture description here
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 :
 Insert picture description here
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 .
 Insert picture description here
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 :
 Insert picture description here
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

 Insert picture description here
Here we create a about log.txt The soft links log_s.
 Insert picture description here
Create a hard link log_h.
 Insert picture description here
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.
 Insert picture description here
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

 Insert picture description here
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 .
 Insert picture description here
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

 Insert picture description here

3.change

The last time the file properties were modified :

chomd u+x file.txt

 Insert picture description here
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 了 .
 Insert picture description here

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.

原网站

版权声明
本文为[Selling lonely little boys]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171133075424.html