Learn more about system I/O

1. Understand the open interface again

The third parameter of 1.1opne

Insert picture description here

1.2 Why the second parameter is plastic

Insert picture description here

2.write system call interface

2.1write interface

The first parameter of wtite is fp. Our process calls the write interface and finds the corresponding structure struct file_struct through the information stored in the PCB. Then find the structure pointer array struct file*fd_array[] in the structure, and then through the given parameter fp, which is the subscript of our array, take out the pointer of the structure struct file of the saved description file, and find the corresponding file Structure, get the information of the file, and the storage location, that is, find our file
Insert picture description here

2.2 The difference between system call interface and library function interface

The system call interface is directly provided by the system for external use, and the library functions are further encapsulated on this
Insert picture description here

3. Buffer

3.1 The buffer is provided by the language

Our buffer is provided by the language and maintained by the FILE structure
Insert picture description here

3.2 Why a buffer mechanism is needed

Improve the efficiency of calculator processing. For example, our program will store all the data in the cache area, and call write to refresh to the system when appropriate.
If every time data is generated, it will be refreshed to the system, which will lead to interactive data. The number of times has increased significantly, and the efficiency will also drop sharply.

4.dup2, file redirection

4.1dup2

int dup2(int oldfd, int newfd);
newfid is a copy of oldfd

Insert picture description here

4.2 file redirection

Combining the above description, we can see that in our output function, the description structure of stdout must include file descriptor No. 1;
and our dup2 can assign and copy the content corresponding to the file descriptor, so the realization of file redirection The principle is to use dup2 civet cat for prince.

Replace the content in the file descriptor No. 1 with a new pointer to the file structure, but in our output function, the information saved is always the file descriptor No. 1 and the
output function will be called to the file descriptor No. 1. Output information in the pointed file, thus completing the redirection.

Append redirection is to add the O_APPEND parameter when opening the file

Open files need to be closed. Actually, our file descriptors are limited (the initial default is 32). Unlimited opening without closing will cause file descriptor resource leakage

5. Understand the file system

5.1 What is a file system

File system is the method and data structure used by the operating system to identify storage devices (disks are common, and solid state drives based on NAND Flash) or partitions

There are many files stored on our hard disks, which also need to be managed, and correspondingly, a manager needs to manage them. This manager is the file system, and the file system performs partitioning and management of the disk.

To be more vivid, the file system manages many files, these files are data, and these data are stored on the hard disk.
Therefore, the file system is a software system for managing disks. It reduces the difficulty of using disk space and displays disk data to users more vividly.
Insert picture description here

5.2 Why do I need a file system

Insert picture description here

5.3 How the file system manages the disk

5.3.1 Disk data abstraction

Insert picture description here

5.3.1 Disk partition management

Insert picture description here

5.4 What is stored in our catalog

Insert picture description here

5.5 Process of file operation

Insert picture description here

5.6 File soft and hard link

File links are divided into hard links and soft links.
The hard link does not have an independent inode, it just adds a file name that has a mapping relationship with the inode, and does not create a new file. The
soft link creates a new file with an independent inode, and this file saves the path of the link file.
Insert picture description here
Insert picture description here

6. The packaging method of dynamic and static libraries

6.1 What is a function library

It can be seen from the above that we only included the header file "stdio.h" in the preprocessing stage, and there is only the declaration of the printf function in the header file, which is not implemented. So where is the function implemented?

The system puts the implementation of these functions in a library file named libc.so.6. When there is no special designation, gcc will search under the system default search path /usr/lib, that is, link to libc.so.6 library function, so that the function is realized, which is also the role of link;

Insert picture description here

6.2 The difference between dynamic and static libraries

Static linking: When compiling and linking , copy the corresponding code to the source file.
If n programs all call the same interface of the library, you need to make a copy, which occupies resources (hard disk resources and memory resources to a certain extent). )

Dynamic library: Link the code of the dynamic library when it is running . There can be only one copy in the entire system, and multiple programs share the code of the library.
Insert picture description here
Insert picture description here

6.3 Function library generation

6.3.1 Common commands

ldd: View the library that an executable file depends on
ar -rc: static library packaging
ar -tv: View the files that the library depends on

6.3.2 Function library needs to have conditions

Insert picture description here

6.3.3 Static library generation

Insert picture description here
Insert picture description here

6.3.4 Dynamic library generation

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/ych9527/article/details/115289437