Brief introduction
The program does not run alone, and the program is assigned resources to run only if it is loaded into memory, and this execution is called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept.
In multi-channel programming, we allow multiple programs to be loaded into memory at the same time, which can be implemented concurrently under the dispatch of the operating system. This is the design that greatly improves the CPU utilization. The advent of the process makes it possible for each user to feel the CPU alone, so the process is proposed for multi-channel programming on the CPU.
Why do threads still have a process?
The process has many advantages, it provides multi-channel programming, let us feel each of us have their own CPU and other resources, can improve the utilization of the computer. Many people do not understand, since the process is so good, why do threads? In fact, careful observation will find that the process is still a lot of defects, mainly reflected in two points:
- A process can only do one thing at a time, and if you want to do two or more things at once, the process is powerless.
- If a process is blocked during execution, such as waiting for input, the entire process hangs, even if some work in the process does not depend on the input data, it cannot be executed.
For example, we use QQ chat, QQ as an independent process if only one thing at the same time, how can he realize at the same moment to be able to monitor the keyboard input, but also can listen to other people send you the message, while still can send someone else's message on the screen? You would say that the operating system is not a tick? But my pro, tick refers to a different process between the time, that is, the operating system to handle your QQ task, and switch to the Word document task, each CPU time slice to your QQ program, your QQ or can only do one thing at the same time.
A bit more straightforward, an operating system like a factory, the factory has a lot of production workshop, different workshops to produce different products, each workshop is equivalent to a process, and your factory is poor, insufficient power, at the same time can only give a workshop power supply, in order to allow all the workshop can be produced at once, your factory Electrician can only give different time-sharing power supply, but it is your QQ workshop, found that only a working workers, the result is very low production efficiency, in order to solve this problem, should do? Yes, you must have thought of, is to add a few workers, let a few artificial people work in parallel, this every worker, is a thread!
Python-Process and thread management