Chapter 4 Threads

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

Operating

Systems:
Internals
and
Design Chapter 4
Principles Threads
Seventh Edition
By William Stallings
Operating Systems:
Internals and Design Principles

The basic idea is that the several


components in any complex system will
perform particular subfunctions that
contribute to the overall function.
—THE SCIENCES OF THE ARTIFICIAL,
Herbert Simon
Processes and Threads
∗Processes have two characteristics:
Resource Ownership Scheduling/Execution
Process includes a Follows an execution path
virtual address space to that may be interleaved with
hold the process image other processes
 the OS performs a  a process has an execution state
protection function to (Running, Ready, etc.) and a
prevent unwanted dispatching priority and is
interference between scheduled and dispatched by the
processes with respect to OS
resources
Processes and Threads
 Theunit of dispatching is referred to as a
thread or lightweight process
 The unit of resource ownership is referred to
as a process or task
 Multithreading - The ability of an OS to
support multiple, concurrent paths of
execution within a single process
Single Threaded Approaches
 A single thread of
execution per process,
in which the concept of
a thread is not
recognized, is referred
to as a single-threaded
approach
 MS-DOS is an
example
Multithreaded Approaches
 The right half of Figure
4.1 depicts
multithreaded
approaches
 A Java run-time
environment is an
example of a system of
one process with
multiple threads
Processes
 The unit or resource allocation and a unit of protection
 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

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)
Threads vs. Processes
Benefits of Threads

Less time to Threads enhance


terminate a thread efficiency in
than a process Switching between communication
Takes less time two threads takes between programs
to create a new less time than
thread than a switching between
process processes
Thread Use in a
Single-User System
 Foreground and background work
 Asynchronous processing
 Speed of execution
 Modular program structure
Threads
 In an OS that supports threads, scheduling and
dispatching is done on a thread basis

suspending
 Most a process
of the state involves
information suspending
dealing with all
threadsisofmaintained
execution the process
in thread-level data
termination ofstructures
a process terminates all threads
within the process
Thread Execution States

The key states for Thread operations


associated with a
a thread are: change in thread
state are:
 Running
 Spawn
 Ready
 Block
 Blocked
 Unblock
 Finish
RPC Using Single Thread
RPC Using One
Thread per Server
Multithreading
on a
Uniprocessor
Thread Synchronization
 Itis necessary to synchronize the activities of
the various threads
 all threads of a process share the same
address space and other resources
 any alteration of a resource by one
thread affects the other threads in the
same process
Types of Threads

Kernel
User Level
levelThread
Thread(ULT)
(KLT)
User-Level Threads (ULTs)
 All thread
management is
done by the
application
 The kernel is not
aware of the
existence of threads
Relationships Between ULT
States and Process States

Figure 4.6 Examples of the Relationships between User-Level Thread States and Process States
Advantages of ULTs

ULTs can run


on any OS
Scheduling can
be application
specific

Thread
switching does
not require
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
s
y
s
t
Overcoming ULT
e
m
Disadvantages
c
W
r
i
t
i

a
n
g

a
n

l
a
p
p
l
i

l
c
a
t
i
o
n

a
s

i
u
l
t
i
p
l

n
e

p
r
o

t
c
e
s
s
e

o
s

r
a
t
h
e
r

t
h

a
a
n

m
u
l
t
i
p
l
e

t
h
r
e
a
n
o
d
s

n
Kernel-Level Threads (KLTs)
 Threadmanagement is
done by the kernel
 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
 Kernel routines can be multithreaded
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

Table 4.2 Relationship between Threads and Processes


Performance Effect
of Multiple Cores

Figure 4.7 (a) Figure 4.7 (b)


Database Workloads on
Multiple-Processor Hardware

Figure 4.8 Scaling of Database Workloads on Multiple Processor Hardware


Applications That Benefit
 Multithreaded native applications
 characterized by having a small number of highly threaded
processes

 Multiprocess applications
 characterized by the presence of many single-threaded processes

 Java applications
 Multiinstance applications
 multiple instances of the application in parallel
Windows Processes

Processes and services provided by the Windows Kernel are relatively simple and
general purpose

• implemented as objects
• created as new process or a copy of an existing
• an executable process may contain one or more threads
• both processes and thread objects have built-in synchronization
capabilities
Relationship Between
Process and Resource

Figure 4.10 A Windows Process and Its Resources


Process and Thread
Objects
Windows makes use of two types of
process-related objects:

Processes Threads
•an entity •a dispatchable unit
corresponding to a of work that
user job or executes
application that owns sequentially and is
resources interruptible
Windows Process and
Thread Objects
Windows Process Object
Attributes

Table 4.3 Windows Process Object Attributes


Windows Thread Object
Attributes

Table 4.4 Windows Thread Object Attributes


Multithreaded Process

Achieves concurrency without the overhead of using


multiple processes

Threads within the same process can Threads in different processes can
exchange information through their exchange information through
common address space and have access shared memory that has been set
to the shared resources of the process up between the two processes
Thread States

Figure 4.12 Windows Thread States


Symmetric Multiprocessing
Support (SMP)
Thread
s of any Soft Har
process
d
can run Affi
on any Affi
process
or
nity nity
•the dispatcher tries to
•an application
assign a ready thread to the
same processor it last ran restricts thread
on
•helps reuse data still in that execution to
processor’s memory
caches from the previous
certain
execution of the thread processors
Solaris Process
- makes use of four thread-related concepts :

Process
•includes the user’s address space, stack, and
process control block

•a user-created unit of execution within a


User-level Threads process

Lightweight •a mapping between ULTs and kernel threads


Processes (LWP)
•fundamental entities that can be scheduled and
Kernel Threads dispatched to run on one of the system processors
Processes and Threads
in Solaris

Figure 4.13 Processes and Threads in Solaris [MCDO07 ]


Traditional Unix vs Solaris

Figure 4.14 Process Structure in Traditional UNIX and Solaris [LEWI96]


A Lightweight Process (LWP) Data
Structure Includes:
 An LWP identifier
 The priority of this LWP
 A signal mask
 Saved values of user-level registers
 The kernel stack for this LWP
 Resource usage and profiling data
 Pointer to the corresponding kernel thread
 Pointer to the process structure
Solaris Thread States

Figure 4.15 Solaris Thread States


Interrupts as Threads
 Most operating systems contain two fundamental
forms of concurrent activity:
Processes (threads)
• cooperate with each other and Interrupts
manage the use of shared data • synchronized by preventing
structures by primitives that their handling for a period of
enforce mutual exclusion and time
synchronize their execution
Solaris Solution

 Solaris employs a set of kernel threads to handle


interrupts
 an interrupt thread has its own identifier, priority,
context, and stack
 the kernel controls access to data structures and
synchronizes among interrupt threads using mutual
exclusion primitives
 interrupt threads are assigned higher priorities than
all other types of kernel threads
Linux Tasks

A process, or This structure


task, in Linux contains
is represented information
by a task_struct in a number
data structure of categories
Linux
Process/Thread Model

Figure 4.16 Linux Process/Thread Model


Linux Threads

Linux does not A new process is The clone() call


recognize a created by copying creates separate stack
distinction between the attributes of the spaces for each
threads and processes current process process
Linux
Clone ()
Flags
Mac OS X Grand Central
Dispatch (GCD)
 Provides a pool of available threads
 Designers can designate portions of applications,
called blocks, that can be dispatched independently
and run concurrently
 Concurrency is based on the number of cores
available and the thread capacity of the system
Block
 A simple extension to a language
 A block defines a self-contained unit of work
 Enables the programmer to encapsulate complex
functions
 Scheduled and dispatched by queues
 Dispatched on a first-in-first-out basis
 Can be associated with an event source, such as a
timer, network socket, or file descriptor
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

You might also like