Interprocess communication and inter-thread communication

Communication between the threads : Since multithreaded shared address space and data space, so the communication between the plurality of threads is a thread data may be provided directly to other threads, rather than through the operating system.

Communication and synchronization between threads so that the main way lock, signals, semaphores

Inter-process communication is different, the independence of its data space determines its communication is relatively complex and requires the operating system.

Communication mechanisms are: pipes, named pipes, message queues, semaphores, shared space, signals, sockets (socket).

Several major means of inter-process communication in Introduction 1. linux:

Pipes (Pipe) and named pipes (named pipe): pipes can be used to have communication between the kinship process, named pipes to overcome the limitations pipeline with no name, therefore, in addition to a pipe with the functions, it also allows unrelated inter-process communication;

Signal (Signal): the signal is more complex communication mode for receiving the notification process has some event occurs, except for inter-process communication, the process can also send a signal to the process itself; Linux early signal Unix semantics in addition to supporting function sigal but also support signal sigaction function semantics conform Posix.1 standard (in fact, the function is based on the BSD, BSD order to achieve reliable signal mechanism, but also to unify external interface, use sigaction function to re-implement the signal function);

Message (Message) queue (message queue): message queue table is linked message, comprising the message queue Posix system V message queue. Has sufficient rights process can add a message to the queue, was given permission to read a process you can go read messages in the queue. Signal bearing the message queue overcome small amount of information, only the carrier pipe plain byte stream buffer size is limited, and other shortcomings.

Shared Memory: allows multiple processes can access the same memory space, is the fastest form of IPC available. For other communication mechanism is not efficient design. Often, such as semaphores in conjunction with other communication mechanism, to achieve synchronization and mutual exclusion between processes.

Semaphore (semaphore): mainly as a means of inter-process synchronization as well as between the different threads in the same process.

Socket (Socket): more general inter-process communication mechanism can be used for inter-process communication between the different machines. It was originally developed by BSD Unix systems branch out, but they are generally portable to other Unix-like systems: Linux and System V variants support sockets.

The main purpose of communication between threads for thread synchronization, communication mechanism so the thread does not like the process of communication for data exchange.

2. Linux system, inter-thread communication is mainly the following:

Locking mechanism: comprising mutexes, condition variables, read-write locks, and spin locks.

Mutex to ensure the same time only one thread access to shared resources. When the lock is occupied trying to lock them into the threads are blocked state (freeing the CPU to run it into a wait state by the state). When the lock is released which waiting thread can acquire the lock depending on what kernel scheduling.

When the read-write lock in write mode and lock in any thread (either read or write) blocked all attempts to write locked state, when the state in read mode and lock in read mode "read" thread is not blocked. " write "thread blocks. Shared read mode, write mode are mutually exclusive.

Condition variables can block the process atomically, until a certain condition is true so far. Test conditions is carried out under the protection of a mutex. Always use condition variables together with a mutex.

When the thread spin lock locked obstruction blocking but not in the loop polling see if you can get the lock, no thread switch and thus no switching overhead, but the occupation of the CPU will lead to a waste of CPU resources. So spin lock structure for parallel (multiple processors) or a suitable lock to be held a short time without generating overhead in case of desired thread switch.

Semaphore mechanism (Semaphore): includes threads unnamed semaphores and semaphore named thread

Signaling mechanism (Signal): signal processing between similar processes

reference

https://blog.csdn.net/liu5320102/article/details/50764645

https://blog.csdn.net/cneaglelee/article/details/7943021

https://blog.csdn.net/sinat_34166518/article/details/82744410

Published 137 original articles · won praise 44 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_38769551/article/details/105147897