Fd=fopen () is a system call that opens a file based on its file name, returns the file descriptor of the file, and allows the process to perform other operations, such as reading, writing, and closing, based on file descriptor FD.
The process of opening files for each operating system is similar, this article takes Unix as an example, describes the work done to open a file operating system, before formally introducing this process, a brief introduction to several concepts.
PCB Process Control block, which is a kernel data structure, equivalent to a file, is the unique identity of the operating system-aware process. Including process status, process id,pc, register, memory information, file open information, etc., as shown in
FCB (file control block) is a part of a file system that typically creates a file system on disk that contains directory information and the FCB information for the file. FCB half contains the file's read-write mode, owner, timestamp, data block pointers and other information, the UNIX FCB is called the inode, its structure as shown
This image from http://codex.cs.yale.edu/avi/os-book/OS9/slide-dir/os-figures.zip
The process of opening the file as shown (from right to left)
First, the operating system looks in the System File open table according to the file name a.
First case:
If file A is already open, assign a table entry to file A in the open table of the process file, and then point a pointer to the table item to the system file to open the table with one of the corresponding entries in file A;
Then, in the PCB, a file descriptor FD is assigned to the file, and the file opens as a pointer to the Open table entry for the process file.
Second case:
If file A is not open, see if the directory entry containing the file a information is in memory, and if not, load the catalog table into memory as the cache;
Find the location of the FCB on the disk according to the file a counterpart in the catalog table;
The FCB of file A is loaded into the active inode in memory;
Then, in the System File open table, add a new table entry for file A, pointing to the FCB of file A in the active inode of the table item pointer;
Then assign a new item to the File open table of the process, and point the table item pointer to the system file to open the table entry for file a in the table;
Then in the PCB, a file descriptor FD is assigned to file A, and the file opens as a pointer to the Open table entry for the process file.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The process of opening a file--the work done by the operating system when calling Fd=open ()