Multithreading
Overview
• A thread is a single sequential flow of execution of tasks of a process so it is
also known as thread of execution or thread of control.
• Within a program, a Thread is a separate execution path.
• It is a lightweight process (normally a user thread, that can share address
space and resources with other threads, reducing context switching time
during execution.) that the operating system can schedule and run
concurrently with other threads.
• The operating system creates and manages threads, and they shares with
other threads belonging to the same process its code section, data section
and other OS resources, such as open files and signals.
• This enables multiple threads to collaborate and work efficiently within a
single program.
• A thread is a basic unit of CPU utilization.
• Each thread comprises a thread Id, a program counter, a register set and a
stack.
Overview
• A traditional process has a single thread of control. If process has multiple
threads of control, it can perform more than one task at a time.
• Below figure illustrates the difference between a traditional single threaded
process and multithreaded process.
Motivation
• Most modern applications are multithreaded (implemented as a separate
process with several threads of control)
Multiple tasks with the application can be implemented by separate
threads
Ex: Word processor may have a thread for displaying graphics, another thread
for responding to keystrokes 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 tasks.
Ex: web server accepts client requests for web pages, images, sound and so
forth. A busy web server may have several of clients concurrently accessing it.
If web server ran as a traditional single-threaded process, it would be able to
process only one client at a time. The amount of time that a client might have
to wait for request to be serviced could be enormous.
Motivation
• One solution is to have the server run as a single process that accepts
requests. The server would create a separate threads for client requests, when
request was made, rather than creating another process (process creation is
time consuming and resource intensive)
• Thus, threads can simplify code and increase efficiency
• Process creation is heavy-weight while thread creation is light-weight
• Many OS Kernels are now multithreaded; several threads operate in the
kernel, and each thread performs a specific task, such as managing devices or
interrupt handling.
Ex: 1. Solaris creates threads in the kernel for interrupt handling.
2. Linux uses a kernel thread for managing the amount of free memory
in the system.
Benefits
Responsiveness – may allow continued execution if part of process is blocked or
is performing lengthy operation, especially important for user interfaces.
Ex: A multithreaded web browser could still allow user interaction in one thread
while an image was being loaded in another thread.
Resource Sharing – threads share the memory and resources of process to which
they belong, easier than shared memory or message passing
Economy – cheaper than process creation: allocating memory and resources for
process creation and managing processes is time consuming (costly). Because
threads share resources of the process to which they belong, it is more
economical to create and context-switch threads.
Utilization of multiprocessor architecture (Scalability) – The benefits of
multithreading can be greatly increased in multiprocessor architectures where
threads may be running in parallel on processors.
Multithreading Models
• Support for threads may be provided at user level, for user threads, or by the
kernel, for kernel threads.
• User threads are supported and managed without kernel support, whereas
kernel threads are supported and managed directly by the OS.
• There exist a relationship between user threads and kernel threads. There are
3 common ways of establishing this relationship.
1. Many-to-One
2. One-to-One
3. Many-to-Many
1. Many-to-One model:
• It maps many user – level threads to one kernel level thread.
• Thread management is done by thread library in user space,
so, it is efficient, but when a user thread makes a blocking system call entire
process blocks.
Multithreading Models
• Multiple threads may not run in parallel on system because only one may be
in kernel at a time.
• Few systems currently use this model
Examples: Solaris and GNU (Pronounced as g-noo)
2. One-to-One model:
• It maps each user thread to a kernel thread.
• Creating a user-level thread creates a kernel thread
• Provides more concurrency than many-to-one
model by allowing another thread to run when a
threads makes blocking system call
• It allows multiple threads to run in parallel on multiprocessors.
Drawback: Creating a user thread requires creating the corresponding kernel
thread. Because the overhead of creating kernel threads can burden the
performance of an application.
• So, number of threads per process sometimes restricted due to overhead
Examples: Windows, Linux and Solaris 9 and later
Multithreading Models
3. Many-to-Many model:
• Allows many user level threads to be mapped to many kernel threads (small or
equal number of kernel threads)
• The number of kernel threads specific to either a particular
application or a particular machine.
• Developers can create as many user threads as necessary, and
the corresponding kernel threads can run in parallel on a
multiprocessor.
• When a thread performs a blocking system call, the kernel
can schedule another thread for execution.
• It is best Multithreading Models