0% found this document useful (0 votes)
36 views34 pages

Threads Part 2

The document discusses various concepts related to threads including thread states, operations, models, libraries, and issues. It covers the life cycle of threads from born to dead state. Common thread operations like create, exit, suspend, resume are described. Popular threading models like many-to-one, one-to-one, many-to-many are explained. Popular thread libraries including Pthreads, Windows threads, and Java threads are summarized. Issues like signal handling, thread cancellation, and scheduler activations are briefly covered.

Uploaded by

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

Threads Part 2

The document discusses various concepts related to threads including thread states, operations, models, libraries, and issues. It covers the life cycle of threads from born to dead state. Common thread operations like create, exit, suspend, resume are described. Popular threading models like many-to-one, one-to-one, many-to-many are explained. Popular thread libraries including Pthreads, Windows threads, and Java threads are summarized. Issues like signal handling, thread cancellation, and scheduler activations are briefly covered.

Uploaded by

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

Threads-Part 2

Thread States: Life Cycle of a Thread


• Thread states
• Born state
• Ready state (runnable state)
• Running state
• Dead state
• Blocked state
• Waiting state
• Sleeping state
• Sleep interval specifies for how long a thread will sleep
Thread life cycle
Thread Operations
• Threads and processes have common operations
• Create
• Exit (terminate)
• Suspend
• Resume
• Sleep
• Wake
Thread operations
• Thread operations do not correspond precisely to process operations
• Cancel
• Indicates that a thread should be terminated, but does not guarantee that the thread
will be terminated
• Threads can mask the cancellation signal
• Join
• A primary thread can wait for all other threads to exit by joining them
• The joining thread blocks until the thread it joined exits
Thread Models
• Three most popular threading models
• User-level threads
• Kernel-level threads
• Combination of user- and kernel-level threads
User Threads and Kernel Threads
• 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
• Linux
• Mac OS X
• iOS
• Android
User and kernel threads
User Threads and Kernel Threads
Multithreading Models
• Many-to-One

• One-to-One

• Many-to-Many
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 may be in kernel
at a time
One-to-One
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Number of threads per process sometimes restricted due to overhead
• Examples
Windows
Linux
Many-to-Many Model or hybrid 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
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

• Implementation:
• Asynchronous
• Synchronous
Pthreads
• May be provided either as user-level or kernel-level
• A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
• Specification, not implementation
• API specifies behavior of the thread library, implementation is up to
development of the library
• Common in UNIX operating systems (Linux & Mac OS X)
Pthreads Example
Pthreads Example (cont)
Pthreads Code for Joining 10 Threads
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
• Five methods explored
• Thread Pools
• Fork-Join
• OpenMP
• Grand Central Dispatch
• Intel Threading Building Blocks
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
Fork-Join Parallelism
Multiple threads (tasks) are forked, and then joined.
Fork-Join Parallelism
General algorithm for fork-join strategy:
Fork-Join Parallelism
OpenMP
• Set of compiler directives and an API for
C, C++, FORTRAN
• 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
OpenMP

Run the for loop in parallel


Grand Central Dispatch(GCD)
• Apple technology for macOS and iOS operating systems
• Extensions to C, C++ and Objective-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
Intel Threading Building Blocks (TBB)
• Template library for designing parallel C++ programs
• A serial version of a simple for loop

• The same for loop written using TBB with parallel_for


statement:
Threading Issues
• Semantics of fork() and exec() system calls
• Signal handling
• Synchronous and asynchronous
• Thread cancellation of target thread
• Asynchronous or deferred
• Thread-local storage
• Scheduler Activations
Semantics of fork() and exec()
• Does fork()duplicate only the calling thread or all threads?
• Some UNIXes have two versions of fork
• exec()usually works as normal – replace the running process
including all threads
• Which version of fork to use depends on the application.
• Exec() just after fork or not.
Signal handling
 Signals are used in UNIX systems to notify a process that a particular
event has occurred.
 A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. default
2. user-defined
 Every signal has default handler that kernel runs when handling signal
 User-defined signal handler can override default
 For single-threaded, signal delivered to process
Signal
 Where should a signal be delivered for multi-threaded?
 Deliver the signal to the thread to which the signal applies
 Deliver the signal to every thread in the process
 Deliver the signal to certain threads in the process
 Assign a specific thread to receive all signals for the process

 Synchronous: internal to process


 Asynchronous: external to process

 Eg:
 synchronous signal (illegal memory access/division by zero): to be delivered to the specific thread.
 Asynchronous signal(Ctrl C): to be delivered to all threads in process.
Thread Cancellation
• Terminating a thread before it has finished
• Eg: Database search by one thread completes or loading of web page is stopped by user.
• Thread to be canceled is target thread
• Two general approaches:
• Asynchronous cancellation terminates the target thread immediately
• Deferred cancellation allows the target thread to periodically check if it should be cancelled
• Pthread code to create and cancel a thread:
Thread Cancellation
• Check the status of thread for resources, so that it can be cancelled.
• Invoking thread cancellation requests cancellation, but actual cancellation depends on thread
state

• If thread has cancellation disabled, cancellation remains pending until thread enables it
• Default type is deferred
• Cancellation only occurs when thread reaches cancellation point
• I.e. pthread_testcancel()
• Then cleanup handler is invoked
• On Linux systems, thread cancellation is handled through signals
Scheduler Activations
• Both M:M and Two-level models require
communication to maintain the appropriate number of
kernel threads allocated to the application
• Typically use an intermediate data structure between
user and kernel threads – lightweight process (LWP)
• Appears to be a virtual processor on which process can
schedule user thread to run
• Each LWP attached to kernel thread
• How many LWPs to create?
• Scheduler activations provide upcalls - a
communication mechanism from the kernel to the
upcall handler in the thread library
• This communication allows an application to maintain
the correct number kernel threads

You might also like