Figure 5.01 - College of the Holy Cross
Download
Report
Transcript Figure 5.01 - College of the Holy Cross
Operating Systems
Lecture 13
Threads
Read Ch 5.1 - 5.3
Operating System Concepts
5.1
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.2
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Single and Multithreaded Processes
Operating System Concepts
5.3
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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:
Operating System Concepts
5.4
Process
Thread
Process
Process
Thread
Process
Thread
Process
Thread
Thread
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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
Operating System Concepts
5.5
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.6
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.7
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.8
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.9
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Many-to-One Model
Operating System Concepts
5.10
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.11
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.12
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
User Level Thread Libraries
User Level Thread Libraries include:
POSIX Pthreads
Mach C-Threads
Solaris-2 UI threads.
Operating System Concepts
5.13
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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
Operating System Concepts
5.14
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
One-to-one Model
Operating System Concepts
5.15
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.16
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
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.
Operating System Concepts
5.17
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Many-to-Many Model
Operating System Concepts
5.18
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005