Threads
Threads
Overview
• A thread is a basic unit of CPU utilization; it comprises a thread ID, a program
counter, a register set, and a stack in Thread Control Block (TCB).
• 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.
• The figure below illustrates the difference between a traditional single-threaded
process and a multithreaded process.
Single and Multithreaded Processes
Multithreading
• The ability of an OS to support multiple, concurrent paths of execution within a
single process - multithreading.
• MS-DOS supports a single user process and a single thread.
• Some UNIX, support multiple user processes but only support one thread per
process
• Java run-time environment is a single process with multiple threads
• Multiple processes and threads are found in Windows, Solaris, and many modern
versions of UNIX
Motivation
• Most software applications that run on modern computers are multithreaded.
• An application typically is implemented as a separate process with several
threads of control. For example:
• 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 keystrokes from the user, and a third thread for performing
spelling and grammar checking in the background.
• Applications can also be designed to leverage processing capabilities on multicore
systems.
• Such applications can perform several CPU-intensive tasks in parallel across the
multiple computing cores.
Motivation
• In certain situations, a single application may be required to perform several
similar tasks.
• For example, a web server accepts client requests for web pages, images,
sound, and so forth.
• A busy web server may have several (perhaps thousands of) clients
concurrently accessing it.
• If the web server ran as a traditional single-threaded process, it would be able
to service only one client at a time, and a client might have to wait a very long
time for its request to be serviced.
• One solution is to have the server run as a single process that accepts
requests.
• When the server receives a request, it creates a separate process to service
that request.
Motivation
• In fact, this process-creation method was in common use before threads became
popular.
• Process creation is time consuming and resource intensive, however.
• If the new process will perform the same tasks as the existing process, why incur
all that overhead?
• It is generally more efficient to use one process that contains multiple threads.
• If the web-server process is multithreaded, the server will create a separate
thread that listens for client requests.
• When a request is made, rather than creating another process, the server
creates a new thread to service the request and resume listening for
additional requests.
Motivation
• Finally, 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.
• For example, Solaris has a set of threads in the kernel specifically for interrupt
handling;
• Linux uses a kernel thread for managing the amount of free memory in the
system.
Process vs Threads
• Process
• A virtual address space which holds the process image
• Protected access to
• Processors, Other processes, Files, I/O resources
• One or More Threads in Process
• Each thread has
• An execution state (running, ready, etc.)
• Saved thread context when not running
• An execution stack
• Some per-thread static storage for local variables
• Access to the memory and resources of its process (all threads of a process share
this)
Single Threaded and Multithreaded process
model
Benefits of Threads
• Takes less time to create a new thread than a process
• Less time to terminate a thread than a process
• Switching between two threads takes less time that switching processes
• Threads can communicate with each other
• without invoking the kernel
• 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.
Benefits of Threads
• Resource sharing.
• Processes can only share resources through techniques such as shared
memory and message passing.
• Such techniques must be explicitly arranged by the programmer.
• However, threads share the memory and the resources of the process to
which they belong by default.
• 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.
Benefits of Threads
• 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.
• In Solaris, for example, creating a process is about thirty times slower than is
creating a thread, and context switching is about five times slower.
• Scalability.
• The benefits of multithreading can be even greater in a multiprocessor
architecture, 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.
Threads
• Several actions that affect all of the threads in a process
• The OS must manage these at the process level.
• Examples:
• Suspending a process involves suspending all threads of the process(ULT)
• Termination of a process, terminates all threads within the process
• Threads have execution states and may synchronize with one another.
• Similar to processes
• We look at these two aspects of thread functionality in turn.
• States
• Synchronization
Thread Execution States
• States associated with a change in thread state
• Spawn (another thread)
• Block
• Issue: will blocking a thread block other, or all, threads
• Unblock
• Ready
• Running
• Finish (thread)
• Deallocate register context and stacks
Thread Synchronization
• All the threads of a process share the same address space and other resources
such as open files.
• Any alteration of a resource by one thread affects the environment of the other
threads in the same process
• It is therefore necessary to synchronize the activities of various threads so that
they do not interfere with each other or corrupt data structures.
• Ex:
• If two threads each try to add an element to a doubly linked list at same time,
one element may be lost or the list may end up malformed.
Types of threads
• User Level Thread (ULT)
• Kernel level Thread (KLT) also called:
• kernel-supported threads
• lightweight processes
User-Level Threads
• All thread management is done by the application
• The kernel is not aware of the existence of threads
• Any application can be programmed to be
multithreaded by using threads library
• By default, application begins with single thread of
execution
• At run time application may spawn a new thread
within the process using thread library
User-Level Threads
• The thread library creates a data structure for new thread and passes control to
one of the threads in ready state within the process using a scheduling algorithm.
• Whenever control passed to thread library, context of the current thread saved
and when control is passed from library to a thread, the context of that thread
(user registers, program counter and stack pointer) restored.
• The kernel unaware of this thread activity continues to schedule the process as a
unit and assigns a single execution state to that process.
Relationships between ULT
Thread and Process States
Relationships between ULT
Thread and Process States
a) Process B executing in its thread 2; and the state of process and its two ULTs
are shown in Figure 4.7(a)
b) The application executing in thread 2 makes a system call that blocks B.
• For eg: I/O call is made.
• Process B now placed in blocked state.
• So process switching happens.
• According to data structure maintained by thread library, thread 2 of process B still in the
Running state.
Relationships between ULT
Thread and Process States
c) A clock interrupt passes control to the kernel, and the kernel determines that
the currently running process has exhausted its time slice.
• The kernel places process B in ready state and switches to another process.
• But according to data structure maintained by thread library, thread 2 of process B is still in
running state.
d) Thread 2 has reached a point where it needs some action performed by thread 1
of process B.
• Thread 2 enters a blocked state and thread 1 transitions from ready to running.
• The process itself remains in the Running state.
Advantages of ULTs
ULTs can
Thread Scheduling can run on
any OS
be application specific