Learning System Programming No.18 [Pipeline Combat of Inter-process Communication]

introduction:

Beijing time: 2023/4/11/21:17, today's article is updated! But it is still not on the hot list, so we need to continue to update the article! I believe that the next blog will definitely be on the hot list, come on! And there is a physical education class tonight, so I only started coding now. In the physical education class, we were taught to play badminton. Although I haven't played for almost a year since the college entrance examination, I still have some strength, but my strength needs to be recovered. Exercise is actually very happy, hey! It's a pity that time needs to be used for coding, and we can't even squeeze out the time for playing ball, so let's strike while the iron is hot, and learn about the pipeline of inter-process communication!

insert image description here

Digging into interprocess communication

Why do we need to have inter-process communication? From the previous knowledge about processes, we can find that what we have learned before is the knowledge about a single process, whether it is process termination, process creation, or process waiting, so a single process is Like an individual, and understand that there must be a certain connection between the individual and the individual, so there should also be a certain connection between the process and the process. The purpose of this connection is like the relationship between the individual and the individual. The development of some aspects, so the connection between the process and the process, the purpose is to promote some aspects, for example, save resources, improve efficiency, etc.! So the purpose of inter-process communication is to provide an environment for the connection between individual processes, so that the processes can perform some operations we want, such as: data transmission ( one process transmits the corresponding data to another process) , resource sharing (one process shares resources to multiple processes), notification event (one process tells another process related events through system call interface parameters, for example, when a child process terminates, it notifies the parent process through output parameters process), process control (a process needs to control the process in order to know the status of another process in real time), but note: the connection environment provided by inter-process communication, this connection does not only involve between processes, the essence It is all done by the operating system, specifically as follows:

Because processes are independent, if you want to allow two processes to see the same resource, you must understand that this operation cannot be completed between processes, and the operating system needs to play an intermediary role, so that One process and another process can see the same resource, and finally realize the basic condition of inter-process communication. Two processes share the same resource (file resource) , so the essence of inter-process communication is not to let processes communicate with each other. Just build an environment for communication between processes and processes, as shown in the following figure:
insert image description here

Understand the standard from communication, and then settle the communication
according to the standard. After understanding the above knowledge about inter-process communication, let’s abandon the process at this time, and talk about the word communication in detail. What is communication? The word communication should be very important in the last century. A concept, its proposal may have been cross-generational at the time. It can be seen simply from now that human beings are inseparable from communication, and have gone further and further on this road. From 5G communication to It can be seen that today's communication technology is very mature, and the efficiency, stability, and security of communication have reached a new height in 5G communication; why there is such a problem as communication, we will not talk about it, let's focus on it Why can communication develop so rapidly, from 2G to 3G to 4G to today's 5G, with each passing day, what is behind the rapid development? At this time, we have to introduce two words that are closely linked to communication, standard , what is a standard, everyone must know, isn't it a standard? Hahaha! So what is a standard, your head is directly confused, isn't a standard a standard? Therefore, according to our daily understanding, a standard is a rule, a rule, a rule and a basis that everyone must follow, so when we understand this, we know that the reason why communication can develop so rapidly is because of the standard, and everyone follows the standard Execute and strictly abide by the standards, so I understand the truth that if anything wants to be successful and successful, it must first have a standard, so the reason why communication can be so successful is because it has its own set of standards: communication standards , interested Students refer to this link: Detailed explanation of communication standards

After understanding the above knowledge, at this time we understand that there is a standard for everything, whether it is a country, a society, or the world, as long as there are people, there must be a standard. This is the first principle of human beings' continuous progress , communication is no exception, and inter-process communication is no exception, and we know that there are mainly two standards for inter-process communication, one is the System inter-process communication standard ( 本地通信), the other is the POSIX inter-process communication standard ( 网络通信) , interested Students refer to this link: Detailed explanation of inter-process communication standards , and understand that there are many types of inter-process communication . In different scenarios or environments, it is possible to use different types of inter-process communication, but no matter what type Inter-process communication, because there is an inter-process communication standard, so when different types of inter-process communication are used in different environments or different scenarios, the implementation of inter-process communication is roughly the same, which is the meaning of the standard.

How to understand the pipeline

After understanding the above knowledge, let's take a look at the simplest type of inter-process communication, the knowledge of inter-process communication pipelines

First of all, we must understand that under the Linux system, |it means that 管道, and when we use the pipeline, the corresponding instructions (function interfaces) on the left and right of the pipeline will eventually become processes, so it is concluded that when you use a pipeline, it will be processed at the same time To generate two processes, if you use two pipelines, three processes will be generated at the same time. By analogy, as shown in the figure below:
insert image description here
insert image description here
Abstract a pipeline, data transmission between two processes, as shown in the figure below:
insert image description here
So as can be seen from the figure above, the operation When the system communicates between processes, it will help us create a pipeline. According to the Linux system, everything is a file. At this time, the essence of the pipeline is a file, which is equivalent to the operating system. When communicating between processes, it will help us maintain a file, and rely on this file to achieve the effect of allowing two different processes to share a "resource" , so that the operating system completes the construction of the inter-process communication environment, so that the processes can communicate, so how to communicate? ?

With the previous and the above knowledge, it is very simple to understand how to communicate. There are no redundant knowledge points and requirements. In essence, it is to let a process (who process) open the pipeline file by writing and copy the data to the pipeline. file, and then let another process (wc -l) open the pipe file for reading, and finally read the data from the pipe file. It’s that simple. The only thing to pay attention to is to understand the default output of the process and default input, as follows:

注意:进程默认打印数据,是向标准输出打印,进程读取数据,默认是从标准输入读取 , so if you want to implement at this time, let a process (who) copy data to the pipeline file instead of marking the output file, then you need to redirect the standard output of the process (who) to the pipeline file, and another One process (wc -l) redirects the standard input to the pipe file. Only in this way, the operating system defaults. The process (who) copies data to the pipe file instead of standard output, and the other process (wc -l) is to pipe file to read data, not standard input

Further understanding of standard output and standard input, such as the above-mentioned changes to the standard output and standard input of the corresponding process to the pipeline file, is essentially based on the principle. In order to make the pipeline file the standard output of the process (who) that transmits data, it means The data to be transmitted is not copied to the display, but copied to the pipeline file, and another process (wc -l), at this time, does not get the data from the standard input (keyboard), but from the pipeline file (redirected)

The above knowledge point has troubled me for a long time. The reason may be that the relationship between the process and the file descriptor table is not clear. Of course, the above knowledge points need to be understood through the knowledge, so the more you learn, you need to master and The more knowledge you use, the higher the cost of understanding between knowledge and knowledge, so the foundation must be solid! And the above-mentioned knowledge point can be said to be the most important point to understand the pipeline. Once this is done, other knowledge is just a matter of course! See the figure below for details:

insert image description here

As shown in the figure above, at this time, since the file descriptor tables of the blood-related processes are inherited from the parent process, the files they point to are the same file, and because the operating system maintains an anonymous pipe file, so At this time, the two processes share a pipeline file, so that the two processes can see the same resource and share the same resource. Finally, since each process has a way to read and write the shared resource, this At this time, the two processes can control the reading and writing, and then control the communication between the processes . 例如At this time, if you want the child process to write, and the parent process (or sibling process) to read, then do The most important thing is to close the reading end of the child process and the writing end of the parent process, specifically because both the parent process and the child process have the ability to read and write shared resources, but because the pipeline can only perform one-way transmission (because the file has only one buffer ) , so the parent process and the child process cannot read and write shared resources at the same time, only you can read and write or you can write and read. content at the very beginning, carry on 资源共享,数据传递,通知事件,进程控制)

And from the sharing of the same file descriptor table between the above-mentioned blood relationship processes, it can explain a phenomenon. This phenomenon is what we saw when we were learning process creation at that printftime cout. display) to print data, the reason is because the file descriptor table is shared (inherited) between bloodline processes

Inter-process communication code implementation

Through the above knowledge, we have all the principles of why and how to communicate between processes, and we have seen the specific diagrams. At this time, let us complete the last process of learning inter-process communication and implement one by ourselves. The code for inter-process communication is as follows:

If you want to implement an inter-process communication code by yourself, as shown in the figure above, the principle is very obvious, and the specific steps are as follows

1. Create a pipe
2. Create a child process
3. Close unneeded fd
4. Start communication

So at this time, let us follow the above steps to implement the code of inter-process communication by ourselves!

Create a pipeline
At this time, you want to create a pipeline. As the above knowledge says, a pipeline is essentially a file, and this file is a memory-level file, which is created by the kernel and managed by the operating system. So understand, this It is definitely impossible for us to create a file. If we want to create this memory-level file, we must call the system call interface and let the operating system create it for us through the kernel. At this time, the system call interface:pipe

The instructions for use under the Linux system are as follows:
insert image description here
As shown in the figure above, the basic instructions for using the pipe, we can see the following information:

pipeThe function is used to create a pipeline to achieve inter-process communication. The definition of the pipe function is as follows: The parameter in the header file function interface #include<unistd.h>call function definition is a pointer to an array type with a size of 2. The function returns 0 when successful, and Fill a pair of open file descriptor values ​​into the array pointed to by the pipefd parameter , return -1 and set the error code (errno) on failureint pipe(int pipefd[2]); pipepipefd

The code is as follows:
insert image description here
So at this time, through the above code, we have built the pipeline shown in the figure below (the essence is: in one process, open two files, one for reading and one for writing way to open)
insert image description here

It should be noted that the pipe interface parameter is an output parameter at this time, the purpose is to obtain the file descriptor of the opened file, so that when we build one-way communication in the subsequent sequence, the unnecessary file descriptor between processes Close, and pay attention: the parameter of the pipe interface is an array pointer. Since the array is born in the form of an address, the parameter of the pipe interface is an address at this time, that is, an output parameter, which changes the value of the array in the pipe interface , outside the interface, the array will also change. At this time, after the call of the pipe interface is completed, the file descriptors of the two open files will be copied to the array, so after the interface is successfully called, the array's Subscript 0 and subscript 1 indicate the file descriptor of the opened file, but at this time because we want to build one-way communication (pipeline requirements), so after creating the child process (fork), we need to fork the process Unnecessary fd is closed, and because the file descriptor tables of the two processes are exactly the same, we can default that the subscript 0 of the array at this time is the file descriptor opened in read mode, and the subscript 1 is opened in write mode file descriptor , and if, at this time, the child process transmits data to the parent process, then at this time, the subscript 0 of the child process, that is, the file opened for reading, is closed, and then the subscript 1 of the parent process is closed. , that is, the file opened by writing is closed (in any case, the essence is to close one of the files opened by two processes for reading and writing, but it must be a different one), so we have built such a one-way communication channel. The details are shown in the following figure and code:
insert image description here
At this time, according to the above code, we can get the environment for inter-process communication as shown in the figure below. At this time, according to the principle of the figure below and the code in the figure above, data transmission can be performed between processes at this time wait for action
insert image description here

The following is the knowledge we will learn next. Due to time constraints, the next blog will be done

System V IPC (native communication standard)

After understanding the above knowledge, let's get to know the System V inter-process communication annotations, including the relevant knowledge of inter-process communication message queues, shared memory, semaphores and other standards!

message queue

For details, first look at this link: 1. Detailed explanation of message queue knowledge 2. Understanding of message queue related knowledge 3. Complementary knowledge understanding of message queue
These three linked articles are enough for us to understand what message queue is

Shared memory

amount of signal

POSIX IPC (Internet Communication Standard)

Message queues, shared memory, semaphores, mutexes, condition variables, read-write locks

Beijing time: 2023/4/13/7:49, eight people early! Run
insert image description here

Summary: This blog could be posted yesterday, unfortunately, due to my own problems, vscode has been working for a long time, but the problem is finally solved, so the problem is not big, hahaha! And found that inter-process communication is a big knowledge point, especially the knowledge points of the process are already many, now with communication, the road ahead is difficult!

Guess you like

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