Several ways to communicate between IPC processes

Interprocess communication is the dissemination or exchange of information between different processes, so what medium exists between different processes that can be accessed by both parties? The user space of the process is independent of each other and generally cannot access each other, the only exception is the shared memory area. However, the system space is "public space", so the kernel can obviously provide such conditions. Other than that, that's a <strong>peripheral</strong> that both parties can access. In this sense, two processes can of course also exchange information through ordinary files on disk, or through certain entries and records in the "registry" or other database. This is also a means of inter-process communication in a broad sense, but this is generally not counted as "inter-process communication". Because the efficiency of those communication methods is too low, and people's requirements for inter-process communication are to have a certain <strong>real-time</strong>. 


  Inter-process communication mainly includes pipes, system IPC (including message queues, semaphores, shared storage), and SOCKET.

There are three types of pipes:
1) Ordinary pipe PIPE usually has some limitations. One is half-duplex, which can only be transmitted in one direction; the other is that it can only be used between parent and child processes. 
2) Stream pipeline s_pipe: removes the first limitation and can transmit in both directions. 
3) Named pipes: name_pipe, removes the second limitation and can communicate between many unrelated processes.
  The three methods of system IPC are similar, and they all use the identifier in the kernel to identify. 
# Pipe: Pipe is a half-duplex communication method, data can only flow in one direction, and can only be used between processes with affinity. The affinity of a process usually refers to a parent-child process relationship.
# Named pipe: Named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
# Semaphore ( semaphore ): A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while a process is accessing the resource. Therefore, it is mainly used as a synchronization method between processes and between different threads in the same process.
# Message queue (message queue): The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. Message Queuing overcomes the shortcomings of less information in signaling, pipes can only carry unformatted byte streams, and limited buffer size.
# Signal ( sinal ): A signal is a relatively complex communication method used to notify the receiving process that an event has occurred.
# Shared memory ( shared memory ): Shared memory is to map a piece of memory that can be accessed by other processes. This shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest way of IPC, and it's designed for the inefficiencies of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
# Socket (socket): The socket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for different and inter-process communication.


FAQ1: What is the relationship between pipes, file descriptors and file pointers? 
  Answer: In fact, the use of pipes is similar to that of files, and common IO functions such as read, write, and open can be used. Pipe descriptors are similar to file descriptors. In fact, the descriptors, file pointers and file descriptors used by pipes are ultimately All are converted into SOCKET descriptors in the system. They are all limited by the SOCKET descriptors in the system kernel. In essence, the pipes in the LINUX kernel source code are implemented through empty files.


FAQ2: How to use pipes? 
  Answer: There are mainly the following methods: 1)pipe, create a pipe and return 2 pipe descriptors. Usually used for communication between parent and child processes. 2)popen, pclose: This method only returns one pipe descriptor, commonly used On the other side of the communication is stdin or stdout; 3) mkpipe: named pipe, for interaction between many processes.


FAQ3: What is the pros and cons between pipeline and system IPC? 
  Answer: Pipes: The advantage is that all UNIX implementations support it, and the pipe is completely removed after the last process accessing the pipe terminates; the disadvantage is that the pipe only allows one-way transmission or is used between parent and child processes.


   System IPC: The advantage is that it is powerful and can communicate between unrelated processes; the disadvantage is that the keyword KEY_T uses the kernel identifier, occupies kernel resources, and can only be explicitly deleted, and some mechanisms of SOCKET cannot be used, For example select, epoll, etc.


FAQ4: What is the relationship between WINDOS inter-process communication and LINUX inter-process communication? 
  Answer: In fact, most of WINDOS process communication is transplanted to UNIX, WINDOS clipboard, file mapping, etc. can find shadows from the shared storage of UNIX process communication. 


FAQ5: What is the relationship between inter-process communication and inter-thread communication? 
  Answer: Because the entity running on WINDOWS is a thread, the inter-process communication in the narrow sense actually refers to the communication between threads belonging to different processes. The thread synchronization problem between a single process can be classified as a special process communication. It The system calls supported by the kernel are used to maintain synchronization between threads. Some thread synchronization methods commonly used include: Event, Mutex, Semaphore, critical section resources, etc.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480110&siteId=291194637