Operating System-Thread (2), operatingsystem
This topic describes the thread model.
1. Multi-Process-Parallel vs Multi-Thread-Parallel
Multi-process Parallelism: the CPU switches between multiple processes, and the system gives the 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. Dedicated resources of processes and threads
As mentioned in the previous article, threads of the same process share the resources of the process. The thread introduced in the process is to allow multiple execution threads to share resources and coordinate the work to complete the task.
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
After a process is started, it contains at least one thread. This thread can create a new thread through system calling. To create a new thread, you only need to specify the method (Procedure) to be executed by this thread ), you do not need to specify the address space of the new thread, because the new thread runs automatically 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.