Threads and processes

Source: Internet
Author: User

Concepts of processes and threads

Definition of the process:
A process is a dynamic execution of a program on a data set. The process is generally composed of three parts: program, data set and process control block. The program we write is used to describe the
The data set is the resource to be used by the program during execution, and the process control block is used to record the external characteristics of the process, describe the process
The system can use it to control and manage the process, which is the only sign that the system-aware process exists.
Imagine a computer scientist with a good hand in cooking is baking a birthday cake for his daughter. He has recipes for birthday cakes, ingredients for the kitchen: flour, eggs, sugar, vanilla juice, etc. In this analogy, the recipe for making a cake is a program (that is, an algorithm that is described in the proper form), and the computer scientist is the processor (CPU), and the raw material that makes the cake is the input data. The process is the sum of a series of actions that the chef reads recipes, takes a variety of ingredients, and bakes cakes. Now suppose the son of a computer scientist came in crying and said his head was stung by a bee. Computer scientists record what he did to the recipe (save the current state of the process), and then take out a first aid manual and follow the instructions to treat the sting. Here, we see the processor switching from one process (making a cake) to another high-priority process (implementing medical treatment), each with its own program (recipes and First aid manuals). When the Bee sting was finished, the computer scientist came back to make the cake and went on from the step he left.
illustrate the process


Why the thread occurred:
The occurrence of the thread is to reduce the consumption of the context switch, improve the concurrency of the system, and break through a process can only do the same thing, so that in-process concurrency becomes possible.
The concept of threading occurs when multiple processes are processing a resource at the same time, with longer switching times
Multiple threads share a process's resources, operate on resources, and switch efficiently

definition of thread :
A thread is also called a lightweight process, which is a basic CPU execution unit, and is the smallest unit in the process of program execution, which is shared by the thread ID, program counter, register collection, and stack.
Composition The introduction of threads reduces the overhead of program concurrency and improves the concurrency performance of the operating system. The thread does not have its own system resources.
Suppose that a text program needs to accept keyboard input, display the content on the screen, and save the information to the hard drive. If there is only one process, it is bound to cause the same time only the embarrassment of doing the same thing (when saved, you can not enter content through the keyboard). If there are multiple processes, each process is responsible for a task, process A is responsible for receiving keyboard input tasks, and process B is responsible for displaying the content on the screen, and process C is responsible for saving the contents to the hard disk. The collaboration between the process a,b,c involves the process communication problem, and there is something that we all need to have ——- text content, constantly switching to cause loss of performance. If there is a mechanism, you can make the task A,b,c share resources, so that context switches need to save and restore less content, but also reduce the performance of the communication caused by the loss, it is good. Yes, this mechanism is a thread. A thread is also called a lightweight process, which is a basic CPU execution unit and the smallest unit in a program's execution, consisting of a thread ID, a program counter, a register collection, and a stack. The introduction of threads reduces the overhead of program concurrency and improves the concurrency performance of the operating system. The thread does not have its own system resources. 
For example, the thread


The difference between a thread and a process:
Process Resource Management Unit (container) The basic unit of CPU dispatch and dispatch
Minimum thread Execution unit
Relationship of process and thread:
11 threads can belong to only one process, while a process may have multiple threads, but at least one thread
2 Resources are allocated to threads, all the threads of the same process share all the resources of the process
3 CPU is divided into threads, that is, really on the CPU is the thread

Serial, concurrent, and parallel

The difference between concurrency and parallelism:
Concurrency Single-core scenario because high-speed switching seems to perform multiple functions simultaneously (process)
Concurrent processing (concurrency processing): Refers to a time period in which several programs are running to completion, and these programs are all in the same
Processor (CPU) running, but at any point in time only one program runs on the processor (CPU)
Parallel Multi-core scenarios when multiple threads are executing simultaneously
Parallel processing (Parallel processing) is a computational method that can perform two or more processes simultaneously in a computer system. Parallel processing can work simultaneously on the same process
Different aspects of the sequence. The main purpose of parallel processing is to save time to solve large and complex problems.

The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is that you have the ability to handle multiple tasks at the same time. So, parallelism is a subset of concurrency

Threads and processes

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.