Process of communication

Pipe (pipe)

A pipe is a half-duplex mode of communication in which data flows only one way and can only be used between related processes. Process kinship usually refers to the parent-child process relationship.

NamedPipe

Named pipes are also a half-duplex communication mode, but they allow communication between unrelated processes.

Semaphores (semaphore)

A semaphore is a counter that can be used to control access to a shared resource by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while one process is accessing it. Therefore, it is mainly used as a means of synchronization between processes and between different threads within the same process.

Messagequeue (messagequeue)

Message queues are linked lists of messages stored in the kernel and identified by message queue identifiers. The message queue overcomes the disadvantages of little signal transmission, pipe carrying only plain byte stream and limited buffer size.

Signal (sinal)

Signals are a complex form of communication used to notify a receiving process that an event has occurred.

Shared memory

Shared memory maps a segment of memory that can be accessed by other processes. This segment of shared memory is created by one process but can be accessed by multiple processes. Shared memory is the fastest IPC method and is specifically designed for the low efficiency of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

The socket (socket)

Sockets are also an interprocess communication mechanism that, unlike other communication mechanisms, can be used to communicate between different devices and their processes.

The way threads communicate

Lock mechanism: including mutex, condition variables, read and write lock

Mutex provides an exclusive way to prevent data structures from being modified concurrently. Read/write locks allow multiple threads to read shared data at the same time, while write operations are mutually exclusive. Condition variables can block the process atomically until a particular condition is true. Conditions are tested under the protection of a mutex. Condition variables are always used with mutex.

Wait wait/notify

Wait notification mechanism is based on wait and notify methods to implement, call the wait method of the thread lock object in a thread, the thread will enter the wait queue until notified or woken up.

Why do I have to acquire locks? Because the wait method must release the lock first, an exception will be thrown if the lock is not held.

Wait: Causes the current thread to relinquish the lock and wait until another thread enters the lock and calls notify() or notifyAll() to wake it up. Notify (): Wakes up the first thread that calls wait() while waiting on this synchronization lock. NotifyAll (): Wakes up all threads that call wait() on the synchronization lock.

Volatile memory sharing

Volatile has two characteristics: visibility, which allows threads to communicate, and order, which prevents instruction reordering.

Volatile semantics guarantee thread visibility by two principles

All volatile variables must be flushed to main memory as soon as they are changed by a thread. All volatile variables must be re-read from main memory before being used. Volatile ensures visibility schemata

Working memory 2 can sense the bus by which working memory 1 updates the value of A. Working memory 1 must pass through the bus when refreshing the value of the main memory. The bus can inform other threads that the value has been changed, and then other threads will actively read the value of the main memory to update.

CountDownLatch concurrency tool

CyclicBarrier concurrency tool

Semaphore mechanism

Includes nameless thread semaphore and named thread semaphore.

Signal Mechanism

Similar to signal processing between processes.

The purpose of communication between threads is mainly for thread synchronization, so threads do not have a communication mechanism for data exchange like process communication.

Reference: www.cnblogs.com/fanguangdex…

zhuanlan.zhihu.com/p/129374075