5 Threads PDF
5 Threads PDF
5 Threads PDF
CA 7204 2
Operating System Concepts
Threads
• A thread is a basic unit of processing
• Thread ID
• Program counter
• Register set
• Stack
• Code section
• Data section
CA 7204 3
Operating System Concepts
Threads
• A heavyweight process is a process that has a single thread of control
• Can only perform a single task at a time
• Fetch data
• Update display
• Check spelling
CA 7204 4
Operating System Concepts
Single and Multithreaded Processes
CA 7204 5
Operating System Concepts
Threads vs Processes
• A thread cannot live on its own. It • A process has at-least one thread.
needs to be attached to a process • Threads within a process share
• There can be more than one the same I/O, code, files.
thread in a process. • If a process dies, all threads die.
• Each thread has its own stack
CA 7204 6
Operating System Concepts
Benefits - Multithreaded Programming
• Economy – cheaper than process creation, thread switching lower overhead than
context switching
CA 7204 7
Operating System Concepts
Multithreaded Server Architecture
CA 7204 8
Operating System Concepts
Multicore Programming
• Dividing activities - How can an application be divided into separate, concurrent tasks?
• Balance - How can those tasks be divided in such a way that each does an equal amount
of work?
• Data splitting - Can the data for those task be divided for processing on separate CPU
cores?
• Testing and debugging - What is the best way to debug a multithreaded program with
many different execution paths?
• Parallelism implies a system can perform more than one task simultaneously
• Types of parallelism
• Data parallelism – distributes subsets of the same data across multiple cores,
same operation on each
• Consider Oracle SPARC T4 with 8 cores, and 8 hardware threads per core
CA 7204 10
Operating System Concepts
Concurrency vs. Parallelism
CA 7204 11
Operating System Concepts
Who manages threads?
• Two strategies
• User threads
• Thread management done by user level thread library. Kernel knows nothing
about the threads.
• Kernel threads
CA 7204 12
Operating System Concepts
User level threads
• Advantages:
• Disadvantages:
CA 7204 13
Operating System Concepts
Kernel level threads
• Advantages:
• Disadvantages:
CA 7204 14
Operating System Concepts
Thread Models
• Many-to-One
CA 7204 15
Operating System Concepts
Many-to-one model
• Pros:
• Cons:
CA 7204 16
Operating System Concepts
One-to-one mode
• Pros.
• Cons.
CA 7204 17
Operating System Concepts
Many-to-Many model
• Pros: flexible
• Cons: Complex
• Double management
CA 7204 18
Operating System Concepts
Typical usage of threads
CA 7204 19
Operating System Concepts
Thread Libraries
• A thread library provides the programmer with an API for creating and managing threads.
• The first approach is to provide a library entirely in user space with no kernel support. All
code and data structures for the library exist in user space. This means that invoking a
function in the library results in a local function call in user space and not a system call.
operating system. In this case, code and data structures for the library exist in kernel space.
Invoking a function in the API for the library typically results in a system call to the kernel.
• Three main thread libraries are in use today: POSIX Pthreads, Windows, and Java.
CA 7204 20
Operating System Concepts
Thread Libraries
• Pthreads, the threads extension of the POSIX standard, may be provided as either a user-
level or a kernel-level library.
• The Java thread API allows threads to be created and managed directly in Java programs.
• For POSIX and Windows threading, any data declared globally —that is, declared outside
of any function—are shared among all threads belonging to the same process.
• Because Java has no notion of global data, access to shared data must be explicitly
arranged between threads.
• Data declared local to a function are typically stored on the stack. Since each thread has
its own stack, each thread has its own copy of local data.
CA 7204 21
Operating System Concepts
Thread Libraries
• With Asynchronous threading, once the parent creates a child thread, the
parent resumes its execution, so that the parent and child execute concurrently.
Each thread runs independently of every other thread, and the parent thread need
not know when its child terminates. Because the threads are independent, there is
typically little data sharing between threads.
• Synchronous threading occurs when the parent thread creates one or more
children and then must wait for all of its children to terminate before it resumes —
the so-called fork-join strategy. Here, the threads created by the parent perform
work concurrently, but the parent cannot continue until this work has been
completed.
CA 7204 22
Operating System Concepts
Thread Libraries
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 (Solaris, Linux, Mac OS X)
Windows Threads
• Implements the one-to-one mapping
• Each thread contains
• A thread id
• Register set
• Separate user and kernel stacks
• Private data storage area
• The register set, stacks, and private storage area are known as the context of the
threads
CA 7204 23
Operating System Concepts
Thread Libraries
Linux Threads
• clone() allows a child task to share the address space of the parent task (process)
Java Threads
CA 7204 24
Operating System Concepts
Threading issues
• Thread cancellation
• Asynchronous or deferred
• Signal handling
• Synchronous and asynchronous
• Thread pooling
• Thread-specific data
• Create facility needed for data private to thread
CA 7204 25
Operating System Concepts
Threading issues - Semantics of fork() and exec() system calls
• Should only the thread that made the call to fork() be duplicated?
• If planning to call exec() after fork(), then there is no need to duplicate all
of the threads in the calling process
CA 7204 26
Operating System Concepts
Threading issues - Thread cancellation
CA 7204 27
Operating System Concepts
Threading issues - Signal handling
• Signals are used in UNIX systems to notify a process that a particular event
has occurred
• CTRL-C is an example of an asynchronous signal that might be sent to a process
• An asynchronous signal is one that is generated from outside the process that receives
it
• The signal is handled by a signal handler (all signals are handled exactly once)
CA 7204 28
Operating System Concepts
Threading issues - Signal handling
CA 7204 29
Operating System Concepts
Threading issues - Signal handling
• Option 1 - Deliver the signal to the thread to which the signal applies
• Most likely option when handling synchronous signals (e.g. only the thread that
attempts to divide by zero needs to know of the error)
• Likely to be used in the event that the process is being terminated (e.g. a
CTRLC is sent to terminate the process, all threads need to receive this signal
and terminate)
CA 7204 30
Operating System Concepts
Threading issues - Thread pools
• In applications where threads are repeatedly being created/destroyed thread pools might
provide a performance benefit
• Example: A server that spawns a new thread each time a client connects to the system and discards
that thread when the client disconnects
• A thread pool is a group of threads that have been pre-created and are available to do work
as needed Create a number of threads in a pool where they await work.
• Threads may be created when the process starts
• After a thread finishes, it is placed back into a queue until it is needed again
• Avoids the extra time needed to spawn new threads when they’re needed
CA 7204 31
Operating System Concepts
Threading issues - Thread pools
• 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
• If the thread pool is empty, then the process must wait for a thread to re-enter
the pool before it can assign work to a thread
CA 7204 32
Operating System Concepts
Thread pools
CA 7204 33
Operating System Concepts
Threading issues
• Useful when you do not have control over the thread creation process (i.e., when
using a thread pool)
Scheduler activations
CA 7204 34
Operating System Concepts