Chapter 2: Threads
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 is heavy-weight while thread creation
is light-weight
Can simplify code, increase efficiency
Kernels are generally multithreaded
Prepared By : Dr. Dhruti Sharma, IT, SCET
Thread
A thread is a basic unit of CPU utilization.
It comprises a thread ID, a 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 traditional (or heavyweight) 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.
Prepared By : Dr. Dhruti Sharma, IT, SCET
Single and Multithreaded Processes
Prepared By : Dr. Dhruti Sharma, IT, SCET
Thread
Most modern software applications are multithreaded.
Example :
• Web browser might have one thread to display images or
text while another thread to retrieve data from the network.
• Word processor may have the separate thread for
displaying graphics, for responding to keystrokes, and for
performing spelling and grammar checking in the
background.
• Web server has multiple threads for accepting different
requests (for web pages, images, sound, etc.) coming from
different/same clients.
Prepared By : Dr. Dhruti Sharma, IT, SCET
Thread
Most operating-system kernels are now multithreaded.
Several threads operate in the kernel, and each thread
performs a specific task, such as managing devices,
managing memory, or interrupt handling.
Explore processes and their thread using software
Process explorer:
https://process-explorer.en.softonic.com/
Prepared By : Dr. Dhruti Sharma, IT, SCET
Multithreaded Server Architecture
Prepared By : Dr. Dhruti Sharma, IT, SCET
Benefits of multithreaded programming
Responsiveness – may allow continued execution if part
of process is blocked, especially important for user
interfaces.
Resource Sharing – by default, the threads share
resources of process, easier than shared memory or
message passing implemented by the programmer.
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.
Scalability – benefits of multithreading can be greater in a
multiprocessor architecture, where threads may be
running in parallel on different processing cores.
Prepared By : Dr. Dhruti Sharma, IT, SCET
Multicore Programming
To improve performance of the system, the traditional
single CPU system were replaced with the multi-CPU
systems.
However, the recent trend includes multicore
architecture (multiple CPUs on a single chip) also
known as multiprocessor systems.
Multithreaded programming improves concurrency.
Concurrency
• In case of single-CPU system the execution of the threads
will be interleaved over time.
• In case of multiple cores, the threads can run in parallel,
because the system can assign a separate thread to each core.
Prepared By : Dr. Dhruti Sharma, IT, SCET
Multicore Programming
Distinction between parallelism and concurrency
A system is parallel if it can perform more than one task
simultaneously (with multi-core architecture).
A concurrent system supports more than one task by
allowing all the tasks to make progress (with single-core or
multi-core architecture).
Thus, it is possible to have concurrency without
parallelism.
Prepared By : Dr. Dhruti Sharma, IT, SCET
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Parallelism on a multi-core system:
Prepared By : Dr. Dhruti Sharma, IT, SCET
Multithreading Models
Two types of threads: User level and Kernel level
User threads
• are supported above the kernel and are managed without
kernel support
• management done by user-level threads library.
• Three primary thread libraries:
POSIX Pthreads
Windows threads
Java threads
Prepared By : Dr. Dhruti Sharma, IT, SCET
Multithreading Models
Two types of threads: User level and Kernel level
Kernel threads
• are supported and managed directly by the operating
system.
• virtually all general-purpose operating systems, including:
Windows Linux, Mac OS X, iOS, Android supports kernel
threads.
There must exist some relationship between user threads
and kernel threads.
Prepared By : Dr. Dhruti Sharma, IT, SCET
User and Kernel Threads
Prepared By : Dr. Dhruti Sharma, IT, SCET
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Prepared By : Dr. Dhruti Sharma, IT, SCET
Many-to-One
Many user-level threads mapped to single kernel thread.
Thread management is done by the thread library in user
space, so it is efficient.
Drawback :
• One thread blocking causes all to block.
• One thread can access the kernel at a time, multiple threads
are unable to run in parallel on multicore systems.
Few systems currently use this model.
Examples:
• Solaris Green Threads
• GNU Portable Threads
Prepared By : Dr. Dhruti Sharma, IT, SCET
One-to-One
Each user-level thread maps to kernel thread.
Creating a user-level thread creates a kernel thread.
provides more concurrency than the many-to-one model
by allowing another thread to run when a thread makes a
blocking system call.
Allows multiple threads to run in parallel on
multiprocessors.
Drawback:
• Creating a user thread requires
creating the corresponding kernel thread.
Examples
• Windows, Linux
Prepared By : Dr. Dhruti Sharma, IT, SCET
Many-to-Many Model
It multiplexes many user-level threads to a smaller or
equal number of kernel threads.
The number of kernel threads may be specific to either a
particular application or a particular machine.
Used by Solaris 9 OS
Prepared By : Dr. Dhruti Sharma, IT, SCET
Many-to-Many Model
Many-to-many vs. other models
• Many-to-one model allows the developer to create as many
user threads as she wishes, it does not result in true
concurrency, because the kernel can schedule only one
thread at a time.
• One-to-one model allows greater concurrency, but the
developer has to be careful not to create too many threads
within an application.
• In Many-to-many model, the developers can create as many
user threads as necessary, and the corresponding kernel
threads can run in parallel on a multiprocessor.
• Also, when a thread performs a blocking system call, the
kernel can schedule another thread for execution.
Prepared By : Dr. Dhruti Sharma, IT, SCET
END (Part-2)
Prepared By : Dr. Dhruti Sharma, IT, SCET