What is the difference between threads and processes (brief introduction)

Source: Internet
Author: User
Tags message queue semaphore

A brief introduction to the basic concepts of threading

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 does not own the system resources, and only has the resources necessary to run it, 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, enabling application concurrency so that each processor is fully operational

Process and thread process: each process has its own code and data space (process context), there is a large overhead of switching between processes, and a process contains 1--n threads. (Process is the smallest unit of resource allocation) Threads:The same class of threads share code and data space, each thread has a separate run stack and program counter (PC), and thread switching overhead is small. (Thread is the smallest unit of CPU scheduling)

Threads and processes are divided into five phases:Create, ready, run, block, terminate.

multi-process means that the operating system can run multiple tasks (programs) at the same time. multithreading refers to the execution of multiple sequential streams in the same program.

Each program that is running on the system is a process. Each process contains one or more threads. A process can also be a dynamic execution of an entire program or part of a program. A thread is a set of instructions, or a special section of a program that can be executed independently in a program. It can also be understood as the context in which the code runs. So a thread is basically a lightweight process that is responsible for multitasking in a single program. Typically, the operating system is responsible for scheduling and executing multiple threads.

In Java, an application can contain multiple threads. Each thread performs a specific task and can perform multithreading concurrently with other threads to minimize idling time of the system, improve CPU utilization, and use a convenient model for the multithreaded programming environment to hide the fact that the CPU switches between tasks when a Java program starts, a thread runs immediately, which is often referred to as the main thread of the program.

The importance of the main thread is reflected in two areas:

1, it is the thread that produces the other child threads.

2, usually it must be executed in the final execution, because it performs various closing actions.

Third, the difference between the process and the 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.

Iv. differences 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.

V. Modalities for inter-process communication?

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

Vi. process and thread relationships:

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

Seven, the advantages of multithreading
      • Using threads can put tasks in a program that takes up a long time to work in the background
      • User interface can be more attractive, such as the user clicked a button to trigger the processing of certain events, you can pop up a progress bar to show the progress of processing
      • The program may run faster
      • Threads are useful for tasks such as user input, file read and write, and network send and receive data. In this case, some precious resources such as memory footprint can be freed.
      • Multithreading technology also has an important place in the development of iOS software.
Eight, the disadvantage of multithreading
      • If you have a large number of threads, it can affect performance because the operating system needs to switch between them.
      • More threads require more memory space.
      • Threads may bring more "bugs" to your program, so use them with care.
      • The termination of a thread requires consideration of its impact on the program's operation.
      • Typically, block model data is shared across multiple threads, and it is necessary to prevent thread deadlock situations from occurring.
Summarize:the difference between a thread and a process is that the child process and the parent process have different code and data spaces, while multiple threads share the data space, and each thread has its own execution stack and program counter for its execution context. Multithreading is mainly to save CPU time, play use, depending on the situation. The running of a thread requires the use of the computer's memory resources and CPU.

What is the difference between threads and processes (brief introduction)

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.