Summary of Linux process/thread communication mode

The main communication methods of processes in the Linux system are as follows:

Process communication method on the same host

   * UNIX inter-process communication methods: including pipes (PIPE), well-known pipes (FIFO), and signals (Signal)

   * System V process communication methods: including semaphore (Semaphore), message queue (Message Queue), and shared memory (Shared Memory)

Process communication mode between network hosts

   * RPC: Remote Procedure Call

   * Socket: Currently the most popular network communication method, a communication method based on TCP/IP protocol.

 

Their characteristics:

  • Pipe (PIPE): Pipe is a half-duplex communication method. Data can only flow in one direction, and it can only be used between processes with kinship (parent and child processes). In addition, the pipe transmits an unformatted byte stream, and the size of the pipe buffer is limited (the pipe buffer exists in memory, and when the pipe is created, a page size is allocated for the buffer).
  • Well-known pipe (FIFO): The well-known pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
  • Signal (Signal): Signal is a more complex communication method used to notify the receiving process that an event has occurred.
  • 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 other processes from accessing shared resources when a process is accessing the resource. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.
  • Message Queue (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.
  • Shared Memory (Shared Memory): Shared memory is to map a section of memory that can be accessed by other processes. This shared memory is created by one process but can be accessed by multiple processes. Shared memory is the fastest IPC method. It is specifically designed for the low efficiency of other inter-process communication methods. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
  • Socket: Socket is also an inter-process communication mechanism. Different from other communication mechanisms, it can be used for process communication between different hosts.

 

The thread communication methods in the Linux system are mainly as follows:

 Lock mechanism: including mutual exclusion locks, condition variables, read-write locks

   Mutex locks provide an exclusive way to prevent concurrent modification of data structures.

   Using condition variables can block the process atomically until a certain condition is true. The conditions are tested under the protection of the mutex lock. Condition variables are always used with mutex locks.

   The read-write lock allows multiple threads to read shared data at the same time, and write operations are mutually exclusive.

 Semaphore: Including unnamed thread semaphore and named thread semaphore semaphore

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

 

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


http://blog.sina.com.cn/s/blog_64b9c6850100ub80.html

Guess you like

Origin blog.csdn.net/hgz_gs/article/details/51890471