CH 4
CH 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
4.1 Overview
4.2 Multicore Programming
4.3 Multithreading Models
Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Introduction to threads
A thread is a basic unit of CPU utilization, it is also called a light weight
process
It comprises a thread ID, program counter, a register set, and a stack.
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.
A thread behave like a process within a process
It does not have its own PCB
A traditional (or heavyweight process) process has a single thread of control.
If a process has multiple threads of control, it can perform more than one task
at a time.
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Threads
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Threads Examples
A web browser might have one thread display images or text while another thread
retrieves data from the network.
A word processor may have a thread for displaying graphics, another thread for
responding to key strokes from the user, and a third thread for performing spelling
and grammar checking in the background.
In certain situations , a single application may be required to perform several similar
task. E.g. A web server accepts client requests for web pages , images. Sound and
so forth.
One solution is to have the server run as a single process that accepts requests.
When request receive it create separate process to service that request. (process
creation is time consuming and resource intensive).
Solution is multithreaded server. When a request is made , server creates a new
thread to service the request and resume listening for additional requests.
Finally most OS kernels are now multithreaded. Several threads operate in kernel ,
and each thread performs a specific task, such as managing devices , managing
memory , or interrupt handling.
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Motivation
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented by separate threads
Update display
Fetch data
Spell checking
Answer a network request
Process creation method was in common use before threads became
popular. Process creation is heavy-weight while thread creation is light-
weight
Can simplify code, increase efficiency
Kernels are generally multithreaded, several threads operate in the kernel,
and each thread performs a specific task, such as managing devices,
managing memory, interrupt handling.
Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture
Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Benefits
Multithreading allows operating system to execute different threads
simultaneously.
Responsiveness – multithreading in interactive application may allow continue
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, the benefit of sharing code and data is that it
allows an application to have several different threads of activity within the same
address space.
Economy – cheaper than process creation, thread switching lower overhead
than context switching
Scalability – process can take advantage of multiprocessor architectures,
where threads may be running in parallel on different processing cores. A single
threaded process can run on only one processor, regardless how many are
available.
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
Single-CPU systems evolved into multi –CPU system
a recent trend in system design is to place multiple computing cores on a
single chip.
Multithreaded programming provides a mechanism for more efficient use of
these multiple cores and improve concurrency.
Parallelism implies a system can perform more than one task simultaneously
,
Concurrency supports more than one task making progress
Single processor / core, scheduler providing concurrency
Multicore or multiprocessor systems putting pressure on programmers,
challenges include:
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
4.3 Multithreading Models
Threads may be handled at different levels. User level and at kernel
levels.
User threads are implemented in user level libraries instead of system calls
the thread switching does not need to call operating system. It does not
cause interrupt to the kernel. The kernel knows nothing about user-level
threads. It manages user level threads as single threaded processes.
Kernel level threads are supported within the kernel of the operating system.
All modern OS support kernel level threads.
Ultimately a relationship must exist between user threads and kernel
threads.
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
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
Many to one does not true
concurrency
One-one provide greater concurrency
but the developer has to be careful
not to create too many threads within
an application.
Many –many model suffers from
neither of these shortcomings.
Solaris prior to version 9
Windows with the ThreadFiber
package
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013