Operating System-Thread (2)
This article mainly introduces Thread model 1, Multi-Process-Parallel vs Multi-Thread-Parallel Multi-Process Parallelism: the CPU switches between multiple processes, the system gives an illusion that multiple processes are executed in parallel. Multi-thread parallel: the CPU switches between multiple threads, and the system gives the illusion that multiple threads are executed in parallel. A process is the basic unit for resource allocation, and a thread is the basic unit for CPU execution. II. The previous article mentioned that the threads of the same process share the resources of the process. The thread introduced in the process is to allow multiple executions (threads) share resources and coordinate the tasks. However, each thread has its own exclusive attribute, as shown in figure. The most important thing is stack. Shows the stack. The stack guarantee of the thread has been called but no program has been returned (the reason why recursion wastes resources is that a large number of program calls are stored in the stack of the running thread ). Stack is used to save execution history. Assume that program A calls program B and program B calls program C and C calls program D. When program D is executed, the data in the stack should be ABCD. After program D is executed, D will go out of the stack, and so on. After A executes and returns, the data on the stack will be cleared. 3. Create a New thread when a process starts, the process contains at least one thread, which can be called by the system to create a new thread, to create a new thread, you only need to specify the method (Procedure) to be executed by the thread, instead of specifying the address space of the new thread, because the new thread will automatically run in the address space of the created thread (that is, the address space of the process ). The created thread can be called a subthread, but hierarchy between threads is not very important.