cse333_ch4
cse333_ch4
Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
Threads!
Chapter 4!
Definition of Threads!
Chapter 4 ! 2!
Chapter 4 ! 3!
What is associated with a process?!
Chapter 4 ! 4!
Thread ?!
■ An execution state (running, ready, etc.)!
■ Saved thread context when not running!
■ Separate for each thread!
● Thread ID, PC, register set, scheduling properties!
(TCB = thread control block)!
● User and kernel stack (execution stack; some per-thread
static storage for local variables)!
■ Shared Among Threads!
● Access to the memory and resources of its process !
● User address space (data & code segment)!
● OS resources (open files & signals)!
Chapter 4 ! 5!
Chapter 4 ! 6!
Benefits of Threads!
1. Economy!
■ Takes less time to create a new thread than a
process!
■ Less time to terminate a thread than a
process!
■ Less time to switch between two threads
within the same process!
● Since threads within the same process share
memory and files, they can communicate with
each other without invoking the kernel!
Chapter 4 ! 7!
Benefits of Threads (2)!
2. Responsiveness!
● allow a program to continue running even if
part of it blocked!
3. Resource Sharing!
■ Share memory and resources of the process!
!
Chapter 4 ! 8!
Examples (1)!
■ Foreground to background work!
● web browser = one thread display image /
text; another thread retrieve data from
network!
● spreadsheet program = one thread display
menu & read user input; another execute user
command and update the spreadsheet!
● web server = when it receives a request:!
! create a separate process to service the
request (extra overhead)!
! Solution: multiple threads to serve the
same purpose!
Chapter 4 ! 9!
Examples (2)!
■ Asynchronous processing!
● Word processor: write its RAM buffer to disk once
every minute.!
!
■ Speed of execution!
● Compute one batch of data while reading the next
batch from a device. !
Chapter 4 ! 10!
Suspension and Termination of Threads!
Chapter 4 ! 11!
Thread States!
Chapter 4 ! 12!
Multithreading!
Chapter 4 ! 13!
User-Level Threads!
■ All thread
management is done
by the application!
■ The kernel is not
aware of the existence
of threads!
!
Chapter 4 ! 14!
Chapter 4 ! 15!
Kernel-Level Threads!
■ All contemporary OS
support kernel-level
threads (such as
Windows XP, Linux,
Mac OS) !
■ Kernel maintains
context information for
the process and the
threads!
■ Scheduling is done on
a thread basis!
Chapter 4 ! 16!
Combined Approaches!
■ Example is Solaris!
■ Thread creation done
in the user space!
■ Bulk of scheduling and
synchronization of
threads within
application!
Chapter 4 ! 17!
Multithreading Models
(Relationship between KLT and ULT)!
■ Many-to-One Model!
● Many user-level
threads mapped to
single kernel thread!
!
■One-to-One Model!
!
Chapter 4 ! 18!
Many-to-Many Model!
Chapter 4 ! 19!
Thread Cancellation!
■ Terminating a thread before it has finished!
● Ex: User presses a stop button on a web browser
that stop a web page from loading any further !
Chapter 4 ! 20!
Thread Pools!
■ Advantages:!
● Usually slightly faster to service a request with an
existing thread than create a new thread!
● Allows the number of threads in the application(s)
to be bound to the size of the pool!
Chapter 4 ! 21!
Thread Libraries!
Chapter 4 ! 22!
Pthreads!
Chapter 4 ! 23!
Pthreads Example!
Chapter 4 ! 24!
Pthreads Example (Cont.)!
Chapter 4 ! 25!
Signal Handling!
■ Used in UNIX systems to notify a process that a
particular event has occurred!
■ Synchronuous Signals : delivered to the same process
that performed the operation !
● illegal memory access, division by zero !
■ Asynchronuos Signals: generated by an event external
to a running process!
● terminating a process with specific keystrokes (such
as <control><C>) !
!
Chapter 4 ! 26!
Signal Handling (2)!
■ All signals follow the following pattern:!
1. Signal is generated by particular event!
2. Signal is delivered to a process!
3. Once delivered, the signal must be handled.!
Chapter 4 ! 27!