Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 4: Threads: Silberschatz, Galvin and Gagne ©2009 Operating System Concepts - 8 Edition

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Chapter 4: Threads

Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne


Chapter 4: Threads
 Overview
 Multithreading Models
 Thread Libraries
 Threading Issues
 Operating System Examples
 Windows XP Threads
 Linux Threads

Operating System Concepts – 8th Edition 4.2 Silberschatz, Galvin and Gagne
Objectives
 To introduce the notion of a thread — a fundamental unit of
CPU utilization that forms the basis of multithreaded
computer systems
 To discuss the APIs for the Pthreads, Win32, and Java thread
libraries
 To examine issues related to multithreaded programming

Operating System Concepts – 8th Edition 4.3 Silberschatz, Galvin and Gagne
Single and Multithreaded Processes
 A thread is  a  basic  unit  of  CPU  utilization,  consisting  of  a  program 
counter, a stack, and a set of registers, ( and a thread ID. )
 Traditional  (  heavyweight  )  processes  have  a  single  thread  of  control  - 
There is one program counter, and one sequence of instructions that can 
be carried out at any given time.
 Multi-threaded  applications  have  multiple  threads  within  a  single 
process,  each  having  their  own  program  counter,  stack  and  set  of 
registers,  but  sharing  common  code,  data,  and  certain  structures  such 
as open files.

Operating System Concepts – 8th Edition 4.4 Silberschatz, Galvin and Gagne
Thread vs Process
 Processes are typically independent, while threads exist as
subsets of a process
 Processes carry considerably more state information than
threads, whereas multiple threads within a process share
process state as well as memory and other resources
 Processes have separate address spaces, whereas threads
share their address space
 Processes interact only through system-provided inter-process
communication mechanisms
 Context switching between threads in the same process is
typically faster than context switching between processes.

Operating System Concepts – 8th Edition 4.5 Silberschatz, Galvin and Gagne
Single and Multithreaded Processes

Operating System Concepts – 8th Edition 4.6 Silberschatz, Galvin and Gagne
Motivation
 Threads are very useful in modern programming whenever a
process has multiple tasks to perform independently of the others.
 Required when one of the tasks may block, and it is desired to allow
the other tasks to proceed without blocking.
 Word processor - background thread may check spelling and
grammar while a foreground thread processes user input
( keystrokes ), while yet a third thread loads images from the hard
drive, and a fourth does periodic automatic backups of the file being
edited.
 Web server - Multiple threads allow for multiple requests to be
satisfied simultaneously, without having to service requests
sequentially or to fork off separate processes for every incoming
request.
Operating System Concepts – 8th Edition 4.7 Silberschatz, Galvin and Gagne
Benefits of Threads
 Responsiveness - One thread may provide rapid response
while other threads are blocked or slowed down doing
intensive calculations.
 Resource Sharing - By default threads share common code,
data, and other resources, which allows multiple tasks to
be performed simultaneously in a single address space.
 Economy - Creating and managing threads ( and context
switches between them ) is much faster than performing
the same tasks for processes.
 Scalability  i.e. Utilization of multiprocessor architectures -
A single threaded process can only run on one CPU, no
matter how many may be available, whereas the execution
of a multi-threaded application may be split amongst
available processors. ( Note that single threaded processes
can still benefit from multi-processor architectures when
there are multiple processes contending for the CPU, i.e.
when the load average is above some certain threshold. )
Operating System Concepts – 8th Edition 4.8 Silberschatz, Galvin and Gagne
Benefits
1. 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. For instance, a multithreaded Web browser can allow user
interaction in one thread while an image is being loaded in another
thread.
2. Resource sharing. Processes may only share resources through
techniques such as shared memory or 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.

Operating System Concepts – 8th Edition 4.9 Silberschatz, Galvin and Gagne
Benefits
3. 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.
Empirically gauging the difference in overhead can be difficult, but in
general it is much more time consuming to create and manage
processes than 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.
4. Scalability. The benefits of multithreading can be greatly increased
in a multiprocessor architecture, where threads may be running in
parallel on different processors. A single-threaded process can only run
on one processor, regardless how many are available. Multithreading on
amulti- CPU machine increases parallelism.
Operating System Concepts – 8th Edition 4.10 Silberschatz, Galvin and Gagne
Multithreading Models
 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts – 8th Edition 4.11 Silberschatz, Galvin and Gagne
Many-to-One
 Many user-level threads mapped to single kernel thread

 Examples:

 Solaris Green Threads

 GNU Portable Threads

 GNU Pth (Portable Threads) is a POSIX/ANSI-


C based user space thread library
for UNIX platforms that provides priority-based
scheduling for multithreading applications. GNU
Pth targets for a high degree of portability

Operating System Concepts – 8th Edition 4.13 Silberschatz, Galvin and Gagne
Many-to-One
 In the many-to-one model, many user-
level threads are all mapped onto a single
kernel thread.
 Thread management is handled by the
thread library in user space, which is very
efficient.
 However, if a blocking system call is made,
then the entire process blocks, even if the
other user threads would otherwise be able
to continue.
 Because a single kernel thread can operate
only on a single CPU, the many-to-one
model does not allow individual processes
to be split across multiple CPUs.
 Green threads for Solaris and GNU Portable
Threads implement the many-to-one
model in the past, but few systems
continue to do so today-

Operating System Concepts – 8th Edition 4.14 Silberschatz, Galvin and Gagne
One-to-One
 Each user-level thread maps to kernel thread
 Examples
 Windows NT/XP/2000
 Linux
 Solaris 9 and later

Operating System Concepts – 8th Edition 4.15 Silberschatz, Galvin and Gagne
One-to-one Model
 The one-to-one model creates a
separate kernel thread to handle
each user thread.
 One-to-one model overcomes the
problems involving blocking system
calls and the splitting of processes
across multiple CPUs.
 However the overhead of managing
the one-to-one model is more
significant, involving more overhead
and slowing down the system.
 Most implementations of this model
place a limit on how many threads
can be created.
 Linux and Windows from 95 to XP
implement the one-to-one model for
threads.

Operating System Concepts – 8th Edition 4.17 Silberschatz, Galvin and Gagne
Many-to-Many Model

 Allows many user level threads to be mapped to


many kernel threads
 Allows the operating system to create a sufficient
number of kernel threads
 Solaris prior to version 9
 Windows NT/2000 with the ThreadFiber package

Operating System Concepts – 8th Edition 4.18 Silberschatz, Galvin and Gagne
Many-to-Many Model
 The many-to-many model
multiplexes any number of user
threads onto an equal or smaller
number of kernel threads,
combining the best features of the
one-to-one and many-to-one
models.
 Users have no restrictions on the
number of threads created.
 Blocking kernel system calls do not
block the entire process.
 Processes can be split across
multiple processors.
 Individual processes may be
allocated variable numbers of
kernel threads, depending on the
number of CPUs present and other
factors.

Operating System Concepts – 8th Edition 4.20 Silberschatz, Galvin and Gagne
Two-level Model

 Similar to M:M, except that it allows a user


thread to be bound to kernel thread
 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

Operating System Concepts – 8th Edition 4.21 Silberschatz, Galvin and Gagne
Two-level Model

 One  popular  variation  on  the  many-to-


many  model  still  multiplexes  many  user-
level  threads  to  a  smaller  or  equal 
number of kernel threads but also allows 
a  user-level  thread  to  be  bound  to  a 
kernel thread. 
 This  variation,  sometimes  referred  to  as 
the  two-level  model,  is  supported  by 
operating systems such as IRIX, HP-UX, 
and Tru64 UNIX. 
 The  Solaris  operating  system  supported 
the  two-level  model  in  versions  older 
than Solaris 9. 

Operating System Concepts – 8th Edition 4.22 Silberschatz, Galvin and Gagne
Threading Issues
 Semantics of fork() and exec() system calls
If one thread in a program calls fork(), does the new process duplicate
all threads, or is the new process single-threaded? Some UNIX
systems have chosen to have two versions of fork(), one that
duplicates all threads and another that duplicates only the thread that
invoked the fork() system call.
For exec() system call, if a thread invokes the exec() system call, the
program specified in the parameter to exec() will replace the entire
process—including all threads.
Which of the two versions of fork() to use depends on the
application. If exec() is called immediately after forking, then
duplicating all threads is unnecessary, as the program specified in
the parameters to exec() will replace the process. In this instance,
duplicating only the calling thread is appropriate.
If, however, the separate process does not call exec() after forking,
the separate process should duplicate all threads.

Operating System Concepts – 8th Edition 4.23 Silberschatz, Galvin and Gagne
Threading Issues
Thread cancellation of target thread
Thread cancellation is the task of terminating a thread
before it has completed ( Data base search). A thread
that is to be canceled is often referred to as the target
thread.
Cancellation of a target thread may occur in two
different scenarios:
 Asynchronous cancellation. One thread
immediately terminates the target thread.
 Deferred cancellation. The target thread
periodically checks whether it should terminate,
allowing it an opportunity to terminate itself in an
orderly fashion.

Operating System Concepts – 8th Edition 4.24 Silberschatz, Galvin and Gagne
Threading Issues
Signal handling
A signal is used in UNIX systems to notify a process that a particular event
has occurred. A signal may be received either synchronously or
asynchronously
All signals, whether synchronous or asynchronous, follow the same
pattern:
 A signal is generated by the occurrence of a particular event.
 A generated signal is delivered to a process.
 Once delivered, the signal must be handled.
For e.g illegal memory access and division by 0. If a running program
performs either of these actions, a signal is generated. Synchronous signals
are delivered to the same process that performed the operation that caused
the Signal
When a signal is generated by an event external to a running process, that
process receives the signal asynchronously. Examples of such signals
include terminating a process with specific keystrokes (such as
<control><C>) and having a timer expire. Typically, an asynchronous signal
is sent to another process.
Operating System Concepts – 8th Edition 4.25 Silberschatz, Galvin and Gagne
Threading Issues
Thread pools
Unlimited threads could exhaust system resources, such as CPU
time or memory. The general idea behind a thread pool is to create a
number of threads at process startup and place them into a pool,
where they sit and wait for work.
When a server receives a request, it awakens a thread from this pool
—if one is available—and passes it the request to service. Once the
thread completes its service, it returns to the pool and awaits more
work. If the pool contains no available thread, the server waits until
one becomes free.
Thread pools offer these primary benefits:
 Servicing a request with an existing thread is usually faster than
waiting to create a thread.
 A thread pool limits the number of threads that exist at any one
point. This is particularly important on systems that cannot
support a large number of concurrent threads.

Operating System Concepts – 8th Edition 4.26 Silberschatz, Galvin and Gagne
Threading Issues
Thread specific data
Threads belonging to the same process share the data of the
process. Indeed, this sharing of data provides one of the benefits
of multithreaded programming.
In some circumstances, each thread might need its own copy of
certain data. We will call such data thread-specific data. For
example, in a transaction-processing system, we might service
each transaction in a separate thread. Furthermore, each
transaction may be assigned a unique identifier. To associate
each thread with its unique identifier, we could use thread-
specific data.

Operating System Concepts – 8th Edition 4.27 Silberschatz, Galvin and Gagne
Threading Issues
Scheduler Activation
A final issue to be considered with multithreaded programs concerns
communication between the kernel and the thread library, which may
be required by the many-to-many and two-level models
Such coordination allows the number of kernel threads to be
dynamically adjusted to help ensure the best performance.
One scheme for communication between the user-thread library and
the kernel is known as scheduler activation. It works as follows:
 The kernel provides an application with a set of virtual
processors (LWPs), and the application can schedule user
threads onto an available virtual processor.
 The kernel must inform an application about certain events.
This procedure is known as an upcall

Operating System Concepts – 8th Edition 4.28 Silberschatz, Galvin and Gagne
Thread Cancellation

 Terminating a thread before it has finished


 Two general approaches:
 Asynchronous cancellation terminates the target thread
immediately
 Deferred cancellation allows the target thread to periodically
check if it should be cancelled

Operating System Concepts – 8th Edition 4.29 Silberschatz, Galvin and Gagne
Summary
 A thread is a flow of control within a process. A multithreaded process
contains several different flows of control within the same address space.
The benefits of multithreading include increased responsiveness to the
user, resource sharing within the process, economy, and scalability issues
such as more efficient use of multiple cores.
 User-level threads are threads that are visible to the programmer and are
 unknown to the kernel. The operating-system kernel supports and
manages kernel-level threads. In general, user-level threads are faster to
create and manage than are kernel threads, as no intervention from the
kernel is required.
 Three different types of models relate user and kernel threads: The many-
to-one model maps many user threads to a single kernel thread. The one-
to-one model maps each user thread to a corresponding kernel thread.
The many-to-many model multiplexes many user threads to a smaller or
equal number of kernel threads.

Operating System Concepts – 8th Edition 4.30 Silberschatz, Galvin and Gagne
Summary
 Most modern operating systems provide kernel support for threads;
among these are Windows 98, XP, and Vista, as well as Solaris and
Linux.
 Thread libraries provide the application programmer with an API for
creating and managing threads. Three primary thread libraries are in
common use: POSIX Pthreads,Win32 threads for Windows systems,
and Java threads.
 Multithreaded programs introduce many challenges for the
programmer, including the semantics of the fork() and exec() system
calls. Other issues include thread cancellation, signal handling, and
thread-specific data.

Operating System Concepts – 8th Edition 4.31 Silberschatz, Galvin and Gagne
End of Chapter 4

Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne

You might also like