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

Chapter 6 Threads

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

Chapter 6: Threads

 Overview
 Multithreading Models
 Threading Issues
 Pthreads
 Solaris 2 Threads
 Windows 2000 Threads
 Linux Threads
 Java Threads
l

Operating System Concepts


Single and Multithreaded Processes

Operating System Concepts


Thread Overview

 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.
 As shown in Figure 4.1, 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


Motivation
 Threads are very useful in modern programming whenever
a process has multiple tasks to perform independently of
the others.
 This is particularly true when one of the tasks may block,
and it is desired to allow the other tasks to proceed without
blocking.
 For example in a word processor, a 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.

Operating System Concepts


Motivation

 Another example is a 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.
 ( The latter is how this sort of thing was done before the
concept of threads was developed. A daemon would
listen at a port, fork off a child for every incoming request
to be processed, and then go back to listening to the port.
)

Operating System Concepts


Benefits
 There are four major categories of benefits to multi-threading:
 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


4 Multicore Programming

 A recent trend in computer architecture is to produce chips with


multiple cores, or CPUs on a single chip.
 A multi-threaded application running on a traditional single-core chip
would have to interleave the threads, as shown in Figure 4.3. and 4.4
below. On a multi-core chip, however, the threads could be spread
across the available cores, allowing true parallel processing, as
shown in Figure 
 For operating systems, multi-core chips require new scheduling
algorithms to make better use of the multiple cores available.
 As multi-threading becomes more pervasive and more important
( thousands instead of tens of threads ), CPUs have been developed
to support more simultaneous threads per core in hardware.

Operating System Concepts


Operating System Concepts
Multithreading Models

 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts


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


Many-to-One Model

Operating System Concepts


One-to-One

 The one-to-one model creates a separate kernel thread


to handle each user thread.
 One-to-one model overcomes the problems listed above
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


One-to-one Model

Operating System Concepts


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


Many-to-Many Model

Operating System Concepts


Thread Libraries
 Thread libraries provide programmers with an API for creating
and managing threads.
 Thread libraries may be implemented either in user space or in
kernel space.
 The user space involves API functions implemented solely
within user space, with no kernel support. The kernel space
involves system calls, and requires a kernel with thread library
support.
 There are three main thread libraries in use today:
 POSIX Pthreads - may be provided as either a user or kernel
library, as an extension to the POSIX standard.
 Win32 threads - provided as a kernel-level library on Windows
systems.
 Java threads - Since Java generally runs on a Java Virtual
Machine, the implementation of threads is based upon whatever
OS and hardware the JVM is running on, i.e. either Pthreads or
Win32 threads depending on the system.
Operating System Concepts
Pthreads

 a POSIX standard (IEEE 1003.1c) API for thread creation


and synchronization.
 API specifies behavior of the thread library,
implementation is up to development of the library.
 Common in UNIX operating systems.

Operating System Concepts


Threading Issues

 Semantics of fork() and exec() system calls.


 Thread cancellation.
 Signal handling
 Thread pools
 Thread specific data

Operating System Concepts


Windows 2000 Threads

 Implements the one-to-one mapping.


 Each thread contains
- a thread id
- register set
- separate user and kernel stacks
- private data storage area

Operating System Concepts


Linux Threads

 Linux refers to them as tasks rather than threads.


 Thread creation is done through clone() system call.
 Clone() allows a child task to share the address space of
the parent task (process)

Operating System Concepts


Java Threads

 Java threads may be created by:

 Extending Thread class


 Implementing the Runnable interface

 Java threads are managed by the JVM.

Operating System Concepts


Solaris 2 Threads

Operating System Concepts


Solaris Process

Operating System Concepts


References

 Abraham Silberschatz, Greg Gagne, and Peter Baer


Galvin, "Operating System Concepts, Ninth Edition ",
Chapter 4
 https://www.cs.uic.edu/~jbell/CourseNotes/
OperatingSystems/4_Threads.htm

Operating System Concepts

You might also like