The difference between threads and processes (verbose)

Source: Internet
Author: User
Tags message queue semaphore

1, the basic concept of threading

Concept: A thread is the smallest unit of execution in a process, is an entity in a process, is the basic unit of dispatch and dispatch by the system, the thread itself does not own the system resources, only has a bit of resources that are essential in the operation, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process.

Benefits: (1) Easy scheduling.

(2) Improve concurrency. Concurrency can be easily and efficiently achieved through threading. A process can create multiple threads to perform different parts of the same program.

(3) less overhead. Creating a line turndown creates a process that is fast and requires little overhead.

(4) Facilitate the full use of multi-processor functions. By creating multithreaded processes, each thread runs on a single processor, which enables the concurrency of the application so that each processor is fully operational.

2. The relationship between the basic state and the state of the process

Status: Running, blocking, pending blocking, ready, pending

Transitions between states: ready-to-process, CPU-scheduled execution, and run-state;

A running process, making an I/O request or not getting the requested resource, into a blocking state;

Running process, process execution (or time slice has arrived), become ready state;

The blocked process hangs, becomes a suspended blocking state, when the I/O operation that causes the process to block is completed before the user restarts the process (called Wake-up), the suspend blocking state becomes the suspend-ready state, and when the user restarts the process before the I/O operation is completed, the suspend blocking state becomes the blocking state;

Suspends a process in readiness (or running) into a pending ready state, and when the process resumes, the pending ready state becomes the ready state;

3. What are the relationships and differences between threads and processes?

Relationship of process and thread:

(1) A thread can belong to only one process, while a process may have multiple threads, but at least one thread.

(2) A resource is allocated to a process, and all the threads of the same process share all the resources of that process.

(3) The processor is assigned to a thread, that is, a thread that is actually running on the processing machine.

(4) Threads need to be synchronized during execution. Synchronization is achieved between threads of different processes using the means of message communication. A thread is an execution unit within a process and a scheduler within a process.

The difference between a process and a thread:

(1) Dispatch: The thread acts as the basic unit of dispatch and allocation, and the process as the basic unit of resources

(2) Concurrency: Not only can concurrent execution between processes, but also concurrent execution between multiple threads of the same process

(3) Owning a resource: a process is an independent unit that owns resources, and threads do not own system resources, but they can access resources that belong to the process.

(4) Overhead: When you create or revoke a process, the overhead of the system is significantly greater than the cost of creating or revoking a thread, because the system allocates and reclaims resources for it.

4. How to communicate between processes?

(1) pipe and well-known pipe (named pipe): Pipelines can be used for communication between parent-child processes that have affinity, and in addition to having the functions of a pipeline, a well-known pipeline allows for inter-process communication without affinity.

(2) signal (signal): signal is a simulation of the interrupt mechanism at the software level, it is a more complex mode of communication, used to inform the process that an event occurred, a process received a signal and the processor received an interrupt request can be said to be consistent.

(3) Message queue: Message queue is the link table of the message, it overcomes the disadvantage of limited signal in the two modes of communication, the process with Write permission can add new information to the message queue according to certain rules; a process that has Read permission on a message queue can read information from the message queue.

(4) Shared memory: This can be said to be the most useful inter-process communication method. It allows multiple processes to access the same piece of memory space, and different processes can see in time that the data in the shared memory is updated in the other process. This approach relies on some kind of synchronous operation, such as mutual exclusion and semaphore.

(5) Semaphore (semaphore): primarily as a synchronous and mutually exclusive means between processes and between different threads of the same process.

(6) socket (socket): This is a more general inter-process communication mechanism, it can be used for inter-process communication between different machines in the network, the application is very extensive.

5, the difference between synchronization and mutual exclusion:

When there are multiple threads, it is often necessary to synchronize these threads to access the same data or resources. For example, suppose you have a program where one thread is used to read a file to memory, while another thread is used to count the number of characters in the file. Of course, it is meaningless to count the entire file before it is transferred into memory. However, because each operation has its own thread, the operating system executes each of the two threads as an unrelated task, which may count the number of words when the entire file is not loaded into memory. To work around this problem, you must synchronize two threads.

The so-called synchronization, refers to the walk in a number of different processes between the pieces of the program, their operation must be in strict accordance with the specified sequence of operation, this order depends on the specific task to be completed. If defined with access to resources, synchronization refers to the mutual exclusion (in most cases), through other mechanisms to achieve the visitor's orderly access to resources. In most cases, synchronization has been mutually exclusive, especially if all writes to the resource must be mutually exclusive. In rare cases, multiple visitors can be allowed to access resources at the same time.

The so-called mutex refers to a number of program fragments scattered between different processes, when a process runs one of the program fragments, other processes will not be able to run any of their program fragments, can only wait until the process runs out of this program fragment to run. If defined with access to a resource, it is unique and exclusive for a resource to allow only one visitor to access it at the same time. However, mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.

The difference between threads and processes (verbose)

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.