Several communication methods between threads

1. Several communication methods between threads

1. A pipe is a half-duplex method, where data flows in one direction and can only be used between threads that are related. It is based on the kernel. It can be regarded as a kernel buffer. The exchange of data between the kernel and the user space requires four data copies, because it copies the user space data to the kernel and then from the kernel to the memory. Then copy from the memory to the kernel, and finally to the user space. As for why it is copied to the kernel, it is because the most important data is executed in the memory. When receiving, the data in the pipeline can only be read once in accordance with the principle of first-in-first-out, and the buffer does not exist after reading. It can only carry unformatted byte streams.

2. A named pipe is similar to a pipe, but it supports communication between threads that are not related. The others are similar. The name of the named pipe corresponds to the disk index cut-off point, and all processes can access it.

3. The message queue is a linked list of messages. It is a series of message lists stored in the kernel. It also requires 4 copies to exchange data. Its advantage is that you can specify a specific message type for each message. It needs to be received in the order of the queue.

4. Shared storage is to map a section of memory that can be accessed by other processes. It is created by one process and can be accessed by multiple processes. It only needs to copy three times to transfer data, user space -> memory -> user space.
5. Semaphore, used to control the access of multiple processes to shared resources, which can be used for thread synchronization.

6. Socket, Socket is the cornerstone of communication and the basic operation unit of network communication supporting TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process, including five kinds of information necessary for network communication: the protocol used for the connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol of the remote process port.

7. Signal is a more complicated communication method used to notify the receiving process that an event has occurred.

Guess you like

Origin blog.csdn.net/qq_44688861/article/details/114284804