0% found this document useful (0 votes)
14 views33 pages

CH 3 Process and Thread Part 2

Uploaded by

Mahmoud Elatar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views33 pages

CH 3 Process and Thread Part 2

Uploaded by

Mahmoud Elatar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 3: Process and

Threads

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
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, Windows, and Java
thread libraries
 To cover operating system support for threads in Windows and
Linux

Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Motivation

 Most modern applications are multithreaded


 Threads run within application
 Multiple tasks with the application can be implemented by
separate threads
 Update display
 Fetch data
 Spell checking
 Answer a network request
 Process creation is heavy-weight while thread creation is
light-weight
 Can simplify code, increase efficiency
 Kernels are generally multithreaded

Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Benefits

 Responsiveness – may allow continued execution if part of


process is blocked, especially important for user interfaces
 Resource Sharing – threads share resources of process, easier
than shared memory or message passing
 Economy – cheaper than process creation, thread switching
lower overhead than context switching(CPU switch or process
switch)
 Scalability – process can take advantage of multiprocessor
architectures

Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
 Multicore or multiprocessor systems putting pressure
on programmers, challenges include:
1. Identifying tasks - Examining applications to find activities that can be
performed concurrently.
2. Balance - Finding tasks to run concurrently that provide equal value. I.e.,
don't waste a thread on trivial tasks.
3. Data splitting - To prevent the threads from interfering with one another.
4. Data dependency - If one task is dependent upon the results of another,
then the tasks need to be synchronized to assure access in the proper
order.
5. Testing and debugging - Inherently more difficult in parallel processing
situations, as the race conditions become much more complex and
difficult to identify.

Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
 Parallelism implies a system can perform more than one task
simultaneously
 Concurrency supports more than one task making progress
 Single processor / core, scheduler providing concurrency

 Concurrent execution on single-core system:

 Parallelism on a multi-core system:

Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)

 Types of parallelism
 Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
 Task parallelism – distributing threads across cores, each
thread performing unique operation
 As # of threads grows, so does architectural support for threading
 CPUs have cores as well as hardware threads
 Consider Oracle SPARC T4 with 8 cores, and 8 hardware
threads per core

Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes

Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
 There are two types of threads to be managed in a
modern system:
 User threads - management done by user-level threads
library
 Three primary thread libraries:
 POSIX Pthreads
 Windows threads
 Java threads
 Kernel threads - Supported by the Kernel
 Examples – virtually all general purpose operating systems, including:
 Windows, Solaris, Linux, Tru64 UNIX, and Mac OS X

Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
Multithreading Models

 In a specific implementation, the user threads must be mapped


to kernel threads, using one of the following strategies.
 Many-to-One
 One-to-One
 Many-to-Many

Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Many-to-One

 Many user-level threads mapped to


single kernel thread
 One thread blocking causes all to block
 Multiple threads may not run in parallel
on muticore system because only one
thread may be in kernel at a time
 Few systems currently use this model
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
One-to-One
 Each user-level thread maps to a kernel thread
 Creating a user-level thread creates a kernel thread
 More concurrency than many-to-one
 The only drawback to this model is that :
 creating a user thread requires creating the corresponding kernel
thread. Because the overhead of creating kernel threads can burden
the performance of an application, most implementations of this model
restrict the number of threads supported by the system.
 Examples
 Windows
 Linux
 Solaris 9 and later

Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
 Allows many user level threads to be mapped to a smaller or equal
number of kernel threads.
 The number of kernel threads may be specific to either a particular
application or a particular machine
 Allows the operating system to create a sufficient number of kernel
threads
 Examples
 Solaris prior to version 9
 Windows with the ThreadFiber package

Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Design Effect on Concurrency
 The many-to-one model allows the developer to create as many user
threads as she/he wishes, it does not result in true concurrency, because
the kernel can schedule only one thread at a time.
 The one-to-one model allows greater concurrency, but the developer has to
be careful not to create too many threads within an application to limit
overhead.

Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Design Effect on Concurrency
 The many-to-many model does not suffer from any of
these shortcomings:
 developers can create as many user threads as necessary, and
the corresponding kernel threads can run in parallel on a
multiprocessor.
 Also, when a thread performs a blocking system call, the kernel
can schedule another thread for execution.

Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Design Effect on Concurrency
 One 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
bounded to a kernel thread. This variation is sometimes
referred to as the two-level model.

Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Two-level Model
 Similar to M:M, except that it allows a user thread to be
bounded to kernel thread
 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Thread Libraries

 Thread library provides programmer with API for creating


and managing threads
 Two primary ways of implementing
 Library entirely in user space
 Kernel-level library supported by the OS
 Three main thread libraries are in use today: POSIX
Pthreads, Windows, and Java.

Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
POSIX Pthread Libraries
 Institute of Electrical and Electronics Engineers (IEEE).
 A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization.
 Pthreads, the threads extension of the POSIX standard,
may be provided as either a user-level or a kernel-
level library.
 Specification, not implementation
 API specifies behavior of the thread library, implementation is up
to development of the library
 Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Windows threads Libraries

 The Windows thread library, is a kernel-


level library available on Windows
systems.
 Lab assignment run the Multithreaded C
program using the windows API Listed on
Page 64.

Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Java Threads

 Java threads are managed by the JVM


 Typically implemented using the threads model provided by
underlying OS
 Java threads may be created by:
 Extending Thread class
 Implementing the Runnable interface

Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Java Threads

Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Implicit Threading
 Growing in popularity as numbers of threads increase,
program correctness more difficult with explicit threads
 Creation and management of threads done by compilers
and run-time libraries rather than programmers
 Three methods explored
 Thread Pools
 OpenMP
 Grand Central Dispatch
 Other methods include Microsoft Threading Building
Blocks (TBB), java.util.concurrent package

Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Thread Pools
 Create a number of threads in a pool where they await
work
 Advantages:
 Usually slightly faster to service a request with an
existing thread than create a new thread
 Allows the number of threads in the application(s) to
be bound to the size of the pool
 Separating task to be performed from mechanics of
creating task allows different strategies for running
task
 i.e.Tasks could be scheduled to run periodically
 Windows API supports thread pools:

Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
OpenMP
 OpenMP is a set of compiler directives as
well as an API for programs written in C,
C++, or FORTRAN that provides support
for parallel programming in shared-
memory environments
 Identifies parallel regions – blocks of
code that can run in parallel
#pragma omp parallel
 Create as many threads as there are
cores
#pragma omp parallel for
for(i=0;i<N;i++) {
c[i] = a[i] + b[i];
}
Run for loop in parallel

Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Grand Central Dispatch

 Apple technology for Mac OS X and iOS operating systems


 Extensions to C, C++ languages, API, and run-time library
 Allows identification of parallel sections
 Manages most of the details of threading
 Block is in “^{ }” - ˆ{ printf("I am a block"); }
 Blocks placed in dispatch queue
 Assigned to available thread in thread pool when
removed from queue

Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Operating System Examples

 Windows Threads
 Linux Threads

Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Windows Threads

 Windows implements the Windows API – primary API for Win


98, Win NT, Win 2000, Win XP, Win 7, and Win 10.
 Implements the one-to-one mapping, kernel-level
 Each thread contains
 A thread id
 Register set representing state of processor
 Separate user and kernel stacks for when thread runs in
user mode or kernel mode
 Private data storage area used by run-time libraries and
dynamic link libraries (DLLs)
 The register set, stacks, and private storage area are known as
the context of the thread

Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Windows Threads (Cont.)

 The primary data structures of a thread include:


 ETHREAD (executive thread block) – includes pointer to
process to which thread belongs and to KTHREAD, in
kernel space
 KTHREAD (kernel thread block) – scheduling and
synchronization info, kernel-mode stack, pointer to TEB, in
kernel space
 TEB (thread environment block) – thread id, user-mode
stack, thread-local storage, in user space

Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
Windows Threads Data Structures

Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
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)
 Flags control behavior

 struct task_struct points to process data structures


(shared or unique)

Operating System Concepts – 9th Edition 4.31 Silberschatz, Galvin and Gagne ©2013
‫ على حضوركم‬.. ‫شكرا لكم‬
‫وحسن تعاونكم‬

Operating System Concepts – 9th Edition 4.32 Silberschatz, Galvin and Gagne ©2013
End of Chapter 3

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like