Overview of processes and threads for the CPython interpreter

Source: Internet
Author: User

The necessary theoretical basis:

# the role of an operating system:    1: Hide ugly Complex hardware interface, provide good abstraction    interface 2: Management, scheduling process, and the competition of more than one process to the hardware order # two multi-channel technology:    1. Background: For the single core, to achieve concurrent    PS:    Now the host is generally multicore, then each core will use multi-channel technology    has 4 CPUs, a program running in CPU1 encountered IO blocking, will wait until the end of the IO and re-Dispatch, will be dispatched to 4    any one of the CPUs, which is determined by the operating system scheduling algorithm.         2. Reuse in space: if there are multiple programs    in memory 3. Time reuse: Multiplexing a CPU time slice       emphasis: Encountering IO cut, taking up CPU time too long also cut, The core is to save the state of the process before it is cut, so that the            next time you switch back, you can continue to run based on where you last cut off.
 What is a process

Process: A process that is in progress or a task. The responsibility for executing the task is CPU.

Example (single core + multi-channel, implementing concurrent execution of multiple processes):

Egon has a lot of tasks to do in a time period: Python's task of preparing lessons, the task of writing a book, the task of making a girlfriend, the task of honoring the king,

But Egon can only do one task at a time (the CPU can only do a single job at an hour), how can I play out the effect of multiple tasks concurrent execution?

Egon prepare a lesson, and then go to talk with Li Jie's girlfriend, then go to play a King glory .... This ensures that each task is in progress.

A program is just a bunch of code, and a process refers to the process of running a program.

Example:

Imagine a computer scientist, who is a good cook, who is baking a birthday cake for his daughter, Hanyyyy.

He has a recipe for birthday cakes,

The kitchen has the necessary raw materials: flour, eggs, leek, garlic and so on.

In this analogy:

the recipe for making a cake is a program . (i.e. an algorithm that is described in the appropriate form)

The computer scientist is the processor (CPU)

and the ingredients for making cakes are 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 computer scientist Egon's son, Alex, ran in crying and said: Xxxxxxxxxxxxxx.

The scientist Egon thought that the task of dealing with his son Alex Sting was more important than the task of making a cake for his daughter Hanyyyy, so

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 from his
Go on with that step.

It should be emphasized that the same program executes two times, which is also two processes

Two what is a thread   

In traditional operating systems, each process has an address space, and there is a control thread by default

The thread as the name implies, is a pipeline work process, a pipeline must belong to a workshop, a workshop work process is a process

The workshop is responsible for the integration of resources together, is a resource unit, and a workshop has at least one pipeline

The line of work requires power, the power is equivalent to the CPU

Therefore, a process is simply used to centralize resources together (a process is just a resource unit, or a resource collection), and the thread is the executing unit on the CPU.

The concept of multi-threaded (that is, multiple control threads) is that there are multiple control threads in a process, and multiple control threads share the process's address space, which is equivalent to a workshop with multiple pipelines, sharing a workshop resource.

For example, the Beijing Metro and the Shanghai Metro are different processes, while the Beijing metro line Line 13 is a thread, the Beijing Metro all the lines share all the resources of the Beijing Metro, such as all passengers can be pulled by all lines.

Thread creation overhead is small

Is the cost of creating a process much larger than a thread?

If our software is a factory, the factory has a number of lines, the pipeline work requires power supply, only one CPU (single core CPU)

A workshop is a process, a workshop at least one pipeline (a process at least one thread)

Creating a process is creating a workshop (application space, building at least one pipeline within that space)

and build a thread, just in a workshop built a pipeline, no need to apply for space, so the creation of a small cost

Between processes is a competitive relationship, between threads is a collaborative relationship?

The workshop is directly competitive/grab the power of the relationship, competition (different processes are directly competitive relationships, are different programmers write programs run, Thunderbolt preempt other processes of speed, 360 of other processes as the virus to dry dead)
A workshop of different assembly line collaborative work relationship (the same process of the thread is a partnership, is the same program written within the program to start, the thread within the Thunderbolt is a cooperative relationship, will not do their own)

Overview of processes and threads for the CPython interpreter

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.