The difference between thread communication and process communication

Source: Internet
Author: User
Tags data structures message queue mutex semaphore

1. Definition

Process: A program with certain independent functions the process is an independent unit of the system's resource allocation and scheduling for a single run activity on a data set. Each process has its own address space. Even though the address values of the two processes are the same, the actual point of location is different. Interprocess communication is generally done through the common areas of the operating system.

Thread: A thread is also known as a lightweight process, an entity of a process, which is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself basically does not own the system resources, only has one point in the running essential resources (such as program counters, a set of registers and stacks), However, it can share all of the resources owned by the process with other threads of the same process. Threads in the same process should be in the same address space and can communicate directly.

2. Relationship

One thread can create and revoke another thread, and can execute concurrently between multiple threads in the same process.

Relative to a process, a thread is a concept that is closer to the execution body, it can share data with other threads in the process, but has its own stack space and has a separate execution sequence.

3. Differences

The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.

1) In short, a program has at least one process, and a process has at least one thread.

2) The thread partition scale is smaller than the process, which makes the multi-thread procedure high concurrency.

3) In addition, the process has a separate memory unit during execution, while multiple threads share memory, which greatly improves the efficiency of the program operation.

4) threads are still different from the process in the execution process. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program. However, threads cannot be executed independently, and must be dependent on the application, which provides multiple threads of execution control.

5) from a logical point of view, the meaning of multithreading is that in an application, there are multiple execution parts that can be executed concurrently. However, the operating system does not consider multiple threads as separate applications to implement scheduling and management of processes and resource allocation. This is the important difference between processes and threads.

4. Pros and cons

Threads and processes have advantages and disadvantages in use:

The thread execution cost is small, but it is not conducive to the management and protection of resources;

And the process is the opposite.

At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.

Inter-thread communication: Because multithreading shares address space and data space, communication between multiple threads is a thread of data that can be provided directly to other threads instead of through the operating system (that is, the kernel's dispatch).

Inter-process communication is different, its data space independence determines that its communication is relatively complex, need to pass the operating system. Previously, interprocess communication can only be a standalone version, and now the operating system inherits the inter-process communication mechanism based on socket (socket). So the communication between processes is not limited to a single computer, the realization of network communication.


There are several ways to communicate between processes:

(1) Nameless pipe (pipe): Pipeline is a half-duplex mode of communication, the data can only flow in one direction, and can only be used between the process of blood relationship. The kinship of a process usually refers to a parent-child process relationship.

(2) famous pipe (named pipe): A well-known pipe is also a half-duplex mode of communication, but it allows inter-process communication without affinity.

(3) Semaphore (Semophore): Semaphore is a counter that can be used to control access to shared resources by multiple processes. It is usually used as a locking mechanism to prevent a process from accessing the shared resource while other processes are accessing it. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.

(4) Message queue: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, and the pipeline can only host unformatted byte streams and buffer size limitations.

(5) signal (signal): signal is a more complex mode of communication for notifying the receiving process that an event has occurred.

(6) Shared memory: Shared memory is the mapping of a section of memory that can be accessed by other processes, which is created by a process, but is accessible by multiple processes, and shared memory is the fastest IPC method, which is designed for inefficient communication between other processes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

(7) Socket: Socket interface is also an inter-process communication mechanism, unlike other communication mechanisms, it can be used for different and inter-process communication.

The communication mechanism between several threads

1. Lock mechanism

1.1 Mutex: Provides a way to block data structures from being concurrently modified in an exclusive manner.

1.2 Read-write Lock: Allows multiple threads to read shared data concurrently, but is mutually exclusive to the write operation.

1.3-Item Variable: The process can be blocked atomically until a particular condition is true. The conditional test is performed under the protection of the mutex. A condition variable is always used with a mutex.

2, semaphore mechanism: including the nameless thread semaphore and the well-known thread signal volume

3, signal mechanism: similar to the process of signal processing.

The primary purpose of the

Inter-thread communication is for thread synchronization, so the thread does not have a communication mechanism for data exchange in process communication.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.