Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threading And Parallel Programming Constructs

Similar presentations


Presentation on theme: "Threading And Parallel Programming Constructs"— Presentation transcript:

1 Threading And Parallel Programming Constructs
Module III

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

3 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

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

5

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

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

8 Synchronization Primitives
Semaphore Lock Condition Variable Usage of fence (also called barrier)

9 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”)

10 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

11

12 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

13 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

14 Mutual exclusion of parallel threads using P and V

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

16 Case Study using PC Problem= bounded buffer

17 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

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

19 Lock Types Mutex Recursive Locks Read-Write Locks Spin Locks

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

21 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

22 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

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

24 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

25 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

26

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

28 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

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

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

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

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

33 Flow control based concepts
Fence & barrier

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


Download ppt "Threading And Parallel Programming Constructs"

Similar presentations


Ads by Google