Multithreading, multi-process concept, multi-thread, process concept
■ What is multithreading:
Multithreading aims to make multiple threads work in parallel to complete multiple tasks and improve system efficiency. The thread is implemented when multiple tasks need to be completed at the same time.
The advantages of using threads are as follows:
· Use threads to place tasks in programs that occupy a long time in the background for processing
· The user interface can be more attractive. For example, if a user clicks a button to trigger processing of some events, a progress bar will pop up to display the processing progress.
· Program running speed may be accelerated
· When implementing some waiting tasks, such as user input, file read/write, and network data sending and receiving, the threads are swimming. In this case, we can release some precious resources, such as memory usage.
■ What is a multi-process:
A process is an execution activity of a program on a computer. When you run a program, you start a process. Obviously, the program is dead (static), and the process is active (dynamic ). Processes can be divided into system processes and user processes. All the processes used to complete various functions of the operating system are system processes, and they are the operating system itself in the running state. You don't need to talk about the user processes, all processes started by you are user processes. A process is the unit in which the operating system allocates resources.
In Windows, a process is refined into a thread, that is, a process has multiple smaller units that can run independently.
In the same time, if two or more processes are allowed to run in the same computer system, this is a multi-task. Modern Operating Systems are almost all multi-task operating systems that can manage the running of multiple processes at the same time. The benefits of multitasking are obvious. For example, you can listen to mp3 while surfing the internet, and even print downloaded documents without interfering with each other. This involves the issue of parallelism. As the saying goes, it cannot be used at all. This is also true for computers. In principle, a CPU can only be allocated to one process to run the process. We usually use only one CPU in the computer, that is, there is only one heart. To make it single-purpose and multi-process running at the same time, we must use the Concurrency Technology. The Concurrency Technology is quite complex, and the easiest to understand is the "time slice rotation process scheduling algorithm". Its idea is briefly introduced as follows: under the management of the operating system, all running processes use the CPU in turn, and each process can take a very short CPU usage time (for example, 10 ms). In this way, the user cannot feel that the CPU is serving multiple processes in turn, it is as if all processes are running continuously. But in fact, only one process occupies the CPU at any time.
If a computer has multiple CPUs, the situation is different. If the number of processes is smaller than the number of CPUs, different processes can be allocated to different CPUs for running, multiple processes are actually running at the same time, which is parallel. However, if the number of processes is greater than the number of CPUs, you still need to use the Concurrency Technology.
In Windows, CPU allocation is based on threads. A process may consist of multiple threads. The situation is more complicated, but simply put, the relationship is as follows:
Total number of threads <= number of CPUs: Parallel Operation
Total threads> CPU count: Concurrent Operation
The efficiency of parallel operation is obviously higher than that of concurrent operation. Therefore, in multi-CPU computers, the efficiency of multi-task operations is relatively high. However, if you run only one process (thread) on a multi-CPU computer, you cannot take advantage of multiple CPUs.
This involves the problem of multi-task operating systems. The basic principle of multi-task operating systems (such as Windows) is: the operating system allocates CPU time slices to multiple threads, each thread is completed in the time segment specified by the operating system (note that multiple threads here belong to different processes ). the operating system constantly switches from the execution of one thread to the execution of another thread. In the macro view, it seems like multiple threads are executed together. since these threads belong to different processes, it seems to us that multiple processes are executed at the same time, thus implementing multiple tasks. whoops