0% found this document useful (0 votes)
7 views

cse333_ch4

Uploaded by

omerdisneyplus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

cse333_ch4

Uploaded by

omerdisneyplus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

The image cannot be displayed.

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!

■ Basic unit of CPU utilization.!


■ Execution context that is independently
scheduled but shares a single address
space with other threads.!
■ Traditional Process = Single threaded
process (single thread of execution per
process – The concept of thread not
recognized)!
■ Multithreaded Process =Multiple threads of
execution.!

Chapter 4 ! 2!
Chapter 4 ! 3!
What is associated with a process?!

■ A virtual address space which holds the process


image!
● It includes:!
!PCB, !
!user-address space (data & codes segments), !
!user and kernel stack!
!OS resources (open files & signals)!
■ Protected access to processor, other processes
(IPC), files & I/O resources. !

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!

■ Suspending a process involves suspending


all threads of the process since all threads
share the same address space!
■ Termination of a process, terminates all
threads within the process!

Chapter 4 ! 11!
Thread States!

■ States (running, ready, blocked)!


■ Basic thread operations associated with a
change in thread state!
● Spawn (spawn another thread)!
● Block!
● Unblock!
● Finish (deallocate register context and stacks)!

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!

■ Allows many user level


threads to be mapped
to many kernel threads!
■ Allows the operating
system to create a
sufficient number of
kernel threads!

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 !

■ Cancellation of a target thread may occur in two


different scenarios:!
● Asynchronous cancellation. One thread
terminates the target thread immediately!
● Deferred cancellation allows the target thread to
periodically check if it should be cancelled!
!

Chapter 4 ! 20!
Thread Pools!

■ Create a number of threads in a pool where


they await work!

■ 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!

■ Thread library provides programmer with


API for creating and managing threads!

■ Two primary ways of implementing!


● Library entirely in user space!
● Kernel-level library supported by the OS!

Chapter 4 ! 22!
Pthreads!

■ May be provided either as user-level or kernel-level!

■ A POSIX standard (IEEE 1003.1c) API for thread


creation and synchronization!

■ API specifies behavior of the thread library,


implementation is up to development of the library!

■ Common in UNIX operating systems (Solaris, Linux,


Mac OS X)!
!

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.!

■ Handling Signals in multithreaded programs!


● Deliver the signal to the thread to which the signal applies!
● Deliver the signal to every thread in the process!
● Deliver the signal to certain threads in the process!
● Assign a specific thread to receive all signals for the process!

Chapter 4 ! 27!

You might also like