Threading And Parallel Programming Constructs MODULE III Introduction

  • Slides: 34
Download presentation
Threading And Parallel Programming Constructs MODULE III

Threading And Parallel Programming Constructs MODULE III

Introduction �Basic parallel programming constructs �Topics : Synchronization & primitives used Critical Section Deadlock

Introduction �Basic parallel programming constructs �Topics : Synchronization & primitives used Critical Section Deadlock Use of messages Concepts based on flow control

Synchronization �Mechanism to impose constraints on the order of execution �Resolves conflict between threads

Synchronization �Mechanism to impose constraints on the order of execution �Resolves conflict between threads �Coordinate thread execution and manage shared data �For example : a message must be sent before it is received �Semaphore, Monitor, Mutex, Locks, Critical Sections

2 types of synchronization Mutual Exclusion : 1. 1. 2. One thread blocks a

2 types of synchronization Mutual Exclusion : 1. 1. 2. One thread blocks a critical section – and one or more threads wait for their turns Controlled by scheduler 2. Condition Synchronization : 1. A thread is blocked until the system state achieves some particular condition

Synchronization function �Proper synchronizations ensures proper order of execution and proper result

Synchronization function �Proper synchronizations ensures proper order of execution and proper result

How Synchronization is performed in an actual multi-threaded implementation Operational Flow of Threads for

How Synchronization is performed in an actual multi-threaded implementation Operational Flow of Threads for an application

Synchronization Primitives Semaphore 2. Lock 3. Condition Variable 1. � Usage of fence (also

Synchronization Primitives Semaphore 2. Lock 3. Condition Variable 1. � Usage of fence (also called barrier)

1. Semaphores �Software oriented primitive �Introduced by Dijkstra �Semaphore is a single integer, sem,

1. Semaphores �Software oriented primitive �Introduced by Dijkstra �Semaphore is a single integer, sem, that apart from initialisation is accessed only through 2 operations Wait() : originally termed P (proberen, means “to test”) Signal() : originally termed V (verhogen, “to increment”)

�P denotes potential delay or wait �V denotes release of thread �Semaphore value sem

�P denotes potential delay or wait �V denotes release of thread �Semaphore value sem must have exclusive access from the threads �P operation blocks a thread. V allows the thread to continue execution

2 types of semaphores based on values it can take �Binary : Also called

2 types of semaphores based on values it can take �Binary : Also called mutex locks Values restricted between 0 and 1 �Counting : Used to control access to a given resource Semaphore indicates number of resource. When a thread wants to use a resource, wait() : decrement When a thread releases reource, signal() : increment

2 types of semaphores from user perspective �Strong All threads that call for P

2 types of semaphores from user perspective �Strong All threads that call for P are satisfied �Weak No guarantee of service to threads. Threads might starve

Mutual exclusion of parallel threads using P and V

Mutual exclusion of parallel threads using P and V

Disadvantage of semaphores �Busy waiting Any other process wanting to access the critical section

Disadvantage of semaphores �Busy waiting Any other process wanting to access the critical section should execute entry code continuously

Case Study using PC Problem= bounded buffer

Case Study using PC Problem= bounded buffer

2. Locks �Acquire() : Waits for lock state to be unlocked and sets the

2. Locks �Acquire() : Waits for lock state to be unlocked and sets the lock state to lock. �Release() : Automatically changes the lock state from locked to unlocked. �Only one thread can hold a lock at a time. �Acquire – operation on shared data – release

�Locks must not be held for long periods of time �Explicit locks and implicit

�Locks must not be held for long periods of time �Explicit locks and implicit locks : safe and reliable to use explicit locks

Lock Types Mutex 2. Recursive Locks 3. Read-Write Locks 4. Spin Locks 1.

Lock Types Mutex 2. Recursive Locks 3. Read-Write Locks 4. Spin Locks 1.

Mutex �Simplest lock �Acquire and release �Release : Does not depend on release() only

Mutex �Simplest lock �Acquire and release �Release : Does not depend on release() only Timer attribute Exception

Recursive Locks �Locks being acquired by the thread holding the lock itself �No deadlock

Recursive Locks �Locks being acquired by the thread holding the lock itself �No deadlock �Each thread has to release it for each time it acquired the lock �Slower performance

Read Write Locks �Also called multiple read-single write locks �Allows multiple threads read access

Read Write Locks �Also called multiple read-single write locks �Allows multiple threads read access to a shared resource, but limits write access to a single thread

Spin Locks �Waiting threads must “spin”, not get blocked. �Poll the state of critical

Spin Locks �Waiting threads must “spin”, not get blocked. �Poll the state of critical section lock

3. Condition Variables (c) �no stored value to test �Using a lock L, 3

3. Condition Variables (c) �no stored value to test �Using a lock L, 3 basic atomic operations are performed on C Wait(L) : releases lock and waits Signal(L) : enables one of the waiting threads to run Broadcast(L) : enables all of the waiting threads to run �An assosciation between condition variable C and lock L

Critical Section �A portion of code where shared dependency variables reside. �No two processes

Critical Section �A portion of code where shared dependency variables reside. �No two processes should execute in their critical sections at the same time. �Entry section, exit section, remainder section

�Also called Synchronization blocks �Minimize size of synchronization block �Larger CS are split to

�Also called Synchronization blocks �Minimize size of synchronization block �Larger CS are split to multiple code blocks

Deadlocks �Occur when a thread is waiting for a resource of another thread that

Deadlocks �Occur when a thread is waiting for a resource of another thread that will never become available. �Example : Cricket �Types of deadlock : Self deadlock Recursive deadlock Lock ordering deadlock

1. Self deadlock �A thread requests for a lock already held by itself.

1. Self deadlock �A thread requests for a lock already held by itself.

�Recursive deadlock Wake up path of one thread lies on another thread �Lock ordering

�Recursive deadlock Wake up path of one thread lies on another thread �Lock ordering

Deadlock transition diagram �Threads can transition from one state to another by requesting, acquiring

Deadlock transition diagram �Threads can transition from one state to another by requesting, acquiring or freeing a resource

Messages �Method of communication to transfer information �Intra process, inter process, process-process �Shared memory

Messages �Method of communication to transfer information �Intra process, inter process, process-process �Shared memory vs msg passing model �MPI : message passing interface

Flow control based concepts �Fence & barrier

Flow control based concepts �Fence & barrier

Implementation dependant threading features �Syntax of threadsis different b/w environments �Critical section �synchronisation

Implementation dependant threading features �Syntax of threadsis different b/w environments �Critical section �synchronisation