当前位置:网站首页>26. Code implementation of file using disk
26. Code implementation of file using disk
2022-07-24 15:07:00 【PacosonSWJTU】
【README】
1. The content of this paper is summarized from B standing 《 operating system - Li Zhijun, teacher of Harbin Institute of technology 》, The content is great , Wall crack recommendation ;
2. Review the contents of the document :
- 1) How to abstract from a raw disk to a file ?
- The core is to calculate the disk block number from the position of the character stream ;
- 2) How to calculate the disc number ?
- Use sequential structure , Or chain structure , Or the mapping table from the character stream provided by the index structure to the disk block number .
【1】 Use the disk again , Use through file

1) fs/read_write.c File reading and writing code
// stay fs/read_write.c in
// fd : File descriptor
// buf: Memory buffer
// count: Number of read and write characters
int sys_write(int fd, const char* buf, int count)
{
struct file *file = current->file[fd];
struct m_inode *inode = file->inode;
if (S_ISREG(inode->i_mode))
return file_write(inode, file, buf, count); // file_write Writing documents
}【 Code instructions 】
- Write At the kernel level, the code is the system call sys_write ;
- Sys_write The code does not use the disk block number , But the disk block number will be calculated through parameters ;
- Actual use of documents , Read and write to disk , Is to deal with a segment in the character stream ;
【 The illustration 】 Read write disk details ( The code level )
- step 1: retrievable inode,i It's the index ,node refer to FCB( File control block ); among FCB Index disk block number stored ( namely The mapping relationship between character stream and disk block number );
- step 2: According to the character position and index to be operated ( The mapping relationship ) Calculate the disk block number corresponding to the character stream for reading and writing ; The code behaves as file_write(inode, file, buf, count) , among inode yes index+fcb( Indexes + Index block ),file The file character stream position range is stored ( Such as 200~212),buf Memory buffer address ,count The number of characters ;
【1.1】file_write The whole working process

1)file_write(inode, file, buf, count) File write function
- inode: inode Index disk block number stored ( Mapping relationship between character range and disk block number );
- file: Character start position 200;
- buf: Memory buffer ;
- count: The number of characters 12;
adopt file and count You can calculate the character range 200~212 ;
2) The whole process of disk reading and writing is as follows ( User program to disk drive , To disk controller ):
- step 1: User application call file_write Function write disk , Pass in inode,file,buf,count Parameters ;
- step 2: file The starting position of the character in 200,count by 12, Can represent the character range 200~212;
- step 3: Read the index through the index disk block number ( Mapping relationship between character range and disk block number ), The disk block number can be found by searching the index according to the starting position of the character 789;
- step 4: Disc number 789 Add to request queue , The interrupt handler in the driver calls the elevator algorithm to select the disk block number , And analyze the disk block number CHS( Cylinder number , Head number , Sector offset ) Into the disk controller ;
- step 5: The disk controller is based on CHS Read and write to specific sectors ;
3)file_write Of Code implementation

【 The illustration 】file_write Detailed steps for writing files
- 1) step 1: Find the file character position to operate pos;
- 2) step 2: call create_block() According to the location pos Search index (inode Stored index ) Get the disk number block;
- 3) step 3: bread(...) Add the disk block number to the request queue , Current thread sleep ( When the disk is read and written , Wake up the current thread by the interrupt handler );
- 4) step 4: After adding to the request queue , Read characters from disk in the loop body ( Read one character at a time );
4)Create_block() Calculate the disk block number code implementation

5) Of the device file inode

【 The illustration 】
- i_mode Point to character file ;
- i_zone Point to the file content data block ;
- MAJOR : The main equipment, ;
- MINOR : Secondary device number ;
【2】 Great file view
【2.1】 Operation file

【 The illustration 】 Operating disk steps
- step 1: Open file ; fd = open(“test.c”) ; // Get file descriptor ;
- step 2: Read the file ; write(fd) ; fd Exist in File list ;
- step 3: Find fd Corresponding inode; ( above 3 A step It's about Get by file name or file path inode)
- step 4: adopt inode Find the disk number ;
- step 5: Send the disk block number into the request queue ;
- step 6: The driver takes the disk block number from the queue , To calculate the CHS Parameters and issue out The command sends parameters to the disk controller ;
- step 7: The disk controller drives the motor to read and write disk sectors , Electricity generates magnetism and magnetism generates electricity ;
【2.2】 Operate the display and other peripherals
- Its call path is also similar to the operation disk path ;
- It's just inode It points to the display , Instead of the file block number ; The operation function is tty_write instead of file_write ;
边栏推荐
- Not configured in app.json (uni releases wechat applet)
- spark:获取日志中每个时间段的访问量(入门级-简单实现)
- Attributeerror: module 'distutils' has no attribute' version error resolution
- Kotlin类与继承
- 老虎口瀑布:铜梁版小壶口瀑布
- zabbix管理员忘记登录密码
- 华为无线设备配置WPA2-802.1X-AES安全策略
- Date processing bean
- VSCode如何调试Nodejs
- [matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes
猜你喜欢

Route planning method for UAV in unknown environment based on improved SAS algorithm

Tiger mouth waterfall: Tongliang version of xiaohukou waterfall

Leetcode-09 (next rank + happy number + full rank)

27.目录与文件系统

Production environment tidb cluster capacity reduction tikv operation steps

Attributeerror: module 'distutils' has no attribute' version error resolution

ZABBIX administrator forgot login password

Existence form and legitimacy of real data in C language (floating point number)

Class loading mechanism and parental delegation mechanism

Meaning of 7 parameters of thread pool
随机推荐
Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
File upload and download and conversion between excel and data sheet data
Discussion on the basic use and address of pointer in array object
Overview of dobesie wavelet (DB wavelet function) in wavelet transform
C language large and small end mode judgment function
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
Atcoder beginer contest 261e / / bitwise thinking + DP
Date class and time class definitions (operator overload application)
Spark: specify the date and output the log of the corresponding date (entry level - simple implementation)
The difference and relation among list, set and map
Vector introduction and underlying principle
Property datasource is required exception handling [idea]
C operator priority memory formula
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
DS sort -- quick sort
基于ABP实现DDD--实体创建和更新
The first n rows sorted after dataframe grouping nlargest argmax idmax tail!!!!
Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
深入浅出边缘云 | 2. 架构
DS inner row heap sort