The following is a brief summary of my understanding of interprocess communication. First of all, be aware of one thing:Each process has an independent address space, so it is impossible to complete the communication between processes in the user mode, and the corresponding operation must be completed with the help of the operating system, that is, the kernel mode.
- Kernel mode and user mode
As can be seen from the figure above, the system is divided into kernel state and user state. The kernel state includes the kernel, and the user state mainly refers to the application. The kernel controls the software and hardware resources of the computer. In order to facilitate the application program to use the related functions provided by the hardware, the operating system provides the system callShell (based on system call) and common function library (based on system call).
- The addresses between processes are independent of each other
Each process has an independent address space, isolated from each other, which is consistent with the processThe status of “the smallest unit of operating system resource allocation”. The independent address space between processes depends on MMU. When Linux operates memory, MMU first queries the page table. If the address reflects the actual physical memory, it will be converted to the actual physical address; The virtual real mapping managed by kernel can’t map the physical memory used by other processes to the virtual address of this process, so the memory space is isolated.
- Interprocess communication
A process can go throughThe kill function sends signals to other processes, and other processes can register relevant function operations for the corresponding signals through the sigaction function.
I think semaphore is actually a lock, which is responsible for protecting critical resources. It is generally used with shared memory. There are mainly two operationsP and V, where operation p is responsible for applying for critical resources, and if the application is successful, the total number of critical resources will be reduced by one; V operation is responsible for releasing critical resources. If the release is successful, the total number of critical resources will be increased by one.
Shared memory is implemented in kernel mode. Depending on system call, multiple processes can map the same virtual memory to the corresponding virtual address space of the process. Shared memory
Socket is generally used for communication between different processes on different computers, but it also supports inter process communication within the machine. Server: socket, bind, listen, accept; Client: socket, connect.
5) Message queuing
With the help of message queue structure provided by kernel, processes can communicate with each other. Asynchronous communication.
6) Named pipe
Similar to message queuing, named pipes can also be used to communicate between two unrelated processes.
- Is message queuing asynchronous communication?
A: message queue belongs to asynchronous communication and has many uses, including decoupling, asynchronous and peak clipping.
Decoupling: similar to the simple factory mode, it decouples the problem of high coupling between the order system and other systems. As shown in the figure below.
Asynchronous: as the name suggests, it can improve the parallelism and reduce the processing time of the system.
Peak shaving: it provides a layer of protection for the system through message queue to prevent the system from running dead due to excessive traffic.
- Why is shared memory the most efficient way of interprocess communication?
A: it can be understood that if the process1 needs to send the content of file 1 to process 2, and process 2 writes the received content to file 2.
Use pipeline, message queue:
A) useThe read function copies the data in file 1 to the temporary buffer in process 1.
B) Will process1 temporary buffer to pipeline or message queue.
C) process2 copy data from a pipeline or message queue to its own buffer.
D) process2 writes the data in the buffer to file 2.
Use shared memory:
A） process1 reads data from file 1 into shared memory.
B） process2 write data from shared memory to file 2.
- How to understandLinux user mode and kernel mode?https://zhuanlan.zhihu.com/p/69554144
- User mode and kernel mode of Linuxhttps://www.cnblogs.com/cyyz-le/p/10962818.html
- [MQ] what is message queue? https://blog.csdn.net/shimazhuge/article/details/93372303
- [operating system] Why is shared memory the fastestWhat about IPC?https://blog.csdn.net/LU_ZHAO/article/details/105237107