Operating Systems
Lecture 7
Suad Alaofi 1
Outlines
Threads
Threads Uses
Threads Benefits
Multicore Processor
Multithreading Models
2
Threads
A thread is the smallest unit of instructions that can be managed
independently by a scheduler.
It shares with other threads belonging to the same process its
code section, data section, and other operating-system resources,
such as open files and signals.
One process may contain multiple threads. So, it can perform
more than one task at a time.
The process that has one thread called single-threaded process.
The process that has multiple threads called multithreaded
process
3
Single Vs. Multi threaded process
4
Threads Uses
Many software packages that run on modern desktop PCs are
multithreaded.
Most operating system kernels are now multithreaded; several
threads operate in the kernel, and each thread performs a specific
task.
Web browser might have one thread display images or text while
another thread retrieves data from the network, for example.
A word processor may have a thread for responding to keystrokes
from the user, and another thread for performing spelling and
grammar checking in the background.
5
Threads Benefits
1. Responsiveness:
Multithreading an interactive application may allow a program to
continue running even if part of it is blocked or is performing a
lengthy operation, thereby increasing responsiveness to the user.
For instance, a multithreaded Web browser can allow user
interaction in one thread while an image is being loaded in
another thread.
2. Resource sharing:
Threads share the memory and the resources of the process to
which they belong by default.
6
Cont.
3. Economy:
Allocating memory and resources for process creation is costly.
Because threads share the resources of the process to which they
belong, it is more economical to create and context-switch
threads.
4. Scalability:
Multithreading on amulti- CPU machine increases parallelism.
A single-threaded process can only run on one processor,
regardless how many are available.
7
Multicore Processor
Multicore Processor is a processor that has multiple cores on a
single chip. Each of these cores appears as a separate processor to
the operating system.
Multithreaded programming provides a mechanism for more efficient
use of multiple cores and improved concurrency.
Concurrency means that the threads can run in parallel, because
the system can assign a separate thread to each core.
The trend toward multicore systems has placed pressure on system
designers as well as application programmers to make better use of
the multiple computing cores.
8
Challenges with multicore systems
Challenges in programming for multicore systems:
1. Dividing activities
2. Balance
3. Data splitting
4. Data dependency
5. Testing and debugging
9
Multithreading Models
Support for threads may be provided either at the user level, for
user threads, or by the kernel for kernel threads.
User threads are supported above the kernel and are managed
without kernel support.
kernel threads are supported and managed directly by the operating
system.
A relationship must exist between user threads and kernel threads.
There are three common models of relationships:
1. Many-to-One Model
2. One-to-One Model
3. Many-to-Many Model 10
Many-to-One Model
The many-to-one model maps
many user-level threads to one
kernel thread.
Because only one thread can
access the kernel at a time,
multiple threads are unable to run
in parallel on multiprocessors
11
One-to-One Model
The one-to-one model maps each user
thread to a kernel thread.
It provides more concurrency than the
many-to-one model by allows multiple
threads to run in parallel on
multiprocessors.
The drawback to this model is the overhead
of creating kernel threads can reduce the
performance of an application.
The number of user threads is restricted.
12
Many-to-Many Model
The many-to-many model multiplexes many
user-level threads to a smaller or equal
number of kernel threads.
Developers can create as many user threads
as necessary,
and the corresponding kernel threads can run
in parallel on a multiprocessor.
13