File opening process-the work done by the operating system when fd = open () is called, fdopen
Fd = fopen () is a system call used to open a file based on the file name and return the file descriptor. After the file is opened, the process can perform other operations based on the file descriptor fd, such as reading, write, close, and other operations.
The process of opening a file in each operating system is similar. This document takes Unix as an example to describe how to open a file operating system. Before officially introducing this process, we will briefly introduce several concepts.
PCBA process control block is a kernel data structure. It is equivalent to a file and is the unique identifier of a process perceived by the operating system. Including Process status, process id, PC, register, memory information, and file opening information, as shown in
FCB (File control block)A file control block is a part of a file system. Generally, a file system is created on a disk, which contains directory information and FCB information of the file. FCB contains the file's read/write mode, owner, timestamp, data block pointer, and other information. The unix FCB is called inode. Its structure is shown in
This picture comes from http://codex.cs.yale.edu/avi/os-book/OS9/slide-dir/os-figures.zip
Shows the process of opening a file (from right to left)
First, the operating system searches the System File opening table based on file name.
First case:
If file a has been opened, a table item is allocated to file a in the process file opening table, and the pointer to the table item corresponding to file a in the System File opening table;
Then, a file descriptor fd is allocated to the file in the PCB, which serves as a pointer to the table item opened in the process file. The file is opened completely.
Case 2:
If file a is not opened, check whether the directory items containing file a are in the memory. If not, load the directory table into the memory as a cache;
Locate the position of FCB on the disk based on the corresponding item of file a in the directory table;
Load the FCB of file a into the Active inode in the memory;
Add a new table item for file a in the System File Open table, and point the table item pointer to the FCB of file a in Active Inode;
Then, assign a new item to the process file opening table and point the pointer to the table item corresponding to file a in the System File opening table;
Then, in the PCB, assign a file descriptor fd to file a, which serves as a pointer to the table entry for process file opening. The file opening is complete.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.