You are on page 1of 34

Operating Systems

Chapter 4
Dr. Sohail Abbas
Chapter 4: Threads &
Concurrency

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


Motivation
 Most modern applications are multithreaded
 Multithreading a program appears to do more than one thing at
a time
 The idea of Multithreading is same as Multiprogramming i.e.
 Multiprogramming is multi-tasking across different
processes
 Multithreading is multi-tasking within a single process
 E.g., A word processing program has separate threads:
 One for displaying graphics
 Other for reading in keystrokes from the user
 Another to perform spelling and grammar checking in the
background

1501-352 Operating Systems 1.3


Motivation
 Process creation is heavy-weight while thread creation is light-
weight

 Can simplify code,


 coz of being lightweight
 increase efficiency
 thread scheduling is better than process scheduling and
improves concurrency/parallelism

 Kernels are generally multithreaded

1501-352 Operating Systems 1.4


Motivation
 Multithreading allows process to be subdivided into different
threads of control
 A thread is the smallest schedulable unit
 A thread in execution works with
 thread ID
 Registers (program counter and working register set)
 Stack (for procedure/function call parameters, local
variables etc.)
 A thread shares the following with other threads (which belong
to the same process)
 Code section
 Data section (static + heap)

1501-352 Operating Systems 1.5


Single and Multithreaded Processes

1501-352 Operating Systems 1.6


Process vs. Thread
 Like process states, threads also have states:
 New, Ready, Running, Waiting and Terminated

 Like processes, the OS will switch between threads (even if


they belong to a single process) for CPU usage
 Threads (within the same process) share the same address
space, whereas different processes do not.
 Threads are used for small tasks, whereas processes are
used for more 'heavyweight' tasks
 Like process creation, thread creation is supported by APIs
 In C, threads are created using functions in <pthread.h>
library

1501-352 Operating Systems 1.7


Multithreaded Server Architecture

1501-352 Operating Systems 1.8


Benefits
 Responsiveness – Allows a program to continue running even if
parts of it is in “waiting”/blocked state; especially important for
user interfaces

 Resource Sharing – Threads share memory and resources of the


process to which they belong, easier than shared memory or
message passing

 Economy – cheaper than process creation, thread switching got


lower overhead than context switching
 In Solaris, creating a process is about 30 times slower than
is creating a thread,
 Similarly, context switching is about five times slower.

 Scalability – one process cannot scale to take advantage of


multiprocessing system, rather threads are scalable

1501-352 Operating Systems 1.9


Multicore Programming
Parallelism implies a system can perform more than one task simultaneously
Concurrency supports more than one task making progress on a single
processor/core, scheduler providing concurrency

 Concurrent execution on single-core system:

 Parallelism on a multi-core system:

1501-352 Operating Systems 1.10


Multicore Programming
 Multicore or multiprocessor systems putting pressure on
programmers, challenges include:
 Identifying and Dividing tasks
 Balance
 tasks (that will run in parallel) should perform equal work
of equal value
 Data splitting
 Data must be divided to run on separate cores (in addition
to task division)
 Testing and debugging
 Testing and debugging is inherently more difficult in
programs running in parallel on multiple cores

1501-352 Operating Systems 1.11


Multicore Programming
 Data dependency
 For dependent tasks, programmers must ensure that the
execution of the tasks is synchronized.

1501-352 Operating Systems 1.12


Multicore Programming
 Types of parallelism
 Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
 Ex: Summation of an Array of size N on dual core processor
 Data: N/2 – 1 on each core
 Task: Summation
 Task parallelism – distributing threads across cores, each
thread performing unique operation
 Ex: two different operations one for each core
 Operating on same or different data sets

1501-352 Operating Systems 1.13


Multicore Programming
 As # of threads grows, so does architectural support for
threading
 CPUs have cores as well as hardware threads
 Consider Oracle SPARC T4 with 8 (octa=8, quad=4) cores, and
8 hardware threads per core

1501-352 Operating Systems 1.14


Software vs. Hardware Threads
 The core of a processor is the part that executes the
instructions; Each Core™ i7 or Xeon® 5500 series processor
shipping currently has 4 cores

 A software thread is a stream of instructions that the processor


executes;
 A hardware thread (or virtual CPU) is the hardware resources
that execute a software thread
 If the number of threads is higher than the number of cores,
Hyper-threading is enabled.

 In a hyper-threaded single core, CPU has 1 core that is divided


into 2 virtual thread pipelines,
 this allows for 1 thread to continue accepting data on the
same core even if the other thread is waiting for data or
something

1501-352 Operating Systems 1.15


Multithreaded Multicore System
 Multithreaded multicore system
 Each core has more than 1 hardware threads.

 Takes advantage of memory stall to make progress on another


thread while memory retrieve happens
 If one thread has a memory stall, switch to another thread!

1501-352 Operating Systems 1.16


Software vs. Hardware Threads

1501-352 Operating Systems 1.17


User Threads and Kernel Threads
 User threads - management done by user-level threads library
(thread management is done by the application and the kernel
is not aware of the existence of threads.)
 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

1501-352 Operating Systems 1.19


User Threads and Kernel Threads

Note: T2 is not actually


running in the sense of
being executed on a
processor; but it is 1. T2 invokes I/O
perceived as being in the
Running state by the
thread's library.
3. T2 blocks and
2. T2 timer expired causes T1 to
start some action

1501-352 Operating Systems 1.20


Multithreading Models: 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 because it cannot be


used for multicore systems

 Examples:
 Solaris Green Threads
 GNU Portable Threads

1501-352 Operating Systems 1.21


Multithreading Models: 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
 However, number of threads per process sometimes restricted
due to overhead

 Examples
 Windows
 Linux

1501-352 Operating Systems 1.22


Multithreading Models: 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
 Adv: Programmer can place
threads which might issue a
blocking system call with one-to-
one mapping.
 Disad: the number of kernel
threads, if not decided cautiously
can slow down the system
(because of the overhead in
kernel space)

 Windows with the ThreadFiber


1501-352 Operating Systems 1.23
Thread Creation Strategies
 General strategies for creating multiple threads: asynchronous
threading and synchronous threading.
 With asynchronous threading, once the parent creates a child
thread, the parent resumes its execution, so that the parent
and the child execute concurrently.
 Each thread runs independently of every other thread, and the
parent thread need not know when its child terminates.
 little data sharing among threads due to lack of
coordination between threads.
 Used in multithreaded servers and for designing responsive
user interfaces.
 Synchronous threading, 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.
 more data sharing among threads.
 For example, the parent thread may combine the results
calculated by its various children.

1501-352 Operating Systems 1.24


fork-join strategy

1501-352 Operating Systems 1.25


Threading Issues
Here are some issues to be considered when designing
multithreaded programs.

 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

1501-352 Operating Systems 1.26


Semantics of fork() and exec()
 If one thread in a program calls fork(), does the new process
duplicate all threads, or is the new process single-threaded?
 Some UNIXes have two versions of fork
 (a) duplicate all threads and
 (b) duplicate only the thread that invoked the fork().

 exec() usually works as normal – replace the running process


(with new one) including all threads
 If, however, the separate process does not call exec() after
forking, the separate process should duplicate all threads.

1501-352 Operating Systems 1.27


Signal Handling
 Signals are used in UNIX systems to notify a process that a
particular event has occurred.
 A signal may be received either synchronously or
asynchronously, depending on the source of and the reason for
the event being signalled.

 All signals, whether synchronous or asynchronous, follow the


same pattern:
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 signal handler [in kernel]
2. User-defined signal handler [user override the default
handler]

1501-352 Operating Systems 1.28


Signal Handling
 Handling signals in single-threaded programs is straightforward:
signals are always delivered to a process.

 But where should a signal be delivered for multi-threaded


process?
 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

 See example on the next slide.

1501-352 Operating Systems 1.29


Signal Handling
 Synchronous signals (internally generated signals) are delivered
to the specific thread in the process that caused the signal
 Ex: illegal memory access or division by zero
 Asynchronous signals (externally generated signals) are
delivered to all threads in the process
 Ex: “Control + C” signal terminating a process

 Signals are handled in different ways.


 Some signals (such as changing the size of a window
[asynchronous]) are simply ignored;
 others (such as an illegal memory access [synchronous]) are
handled by terminating the program.

1501-352 Operating Systems 1.30


Signal Handling
 POSIX Pthreads provides the following function,

 The pthread_kill function allows a signal to be delivered to


a specified thread with tid

1501-352 Operating Systems 1.31


Thread Cancellation
 Terminating a thread before it has finished
 EX., if multiple threads are concurrently searching through a
database and one thread returns the result, the remaining
threads might be cancelled.
 When a user presses the stop button on the browser, all
threads loading the page are cancelled.
 Thread to be cancelled is called target thread
 Two general approaches:
 Asynchronous cancellation terminates the target thread
immediately [problem: allocated resources may not be fully
de-allocated]
 Deferred cancellation allows the target thread to
periodically check if it should be cancelled (using a flag)
[also called safe cancellation]

1501-352 Operating Systems 1.32


Thread Cancellation
 Pthread code to create and cancel a thread:

1501-352 Operating Systems 1.33


Thread-Local Storage
 Thread-local storage (TLS) is a computer programming method
that uses static or global memory local to a thread.
 Threads belonging to a process share the data of the process,
however TLS allows each thread to have its own copy of data

 Different from local variables


 Local variables visible only during single function invocation
 TLS visible across all function invocations (inside the same
thread)

 Similar to static data


 However, static variables are shared by all the threads in a
process and the TLS is unique to each thread.
Program
{ int Global;
T1 (TLS fun1(){int local;} fun2(){} )

T2 ( funx(){} funy() {} )
1501-352 Operating Systems 1.34
}
Thank You!

You might also like