Operating Systems: Internals and Design Principles: Threads
Operating Systems: Internals and Design Principles: Threads
Operating Systems: Internals and Design Principles: Threads
Systems:
Internals
and
Design Chapter 4
Principles Threads
Seventh Edition
By William Stallings
Processes and Threads
Traditional processes have two characteristics:
Resource Ownership Scheduling/Execution
Process includes a Follows an execution path that
virtual address space to may be interleaved with other
hold the process image processes
the OS provides protection a process has an execution state
to prevent unwanted (Running, Ready, etc.) and a
interference between dispatching priority and is scheduled
processes with respect to and dispatched by the OS
resources Traditional processes are sequential;
i.e. only one execution path
Processes and Threads
Multithreading - The ability of an OS to
support multiple, concurrent paths of
execution within a single process
The unit of resource ownership is referred to
as a process or task
Theunit of dispatching is referred to as a
thread or lightweight process
Single Threaded Approaches
A single execution path
per process, in which
the concept of a thread
is not recognized, is
referred to as a single-
threaded approach
MS-DOS, some
versions of UNIX
supported only this
type of process.
Multithreaded Approaches
The right half of Figure
4.1 depicts
multithreaded
approaches
A Java run-time
environment is a
system of one process
with multiple threads;
Windows, some
UNIXes, support
multiple multithreaded
processes.
Processes
In a multithreaded environment the process is the unit
that owns resources and the unit of protection.
i.e., the OS provides protection at the process level
Processes have
A virtual address space that holds the process image
Protected
access to
processors
other processes
files
I/O resources
One or More Threads
in a Process
Kernel
User Level
level Thread
Thread (ULT)
(KLT)
4.6a→4.6b
4.6a→4.6c
4.6a→4.6d
Figure 4.6 Examples of the Relationships between User-Level Thread States and Process States
Advantages of ULTs
Thread switching
does not require
kernel mode
privileges (no
mode switches)
Disadvantages of ULTs
Ina typical OS many system calls are blocking
as a result, when a ULT executes a system call,
not only is that thread blocked, but all of the
threads within the process are blocked
In
a pure ULT strategy, a multithreaded
application cannot take advantage of
multiprocessing
Overcoming ULT
Disadvantages
Jacketing
converts a blocking
system call into a non-
blocking system call
Wr
itin
g
an
ap
pli
cat
ion
as
mu
ltip
le
pro
ces
ses
rat
her
tha
n
mu
ltip
le
thr
ea
ds
Kernel-Level Threads (KLTs)
Thread management is
done by the kernel
(could call them KMT)
no thread management
is done by the
application
Windows is an example
of this approach
Advantages of KLTs
The kernel can simultaneously schedule multiple
threads from the same process on multiple
processors
If one thread in a process is blocked, the kernel can
schedule another thread of the same process
Disadvantage of KLTs
The transfer of control from one thread to another
within the same process requires a mode switch to
the kernel
Combined Approaches
Thread creation is done in the
user space
Bulk of scheduling and
synchronization of threads is
by the application
Solaris is an example
Relationship Between
Threads and Processes
Multiprocess applications
characterized by the presence of many single-threaded processes
Java applications
Multi-instance applications
multiple instances of the application in parallel
Summary
User-level threads
created and managed by a threads library that runs in the user space of a process
a mode switch is not required to switch from one thread to another
only a single user-level thread within a process can execute at a time
if one thread blocks, the entire process is blocked
Kernel-level threads
threads within a process that are maintained by the kernel
a mode switch is required to switch from one thread to another
multiple threads within the same process can execute in parallel on a multiprocessor
blocking of a thread does not block the entire process
Process/related to resource ownership
Thread/related to program execution