Communication between processes and between threads

Communication between processes and between threads

0X00 Communication method between processes

0X01 Overview

Each process has its own address space, so from the perspective of resource occupation, different processes are independent of each other, that is, there are obvious boundaries. So compared to threads, communication is a bit more difficult. The more commonly used methods are: pipes , message queues , semaphores , signals , shared memory , sockets

0X02 pipeline

Pipes mainly include unnamed pipes and named pipes.
Pipes can be used for communication between parent and child processes with kinship. In addition to the functions of pipes, a well-known pipe also allows communication between unrelated processes

Ordinary pipe PIPE

1. It is half-duplex (that is, data can only flow in one direction), with a fixed read and write ends.
2. It can only be used for communication between processes that have a relationship (also parent-child processes or brothers) Between processes)
3. It can be regarded as a special file, and ordinary read, write and other functions can also be used for its reading and writing. But it is not an ordinary file, does not belong to any other file system, and only exists in memory .

Named pipe FIFO

1) FIFO can exchange data between unrelated processes
2) FIFO has a path name associated with it, and it exists in the file system in the form of a special device file.

0X03 message queue

The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. The message queue overcomes the shortcomings of less signal transmission information , the pipeline can only carry unformatted byte streams, and the limited buffer size .

Features:
1) The message queue is record-oriented , and the messages in it have a specific format and a specific priority.
2) The message queue is independent of the sending and receiving process. When the process terminates, the message queue and its contents will not be deleted.
3) The message queue can realize random query of messages. The messages do not have to be read in the first-in first-out order, and they can also be read according to the type of message.

0X04 semaphore

The semaphore is different from the IPC structure already introduced. It is a counter that can be used to control the access of multiple processes to shared resources. Semaphore is used to achieve mutual exclusion and synchronization between processes , not to store data for communication between processes.

Features:
1) Semaphore is used for inter-process synchronization. To transfer data between processes, it needs to be combined with shared memory.
2) The semaphore is based on the PV operation of the operating system, and the program's operations on the semaphore are all atomic operations.
3) Each PV operation on the semaphore is not limited to adding 1 or subtracting 1 to the semaphore value, and can add or subtract any positive integer.
4) Support semaphore group.

0X05 signal (signal)

Signal is a more complicated way of communication, used to notify the receiving process that an event has occurred.
For example, in the child process to send a signal to the parent process, let the parent process to recycle

0X06 Shared Memory (Shared Memory)

It allows multiple processes to access the same memory space , and different processes can see that the data in the shared memory is updated in the other process in time. This method needs to rely on some kind of synchronization operation, such as mutexes and semaphores, etc.

Features:
1) Shared memory is the fastest type of IPC, because the process directly accesses the memory
2) Because multiple processes can operate at the same time, it needs to be synchronized
3) Semaphore + shared memory is usually used in combination, Semaphore is used to synchronize access to shared memory

0X07 socket (SOCKET)

Socket is also an inter-process communication mechanism. Different from other communication mechanisms, it can be used for process communication between different hosts. Can communicate through the network.

0X10 Communication between threads

0X11 Overview

What needs to be stated here is that the thread we are discussing is a thread under the same process, otherwise it will return to the process of communication.
Because the threads are in the same address space, the stack space and register information are different, and other resources are shared, so communication is relatively simple.
Rather than talking about communication, it is more about solving synchronization problems. There are several methods: critical section , mutex , semaphore , event (signal)

0X12 critical section

Access public resources or a piece of code through multi-threaded serialization, which is fast and suitable for controlling data access;

0X13 Mutex (Synchronized/Lock)

With the mutex object mechanism, only the thread that owns the mutex object has the authority to access public resources. Because there is only one mutex, it can be guaranteed that public resources will not be accessed by multiple threads at the same time

0X14 Semaphore (Semphare)

It is designed to control a limited number of user resources. It allows multiple threads to access the same resource at the same time, but it is generally necessary to limit the maximum number of threads that can access this resource at the same time.

0X15 event (signal) (Wait/Notify)

Multi-thread synchronization is maintained by notification operation, and multi-thread priority comparison operations can also be easily implemented

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104319632