Chapter 4 : Threads
Thread Examples
•Foreground and background work
–one thread reading user input, another executing the commands and updating the spreadsheet; yet another making periodic backups
•Asynchronous processing(异步处理)
–a thread performing periodic backups against power failures in a word-processor
•Speed of execution
–independent threads can execute in parallel
•Modular program structure
–different tasks/activities in a program may be implemented using different threads
•Client/Server Computing
In an OS that supports threads, scheduling and dispatching is done on a thread basis
Thread Execution States
-
Spawn
when a new process is spawned, a thread for that process is also spawned. Subsequently, a thread within a process may spawn another thread within the same process, providing an instruction pointer and arguments for the new thread. The new thread is provided with its own register context and stack space and placed on the ready queue.
-
Block
When a thread needs to wait for an event, it will block (saving its user registers, program counter, and stack pointers). The processor may now turn to the execution of another ready thread in the same or a different process.
-
Unblock
When the event for which a thread is blocked occurs, the thread is moved to the Ready queue.
-
finish
When a thread completes, its register context and stacks are deallocated.
Threads 分类
ULT
- All thread management is done by the application
- 内核不知道线程的存在,并将进程作为一个单元进行调度,并分配一个单一的执行状态
- Any application can be programmed to be multithreaded by using a threads library which contains code for creating and destroying threads, for passing messages and data between threads, for scheduling thread execution, and for saving and restoring thread contexts.
KLT
- All of the work of thread management is done by the kernel