20. Differences between how processes and threads communicate

Differences between how process threads communicate

Each process has its own address space. Addresses in two processes, even if the value is the same, actually point to different locations. Interprocess communication is generally carried out through the common areas of the operating system.

   Threads in the same process can communicate directly because they belong to the same address space.

It is not only an entity that operates independently within the system, but also an entity that competes for resources independently.

Threads are also known as light-weight processes. Threads of the same process share global variables and memory, making it easy and convenient to share data between threads, but it will bring some mutual exclusion problems of shared data.

Xu pairs of programs are written with threads in order to improve efficiency.

The fork of the parent-child process is very expensive, and the communication between the parent-child process requires ipc or other methods to achieve, which is more troublesome. The creation of threads is much less expensive, and threads in the same process share the global memory area, so communication is convenient.

The disadvantages of threads are also caused by its advantages, mainly synchronization, asynchrony and mutual exclusion, which are worth careful design when using.

Only inter-process communication is required. The threads of the same process share the address space . There is no need for communication, but synchronization / mutual exclusion mutex should be done well to protect shared global variables. A thread has its own stack. Synchronization / mutual exclusion are primitives. And inter-process communication, whether it is a signal, a pipe or a shared memory, is guaranteed by the operating system and is a system call .

Inter-thread communication: Since multiple threads share address space and data space, the communication between multiple threads is that the data of one thread can be directly provided to other threads for use without going through the operating system (that is, the scheduling of the kernel ).

The communication between processes is different. The independence of its data space determines that its communication is relatively complicated and needs to go through the operating system.

In the past, the communication between processes could only be a stand-alone version, and now the operating systems have inherited the communication mechanism between processes based on sockets ( sockets ). In this way , the communication between processes is not limited to a single computer, and network communication is realized.

First, the communication method between processes

Inter-process communication, the independence of its data space determines that its communication is relatively complex and needs to be passed through the operating system. In the past, the communication between processes could only be a stand-alone version, but now the operating systems have inherited the communication mechanism between processes based on sockets . In this way, the communication between processes is not limited to a single computer, and network communication is realized.
   
The communication mechanisms of processes mainly include: pipes, named pipes, signals, semaphores, message queues, shared memory, and sockets.

#Pipe : Pipe is a half-duplex communication method, data can only flow in one direction, and can only be used between processes with affinity. The affinity of a process usually refers to a parent-child process relationship.
#Named
pipe (namedpipe): The named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
# Signal (sinal): A signal is a relatively complex communication method used to notify the receiving process that an event has occurred . Signal is the only asynchronous communication mechanism in the inter-process communication mechanism, which can be regarded as asynchronous notification
# Semaphore
(semaphore): A semaphore is a counter that can be used to control the access of multiple processes to shared resources . It is often used as a locking mechanism to prevent other processes from accessing a shared resource while a process is accessing the resource. Therefore, it is mainly used as a synchronization method between processes and between different threads in the same process.
# Message queue ( messagequeue ): The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. Message Queuing overcomes the shortcomings of less information in signaling, pipes can only carry unformatted byte streams, and limited buffer size. # Shared memory (shared memory): Shared memory is to map a piece 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
The IPC method is specially designed for the inefficiency of other inter-process communication methods. It is often used in conjunction with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
#Socket
(socket): The socket solution is also an inter-process communication mechanism. Different from other communication mechanisms, it realizes network communication .

Second, the communication method between threads

1.    Lock mechanism: including mutex, condition variable, read-write lock 

1.     Mutexes provide an exclusive way to prevent concurrent modification of data structures.

2.     Read-write locks allow multiple threads to read shared data at the same time, and write operations are mutually exclusive.

3.     Condition variables can atomically block a process until a certain condition is true. Testing of the condition is performed under the protection of a mutex. Condition variables are always used with mutex locks.

2.    Semaphore : including unnamed thread semaphore and named thread semaphore Signal mechanism ( Signal) : 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 in process communication.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324578297&siteId=291194637