Learning System Programming No.16 [Interprocess Communication]

introduction:

Beijing time: 2023/4/9/20:44, yesterday, that is, Saturday of this weekend, was the legendary Blue Bridge Cup, hahaha! It's bad, and I'm really not good at doing the questions. Unfortunately, it may be that I was young and frivolous or had little experience. I hope that after this experience, I can be more calm next year! Who told us not to do the questions? To be precise, I don’t have the habit of doing questions, and it can also be said that I don’t have much time to do questions. Although I have known since I was a child that time is like water in a sponge, there is always a squeeze, but a lazy word, for example, blue When Qiao Bei came back, he could have started blogging directly, but it has been so bad until now. Forget it, the main reason is that there is not much pressure (this may be the status quo of contemporary freshman students, no goals, no pressure), in this case , A person has 100 reasons to convince himself that he is bad, and I am no exception, hahaha! But, still the same sentence, if you don't give up, it's fine if you don't give up, just have a small goal, hahaha! Of course, like me, the pressure is still very high. The most direct point is that the big guys on CSDN insist on updating, and we are naturally unwilling to be left behind! So it is easier for me to get on the road! So today let's learn about inter-process communication and the knowledge about dynamic and static libraries in the previous blog!
insert image description here

Review dynamic and static libraries

In the previous blog, we learned that when the dynamic library is used, it does not copy all the data in the library to the target program, but is called in the form of a header file, so only the compiler can recognize the dynamic library path, and the operation The system cannot recognize it, which will lead to a link error in the operating system when the dynamic library is in use, that is, when the executable program is to be executed, and I understand that in order to solve this problem, there are three solutions, as follows:

  1. Import the library path into an environment variable (temporarily)
  2. Soft link (permanent)
  3. Profile Scheme

And in the last blog, we have already learned about the first two, importing environment variables and establishing soft links, so let us learn the third way, the way of configuring file schemes! And review the first two, import environment variables and create soft links:

Importing environment variables
is to import the path where the dynamic library is located into the environment variable that the operating system can recognize by default, because the essence is to make the path where the dynamic library is located can be found by the operating system. At this time, the environment variable is: &LD_LIBRARY_PATH, So if you want the operating system to find the path of the dynamic library at this time, just import the path of the dynamic library into this environment variable, the command : export LD_LIBRARY_PATH=&LD_LIBRARY_PATH:"动态库所在的路径", so when we import the path of the dynamic library into this environment variable, the operating system At this point, you can find the corresponding path, as shown in the following figure:
insert image description here


I still understand that the establishment of a soft link is essentially to allow the operating system to find the path of the dynamic library, so the essence of establishing a soft link is to create a mapping file for the path of the dynamic library that can be recognized by the operating system by default . , instruction: sudo ln -s /home/vimtest/其他人的动静态库/我的动态库/lib/libmymath.so /lib64/libmymath.so, as shown in the figure below:

insert image description here
It means that the path where the dynamic library is located is created under lib64the path that the operating system can recognize by default as a libmymath.sosoft link mapping file,
as shown in the figure below: At this time, the operating system can find the path where the dynamic library is located by default.
insert image description here

Configuration file scheme
First of all, I still understand that no matter which way it is, its essence is to allow the operating system to find the file path where the dynamic library is located, so the way to configure the file is the same, as shown in the figure below, and: in the Linux 注意system Next, there is a configuration file path by default
insert image description here
and we understand that we can create our own files under this configuration file path, command:sudo touch /etc/ld.so.conf.d/bit_107.conf

insert image description here
As shown in the figure above, we can find that under the path of the system configuration file, there is bit_107.confthis file at this time, so at this time we only need to write the path where the dynamic library is located in the file, as shown in the figure below:
insert image description here
and finally the configuration The path of the file is reactivated, command: sudo ldconfig, at this time, the path where the dynamic library is located can be recognized by the operating system, as shown in the following figure:
insert image description here

General: Through the above three methods, it can be understood at this time that when the operating system executes an executable program, there are three ways to find the corresponding dynamic library path


Go deep into the dynamic and static library

First of all, understand that the above knowledge and the knowledge of the previous blog are essentially only the knowledge of the operation of dynamic and static libraries, so let us go deep into the interior of dynamic and static libraries at this time, and take a look at dynamic and static libraries in detail! (theory)

In essence, it is 使用特征the difference between dynamic and static libraries: when using a static library, the target program needs to copy the implementation of the entire static library into the program, while the dynamic library indirectly uses the dynamic library through the declaration of the function interface in the header file. The implementation of the function interface, and the dynamic library can distinguish the used function interface from the unused function interface through conditional judgment, so as to achieve the ability of precise use without blind copying

When you understand the above knowledge, you can easily know at this time that when the static library is loaded into the program, it will take up a lot of resources at this time , regardless of time and space resources, or network resources. When the dynamic library is used, due to Only the header file is loaded into the target program, and the implementation of the entire dynamic library is loaded into the memory when the program is executed (because it is essentially a binary file), so the use of the dynamic library will not be so important at this time. Occupies resources , as shown in the figure below:

static library
insert image description here
dynamic library

The dynamic library is linked to the target program. At this time, the specific content in the dynamic library is not copied, but the address of the interface implementation corresponding to the dynamic library is replaced by the external symbol corresponding to the corresponding target program through the header file and conditional judgment. The process is shown in the figure below:

insert image description here

In general, when using the dynamic library, not only the target program needs to be loaded into the memory, but also the dynamic library needs to be loaded into the memory
6TFDD
and 注意, according to the above figure, we can know that the physical memory has already stored The corresponding dynamic library is already available. If other programs, that is, other processes, also need to use the dynamic library at this time, then when the operating system recognizes that there is already a corresponding library in the memory, it is not needed at this time. Instead of reloading from the disk, it is enough to directly map the dynamic library to the shared area of ​​its own address space through the page table, so as to achieve the function of one library with multiple uses , so that resources can be greatly saved, both in terms of operating system efficiency and space-time aspects

How to understand the dynamic library address ( fPIC)

After finishing the above, how the dynamic and static libraries are called in the memory, let’s take a deeper look at the specific addressing methods of the dynamic and static libraries. The purpose is to understand the relevant knowledge (regardless of position codes) , fPICas follows The narration says:

First of all, the first thing to understand is that a program allows multiple addresses to exist at the same time. The analogy is that a person is allowed to have multiple names or multiple numbers, such as student number, dormitory number, and ID number. After understanding this point , it is obvious that a program must have different addresses, for example, the logical address in the disk or the physical address in the physical memory or the virtual address in the virtual address space, which means that an objectively existing object or People, in different scenarios, will have different attributes. This attribute may be morphological or logical. After understanding this, let us continue to delve into the address!

Understand the above knowledge, know that a program can have different addresses, logical addresses, physical addresses, virtual addresses, and understand that all these different addresses have different names, just because of the different scenarios of the program, so always Generally speaking, the program has many addresses, and the address will be different due to different scenarios, but in general, there are only two types of addresses in essence, one is absolute address (static library), and the other is relative address (dynamic library ) , and understand that the addressing in the virtual address space is an absolute address

Understand absolute addresses and relative addresses :
I believe we have all known what is an absolute path and what is a relative path before. Similarly, when addressing, the address is the same. The absolute address is like the physical address in the physical memory . Each byte in has its own number (address), which is called an absolute address , and a relative address, just like a logical address in a disk, is referenced by a fixed point or value, and then passed through the offset Addressing in the form of quantity, this is the relative address

Why does the dynamic library use relative addressing :
Whether it is from the previous blog knowledge or the above knowledge, we all understand at this time that when a target program links to a static library, it directly copies all the interfaces in the static library to the target program , so when the virtual address space of the process maps physical memory through the page table at this time, the virtual address space stores the complete code of the target program on the physical memory mapped through the page table . As long as you can find it; as for the dynamic library, due to different processes, different operating degrees or different running programs, the number of dynamic library interfaces or interface implementations that need to be used is different, or the third-party libraries that need to be used are different, so it is destined that each The dynamic library stored in the virtual address space of the target program is different, the address space of each process is different , and the interface implementation code stored in the shared area of ​​each process is different, so the dynamic library does not allow the use of absolute addresses, but relative addresses. The specific reasons are as follows:

The dynamic library cannot use absolute addressing because the dynamic library will be mapped into the address space of the process when it is loaded, and the dynamic library is shared by multiple programs , and the memory address space of each program is different. So the dynamic library cannot use absolute addressing because the dynamic library determines its final memory location when it is loaded into the memory space of the process, and the memory address spaces of different processes are different (different executable programs) , so if dynamic The library uses absolute addresses for addressing, so in different processes that load the library, its code and data may be mapped to different memory addresses . This results in code that cannot be shared between multiple processes because addresses in one process cannot be interpreted correctly by the other processes. Therefore, dynamic libraries must adopt relative addresses to ensure that their code and data can be accessed using the same offset in all processes

As shown in the figure below: As shown
insert image description here
in the figure above, that is why relative paths are used instead of absolute paths, and note: the addresses of all function interface implementations in the dynamic library are offsets, and they start from 0 by default and are fully understood
:

So in essence, the dynamic library does not allow the use of absolute addresses, because the mapping relationship between the address space and the memory address is corresponding, and the dynamic library on the address space is different due to the different program codes, but because the processes are different. Share a dynamic library, so the interface of the dynamic library in the address space must also be the same, otherwise the unique dynamic library data on the memory cannot be found through the corresponding mapping relationship, so the absolute address cannot be used in the address space at this time , thus using the relative address, because using the relative address, the offset can be added to the starting position of the dynamic library at this time (as long as the interface implementations used by each process are the same, the offset will be the same), so when calling, call The interface of the dynamic library is the same, and finally according to the mapping relationship, the interface implementation of the dynamic library on the accessed memory is the same

So to conclude:

Total: This is why fPIC is used

Anyway, the ultimate goal is to understand how the dynamic library realizes partial loading, the concept of relative address

Specific usage scenarios for testing dynamic and static libraries

After understanding the above knowledge, we have completed the knowledge about the operation and theory of dynamic and static libraries. At this time, we will go deeper into a few scenarios to see what are the specific characteristics of dynamic and static libraries. First of all, we must understand that a huge The program has a lot of code and data. At this time, not only the implementation of the dynamic library is required, but also the implementation of the static library. Therefore, most programs are completed by using the static library and the dynamic library at the same time. Yes, but generally some small programs can be completed using only a dynamic library or a static library, so if there is a dynamic library and a static library at this time, and the function interface implementations in these two libraries are required by the target program, So at this time, does the system link the static library or the dynamic library? As shown below:

insert image description here
So at this time we know that when there are both static libraries and dynamic libraries in a library, the program will call the dynamic library first at this time, so I believe that everyone must have a question at this time, that is, how to call the static library first? ? As shown in the figure below:
insert image description here
So we know that when a library has both a dynamic library and a static library, although the default is the dynamic library, we can still call the static library first, as shown in the figure below by viewing the properties of the file. We can also see whether the program is linked to a dynamic library or a static library. Specific instructions:
gcc -o mytest-d main.c -I include -L lib -l mymath
gcc -o mytest-s main.c -I include -L lib -l mymath -static

insert image description here
In general, it is what is used, but the default dynamic library is preferred, and the static library is linked only if there is no dynamic library

interprocess communication

Purpose of interprocess communication
Data transfer: one process needs to send its data to another process
Resource sharing: share the same resource among multiple processes
Notification event: A process needs to send a message to another process or a group of processes to notify it (they) that some event has occurred (such as notifying the parent process when the process terminates)
Process control: Some processes want to completely control the execution of another process (such as Debug process), at this time the control process hopes to be able to intercept all traps and exceptions of another process, and to be able to know its state changes in time

Inter-process communication development

1. Pipeline 2. System V interprocess communication 3. POSIX interprocess communication

Classification of interprocess communication

1. Pipeline 2. Anonymous pipe pipe 3. Named pipe

See the next blog for detailed knowledge

insert image description here

Summary: There is really a lot of knowledge about dynamic and static libraries, and finally everything is done, which is difficult! Next, let's talk about the process! Detailed explanation of inter-process communication, see you in the next blog!

Guess you like

Origin blog.csdn.net/weixin_74004489/article/details/130047523