What is the process?
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.
If these two shortcomings are difficult to understand, give a realistic example, perhaps you are clear: if we take the course of the process as a process, then we have to do is ear to listen to the teacher lectures, hand also take notes, the brain still have to think about the problem, so as to effectively complete the task of the lectures. And if only to provide the process of this mechanism, the above three things will not be executed at the same time, can only do one thing, listen to the time can not take notes, and can not use the brain to think, this is one; if the teacher wrote the calculation process on the blackboard, we began to take notes, and the teacher suddenly had a step down, blocked He was thinking over there, and we couldn't do anything else, even if you wanted to think about a problem you didn't understand just now, which is second.
Now you should understand the defects of the process, and the solution is very simple, we can let listen, write, think three independent process, parallel together, so it is obvious to improve the efficiency of lectures. The same mechanism, the thread, is also introduced in the actual operating system.
Advantages of threading
Because of concurrency, we invented the process and further invented the thread. Only the concurrency level of the process and thread is different: The process is an abstraction provided at the processor level, and the thread is a layer of concurrent abstraction at the level of the process. If we enter the computer architecture, we will find that the pipeline provides a kind of concurrency, but instruction-level concurrency. In this way, pipelines, threads, and processes from low to high on three levels to provide the concurrency we desperately need!
In addition to increasing the concurrency of the process, the thread has the advantage of being able to effectively utilize multiprocessor and multicore computers. There is a tendency for processors to evolve toward multicore, and multicore does not allow a process to execute faster without threads, due to all the two-point limits above. However, if a process is broken down into several threads, it is possible to have different threads running on different cores, thus increasing the execution speed of the process.
For example: We often use Microsoft Word for text typesetting, and actually open multiple threads. One of these threads is responsible for displaying, one accepting keyboard input, one for saving, and so on. These threads run together, allowing us to feel that our input and screen display occur simultaneously, rather than entering some characters, which can be seen over a period of time. When we inadvertently, we also carried out automatic disk operation. This is the convenience that the thread brings to us.
The difference between a process and a thread
A process is a program with a certain independent function about a single run activity on a data set, a process that is an independent unit of the system's resource allocation and scheduling.
A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself basically does not own the system resources, only has a point in the operation of the necessary resources (such as program counters, a set of registers and stacks), However, it can share all of the resources owned by the process with other threads that belong to one process.
One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process.
The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.
The difference between a process and a thread