Linux process communication and thread communication

The way of inter-process communication under Linux:

  1. Pipes (Pipe) and named pipes (named pipes)
      can be used for communication between processes with kinship. A well-known pipe overcomes the restriction that pipes have no name. Therefore, in addition to having the functions of pipes, it also allows unrelated processes Intercommunication

  2. Signal (Signal)
      signal is a more complex communication method, used to notify the receiving process that an event has occurred, in addition to inter-process communication, the process can also send signals to the process itself; Linux in addition to supporting Unix early signal semantic function sigal , And also supports the signal function sigaction whose semantic conforms to the Posix.1 standard (in fact, this function is based on BSD. In order to achieve a reliable signal mechanism, BSD can also unify the external interface and reimplement the signal function with the sigaction function)

  3. Message Queue (Message Queue) The
      message queue is a linked list of messages, including the Posix message queue system V message queue. Processes with sufficient permissions can add messages to the queue, and processes with read permissions can read messages from the queue. The message queue overcomes the shortcomings that the signal carries less information, the pipeline can only carry unformatted byte streams, and the buffer size is limited.

  4. Shared memory
      allows multiple processes to access the same memory space, the fastest available form of IPC. It is designed for the low efficiency of other communication mechanisms. Often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes

  5. Semaphore is
    mainly used as a synchronization method between processes and between different threads of the same process.

  6. Socket (Socket) is a
      more general inter-process communication mechanism that can be used for inter-process communication between different machines. It was originally developed by the BSD branch of the Unix system, but now it can generally be ported to other Unix-like systems: both Linux and System V variants support sockets.

Generally speaking, the process under Linux contains the following key elements:

  • There is an executable program
  • Has dedicated system stack space
  • The kernel has its control block (process control block), describing the resources occupied by the process, so that the process can accept the kernel's scheduling
  • With independent storage space

The purpose of communication between threads is mainly used for thread synchronization, so threads do not have a communication mechanism for data exchange like process communication. Communication method between threads in Linux system :

  • Lock mechanism : including mutex lock, condition variable, read-write lock and spin lock.
      Mutexes ensure that only one thread can access a shared resource at a time. When the lock is occupied, the thread that tries to lock it enters the blocking state (releases CPU resources to make it enter the waiting state from the running state). Which waiting thread can obtain the lock when the lock is released depends on the kernel's scheduling.
      Read-write lock Any thread that attempts to lock (whether read or write) is blocked when it is locked in write mode and is in the write state. The "read" thread is not blocked when locked in read mode and in the read state. The "write" thread is blocked. The read mode is shared and the write mode is mutually exclusive.
      Condition variables can block processes atomically until a certain condition is true. The test of the condition is carried out under the protection of the mutex. Condition variables are always used with mutex locks.
      When the spin lock is blocked, the thread is not blocked but polls in a loop to see if the lock can be obtained. There is no thread switching and therefore no switching overhead, but the CPU occupation will result in a waste of CPU resources. So spin locks are suitable for parallel architectures (multiple processors) or for situations where the lock is held for a short time and you do not want to incur overhead during thread switching.

  • Semaphore : including unnamed thread semaphore and named thread semaphore

  • Signal mechanism (Signal) : similar to the signal processing between processes

Published 162 original articles · praised 58 · 90,000 views

Guess you like

Origin blog.csdn.net/ThreeAspects/article/details/104416706