[LINUX] Basic IO (file system, hardware and software link, dynamic and static libraries)

File system

When we view it in LINUX, we will see that the file not only has the file name but also some other attribute information.
Insert picture description here
include:

Mode,
number of hard links,
file owner,
group,
size,
last modified time,
file name,
Insert picture description here
then we think about how this information is queried and where is it stored. Here we will introduce inode and block.

inode|block

The storage in the hardware was initially on the disk.
Insert picture description here
Determine the disk surface, track, and sector by the head. We can transform it linearly and think of it as an array.
Insert picture description here
Block Group as shown in the figure below : The ext2 file system is divided into several Block Groups according to the size of the partition. Each Block Group has the same structure. Examples of government management of various districts

Super Block: Stores the structural information of the file system itself. The recorded information mainly includes: the total amount of bolck and inode, the number of unused blocks and inodes, the size of a block and inode, the time of the most recent mount, the time of the most recent data write, and the time of the most recent disk inspection Other file system related information. The information of Super Block is destroyed. It can be said that the entire file system structure is destroyed.

GDT, Group Descriptor Table: Block group descriptor, describing the attribute information of the block group

Block Bitmap: The Block Bitmap records which data block in the Data Block has been occupied and which data block is not occupied

Inode Bitmap: Each bit indicates whether an inode is free and available. i-node table: store file attributes such as file size, owner, last modification time, etc. Data area: store file content

Insert picture description here
There are four main operations for creating a new file:

  1. Storage attributes The
    kernel first finds an idle i-node (in this case, 263466). The kernel records the file information in it.
  2. Store data.
    The file needs to be stored in three disk blocks. The kernel found three free blocks: 300, 500, and 800. Copy the first block of data in the kernel buffer to 300, the next block to 500, and so on.
  3. Record distribution situation The
    contents of the file are stored in order of 300, 500, 800. The kernel's disk distribution area on the inode records the above block list.
  4. Add the file name to the directory. The
    new file name abc. How does linux record this file in the current directory? The kernel adds the entry (263466, abc) to the catalog file. The correspondence between the file name and the inode connects the file name with the content and attributes of the file.
    The same is true for the directory. The inode and mapping relationship of the file are stored inside.

Soft and hard link

Soft link: The
soft link has its own inode, and the path of another file is stored in the file. It is an independent document.
Hard link: The
hard link and the pointed file have the same inode, not a separate file. In linux, multiple files can use the same inode.

Dynamic and static library

Static library (.a): The code of the library is linked to the executable file when the program is compiled and linked. The static library will no longer be needed when the program is running

Dynamic library (.so): The code of the dynamic library is linked when the program is running, and the code of the library is shared by multiple programs.

An executable file linked with a dynamic library only contains a table of the function entry addresses it uses, not the entire machine code of the object file where the external function is located

Before the executable file starts to run, the machine code of the external function is copied from the dynamic library on the disk to the memory by the operating system. This process is called dynamic linking.

Dynamic libraries can be shared among multiple programs, so dynamic linking makes executable files smaller and saves disk space. The operating system uses a virtual memory mechanism to allow a dynamic library in physical memory to be shared by all processes that use the library, saving memory and disk space

Static library

Generally, when you package to others, you will give the static library and header files to each other, so that others can reuse your code, which is highly portable, but it costs more resources, and many static libraries may be duplicated.

生成静态库
[root@localhost linux]# ar -rc libmymath.a add.o sub.o
ar是gnu归档工具,rc表示(replace and create)
查看静态库中的目录列表
[root@localhost linux]# ar -tv libmymath.a
rw-r--r-- 0/0 1240 Sep 15 16:53 2017 add.o
rw-r--r-- 0/0 1240 Sep 15 16:53 2017 sub.o
t:列出静态库中的文件
v:verbose 详细信息
[root@localhost linux]# gcc main.c -L. -lmymath
-L 指定库路径
-l 指定库名
测试目标文件生成后,静态库删掉,程序照样可以运行。

Library search path

Search the directory specified by -L from left to right.
The directory specified by the environment variable (LIBRARY_PATH)
is the directory specified by the system
/usr/lib
/usr/local/lib

Dynamic library

Generate dynamic library:

shared: 表示生成共享库格式
fPIC:产生位置无关码(position independent code)
库名规则:libxxx.so

The difference between the compilation and the static library is that there is no static, and the name of the dynamic library is followed by l, that is, lib and .so are removed

Example: gcc main.o -o main –L. -lhello

Run dynamic library

1 Copy the .so file to the system shared library path, generally referring to /usr/lib
2. Changing LD_LIBRARY_PATH// does not require adding your own environment variables after L
3. ldconfig configuration /etc/ld.so.conf.d/, ldconfig update

External library

There are actually many libraries in the system, and they usually consist of a set of interrelated functions used to complete a common task. For example, the function used to handle the screen display (ncurses library)
(now most cloud lib libraries also have external libraries that are more convenient, if there is no virtual machine, you need to use the previous operation to operate)
-lm means to link libm.so or libm.a library file

Guess you like

Origin blog.csdn.net/weixin_43762735/article/details/115311460