There are several main ways to communicate threads in a Linux system:
* Lock mechanism: Includes mutex, condition variable, read-write lock
Process communication:
- Pipe: A pipe is a half-duplex mode of communication in which data can flow only one way, and can only be used between processes that have affinity (parent-child processes). In addition, the pipeline transmits an unformatted byte stream, and the size of the pipe buffer is limited (the pipe buffer is present in memory, and the buffer is assigned a page size when the pipeline is created).
- Famous Pipeline (FIFO): A well-known pipe is also a half-duplex communication mode, but it allows for non-affinity interprocess communication.
- Signal (Signal): A signal is a more complex form of communication that notifies the receiving process that an event has occurred.
- Semaphore (Semaphore): 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 a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.
- Message Queuing: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.
- Shared memory: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
- Socket (SOCKET): Socket is also an inter-process communication mechanism, unlike other communication mechanisms, it can be used for process communication between different hosts.
Process communication and thread communication