Thread communication & Process Communication

Processes and threads difference:

For the process, the child process is a copy of the parent process, obtain a copy of the parent process from the parent process where the data space, heap and stack.

The thread, with respect to the process, is a concept closer to the implementation body, and between other threads can directly share data with the process, but also has its own stack space, an independent sequence.

Thing in common: they can improve concurrency procedures, improve process efficiency and response time. Threads and processes have advantages and disadvantages in the use. Thread execution overhead is relatively small, but not conducive to the management and conservation of resources, but rather the process. At the same time, the thread to run on SMP machines, and the process can migrate across machines.

The fundamental difference between them is that multiple processes in each process has its own address space, the thread is a shared address space. All other differences are generated because of this difference. For example: 
1. Speed. Speed thread produced fast, fast communication, fast switching, because they are in the same address space. 
2. Thread the better resource utilization. 
3. When using public variables or threads of memory required synchronization mechanism, but not the process.

And they are still differences in communication is due to this fundamental causes. 


The difference between the communication system

Because the root cause, in fact, only inter-process communication needs, the same process threads share address space, there is no need to communicate, but to do the synchronization / mutual exclusion to protect shared global variables.

And whether it is interprocess communication signal, pipeline pipe or shared memory are guaranteed by the operating system, system call. 


A communication between the process

1. The duct (pipe): 
the pipe is half-duplex communication mode, data flows only one way, but can only be used in a process having a genetic relationship between. Kinship process usually refers to the process of parent-child relationship.

2. named pipe (namedpipe): 
named pipe is half-duplex communication, but allows no communication between the kinship process.

3. Signal (sinal): 
Signal is a more sophisticated means of communication, an event used to notify the receiving process has occurred.

4. semaphore (semophore): 
semaphore is a counter, a plurality of processes can be used to control access to shared resources. It is often used as a locking mechanism to prevent access to shared resources is a process, other processes can also access the resource. Therefore, the main as well as inter-process synchronization means between the different threads within the same process.

The message queue (messagequeue): 
message queue is a linked list of messages, by message queue identifier stored in kernel. Signaling message queue overcome the less information, only the carrier pipe plain byte stream buffer size is limited, and other shortcomings.

6. The shared memory (shared memory): 
Shared memory is memory mapped a period that can be accessed by other processes, this shared memory created by a process, but can be accessed by multiple processes. IPC shared memory is the fastest way, it is for the other inter-process communication running low efficiency specifically designed. It is often associated with other communication mechanisms such as semaphores, used in conjunction to achieve synchronization and communication between processes.

7. The socket (socket): 
socket is an inter-process communication mechanism, with various other communication mechanism is that it can be used for interprocess communication between different devices and. 


Second, communication between threads

1. The locking mechanism: a mutex, a condition variable, read-write locks 


1. The mutex is provided a method are exclusively prevented from being concurrently modified data structure.

2. The read-write lock allows multiple threads to the shared data simultaneously read, and write operations are mutually exclusive.

3. 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.

2. The signal mechanism (Semaphore): includes threads unnamed semaphores and semaphore named thread 

3. signaling mechanism (Signal): signal processing between similar process is used to notify the receiving thread an event has occurred.

 

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.

Published 407 original articles · won praise 150 · views 380 000 +

Guess you like

Origin blog.csdn.net/ds1130071727/article/details/102802620