Download presentation
Presentation is loading. Please wait.
Published byDenis Stevenson Modified over 9 years ago
1
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.1 Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1 - 5.3
2
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.2 Operating System Concepts Threads A thread (a lightweight process) is a basic unit of CPU utilization. A thread has a single sequential flow of control. A thread is comprised of: A thread ID, a program counter, a register set and a stack. A process is the execution environment in which threads run. (Recall previous definition of process: program in execution). The process has the code section, data section, OS resources (e.g. open files and signals). Traditional processes have a single thread of control Multi-threaded processes have multiple threads of control The threads share the address space and resources of the process that owns them.
3
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.3 Operating System Concepts Single and Multithreaded Processes
4
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.4 Operating System Concepts Processes vs. Threads Which of the following belong to the process and which to the thread? Program code: local or temporary data: global data: allocated resources: execution stack: memory management info: Program counter: Parent identification: Thread state: Registers: Process Thread Process Thread Process Thread Process Thread
5
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.5 Operating System Concepts Control Blocks The thread control block (TCB) contains: Thread state, Program Counter, Registers PCB' = everything else (e.g. process id, open files, etc.) The process control block (PCB) = PCB' U TCP
6
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.6 Operating System Concepts Why use threads? Because threads have minimal internal state, it takes less time to create a thread than a process (10x speedup in UNIX). It takes less time to terminate a thread. It takes less time to switch to a different thread. A multi-threaded process is much cheaper than multiple (redundant) processes.
7
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.7 Operating System Concepts Examples of Using Threads Threads are useful for any application with multiple tasks that can be run with separate threads of control. A Word processor may have separate threads for: User input Spell check grammar check displaying graphics document layout A web server may spawn a thread for each client Can serve clients concurrently with multiple threads. It takes less overhead to use multiple threads than to use multiple processes.
8
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.8 Operating System Concepts Benefits Responsiveness: Threads allow a program to continue running even if part is blocked. For example, a web browser can allow user input while loading an image. Resource Sharing: Threads share memory and resources of the process to which they belong. Economy: Allocating memory and resources to a process is costly. Threads are faster to create and faster to switch between. Utilization of Multiprocessor Architectures: Threads can run in parallel on different processors. A single threaded process can run only on one processor no matter how many are available.
9
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.9 Operating System Concepts User Threads Thread management may be done at either the user level or the kernel (OS) level. User Threads: Thread management done by user-level threads library The kernel is unaware that the process is multithreaded. Thread creation and scheduling is done in user space without kernel intervention.
10
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.10 Operating System Concepts Many-to-One Model
11
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.11 Operating System Concepts States of User Threads and Processes What happens when a process or thread is blocked? Suppose process B has two threads, of which thread 2 is running. The following conditions could occur: Thread 2 makes an I/O or other system call: The kernel puts process B into a blocked state. The thread data structure will still show thread 2 in the running state. Thread 2 is not actually running. (The entire process is blocked). Process B exhausts its time slice: The kernel puts process B into the ready state. Thread 2 still in running state (but not actually running!) Thread 2 needs action performed by Thread 1: Thread 2 goes into blocked state. Thread 1 starts running. Process B remains in running state.
12
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.12 Operating System Concepts Pros and Cons of User Level Threads: Pros: Fast switch between threads (kernel is not needed). Can have multi-threaded process on a system that does not understand threads. Cheap to create and destroy threads. User has complete control over threads (e.g. can assign priorities, etc.) Cons: System call from one thread blocks all threads in the same process. Process cannot use multiple processors. Extra work for user.
13
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.13 Operating System Concepts User Level Thread Libraries User Level Thread Libraries include: POSIX Pthreads Mach C-Threads Solaris-2 UI threads.
14
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.14 Operating System Concepts Kernel Level Threads Kernel Level Threads: All thread management is done by the Operating System. Each user-level thread maps to kernel thread. Kernel does creation, scheduling and management of threads. Examples - Windows 95/98/NT/2000 - OS/2 - Solaris-2 - Some flavors of UNIX
15
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.15 Operating System Concepts One-to-one Model
16
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.16 Operating System Concepts Pros and Cons of Kernel Level Threads Pros: System call from a thread does not block other threads in the same process. One process can use multiple processors. Create/destroy/switch of threads is less expensive than for processes. Cons: Create/destroy/switch of threads is more expensive than for user level threads. CPU scheduling algorithms are unfair: Each thread is given the same time slice. Tasks with more threads are given more CPU time than those with fewer threads.
17
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.17 Operating System Concepts Combining User and Kernel Level Threads User and Kernel level threads execute simultaneously. Allows many user level threads to be mapped to many kernel threads. If one user level thread is blocked, another kernel thread may be chosen to execute. Assigning a User Level thread to a Kernel Level thread is implicit and hidden from the programmer. Examples of systems with combination of User and Kernel Level threads: Solaris 2 Windows NT/2000 with the ThreadFiber package Some other UNIX flavors.
18
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 5.18 Operating System Concepts Many-to-Many Model
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.