Operating System-Thread (1)
Starting the Thread journey, as a programmer, dealing with more threads, various multi-threaded programs, and parallel programming are based on threads. The main content of this article: What and Why Thread process and Thread comparison 1. What and Why Thread 1.1 What Thread is the execution unit inside the process, A process is used as an instance of the running program to put resources together. A thread is a process. A thread exists as an entity executed on the CPU. A thread runs on the CPU, the previous article said that a CPU can only run one process at a time. This is a general statement. The correct statement should be that a CPU can only run one process at a time. Each process has its own independent address space, but all threads in a process share the address space of the process (including data of course ). 1.2 Why first introduces an example. A Word process has three threads (actually there are 30 threads ). One of them is responsible for receiving input from the keyboard, the other is responsible for automatically saving, and the other is responsible for adjusting the format (for example, the Word format should be adjusted immediately after a large segment of text is deleted ). The three threads belong to the same Word process and share all resources, that is, the same Word document. Because the CPU actually runs a thread, these three threads can be parallel. for users, the three operations for Word operations seem to be performed simultaneously. From the perspective of system users, operations on the same resource (process) have also become parallel (in a single-core state, it is actually pseudo-parallel, hypothetical ). Because multiple threads exist, even though the system has been automatically adjusting the format and saving it during Word processing, the user's input never feels delay. In multi-core scenarios, multiple threads have their respective CPUs, And the CPU will never stop scheduling when there is no single-core Delay or the number of cores is small, and resources will be allocated to each thread at intervals, the user will not feel the obvious delay. The preceding example cannot be implemented by a process. If we replace the preceding three threads with three processes, and each process has only one thread to perform the corresponding tasks, what will happen? The three processes run Auto-Save on the thread of the first process of the three Word documents, the Format of the second thread execution in the document actually edited by the user cannot be reflected in the document edited by the current user. The above Word example can also be used in Excel, for example, some table values in Excel are input by the user, and some table values are calculated automatically by input. Here there are two threads, one for receiving and processing user input, the other is used for automatic calculation. The thread is more lightweight than the process, and the overhead of creation and destruction is much smaller than the process. Creating a thread in many systems is 10-times faster than creating a process. 2. Comparison of threads and processes is first shown in the figure above. T1 and T2 are the threads of Process1; T3 and T4 are the threads of Process2. A process is abstracted at a higher level, representing a running program. It is a visible application from the user's perspective, such as a Word, an Excel, and a cursor waiting for input. Processes share various system resources at the system level, such as disks, files, and memory. Parallel Processing Between processes is the most perceptible parallelism for users. Users will say that my computer runs two words at the same time, and an Excel file. The final execution will still be implemented to the thread. The thread is a process and is a real execution unit. The threads inside a process share the address space and resources of the current process, of course, each thread will still have personalized data (which will be specially written for introduction ). The parallel processing of two processes that the user perceives (for example, two Word documents run simultaneously) is actually the Parallel Processing Between the threads inside the two Word documents.