Unit4 Threads
Unit4 Threads
Motivati
on
Most modern applications are
multithreaded
Multiple
Threads tasks
run within application
with the application can be implemented
separate
by threads
Process creation is heavy-weight while thread creation is
light-
weight
Can simplify code, increase
efficiency
Kernels are generally multithreaded
Multithreaded Server
Architecture
Word Processor:
A programmer wish to type the text in word processor. Then the
programmer opens a file in a word processor and typing the text
(It is a thread), the text is automatically formatting (It is
another thread). The text automatically specifies the spelling
mistakes (It is another thread), and the file is automatically
saved in the disk (It is another thread).
Benefi
ts
Responsiveness – may allow continued execution if
part of process is blocked, especially important for user
interfaces
Resource Sharing – threads share resources of
process, easier than shared memory or message
passing
Economy – cheaper than process creation, thread
switching lower overhead than context switching
Scalability – process can take advantage of
multiprocessor architectures
Single and Multithreaded
Processes
Why Threads?
Light Weight
Efficient Communication between the entities
Efficient Context Switching
Thread vs Process
Thread States in Operating Systems
https://computing.llnl.gov/tutorials/pt
hreads/
User Threads and Kernel
Threads
User threads - management done by
user-level threads library
Three primary thread libraries:
POSIX Pthreads
Windows threads
Java threads
Kernel threads - Supported by the Kernel
Examples – virtually all general purpose
operating systems, including:
Windows
Solaris
Linux
Tru64 UNIX
Mac OS X
Multithreading
Models
Many-to-One
One-to-One
Many-to-Many
Many-to-
One
Many user-level threads mapped to single kernel
thread
One thread blocking causes all to block
Multiple threads may not run in parallel on
muticore system because only one may be in
kernel at a time
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
One-to-
One
Each user-level thread maps to kernel thread
Creating a user-level thread creates a kernel thread
More concurrency than many-to-one
Number of threads per process sometimes restricted
due to overhead
Examples
Windows
Linux
Solaris 9 and later
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
Solaris prior to version 9
Windows with the ThreadFiber package
Two-level
Model
Similar to M:M, except that it allows a user thread to
be
bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier