Implementing a Lock - High Performance Computing - Lecture Slides, Slides of Computer Science

Some concept of High Performance Computing are Addressing Modes, Program Execution, Basic Computer Organization, Control Hazard Solutions, Least Recently Used, Memory Hierarchy Progression. Main points of this lecture are: Implementing a Lock, Acquirelock, Releaselock, Fails, Processes, Available, Lock Implementation, Set Instruction, Atomically, Indivisibly

Typology: Slides

2012/2013

Uploaded on 04/28/2013

dewaan
dewaan 🇮🇳

3.8

(4)

43 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
High Performance Computing
Lecture 20
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Implementing a Lock - High Performance Computing - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

High Performance Computing

Lecture 20

2

Implementing a Lock

int L=0; /* 0: lock available */

AcquireLock(L):

while (L==1);

L = 1;

ReleaseLock(L):

L = 0;

/* `BUSY WAITING’ */

4

Why this implementation fails

wait: LW R1, Addr(L) BNEZ R1, wait ADDI R1, R0, 1 SW R1, Addr(L) Process 1 Process 2 LW R1 = 0 LW R1 = 0 BNEZ ADDI SW Enter CS BNEZ ADDI SW Enter CS time Assume that lock L is currently available (L = 0) and that 2 processes, P1 and P2 try to acquire the lock L CSwitch CSwitch IMPLEMENTATION ALLOWS PROCESSES P1 and P2 TO BE IN CRITICAL SECTION TOGETHER!

5

Why this implementation fails

while ( L == 1) ; L = 1; wait: LW R1, Addr(L) BNEZ R1, wait ADDI R1, R0, 1 SW R1, Addr(L)

7

Busy Wait Lock with Test&Set

Lock variable declared as int L

L == 0 means that the lock is available

L == 1 means that the lock is in use

AcquireLock(L)

while (Test&Set(L)) /* busy wait */ ;

/ Busy wait until L has been Test&Set from 0 to 1

/ i.e., the return value from Test&Set is 0

ReleaseLock(L)

L = 0;

8

Busy Wait Lock with Test&Set

AcquireLock(L): while (Test&Set(L)) ;

ReleaseLock(L): L = 0;

P1 P2 P

while(Test&Set(L)); while(Test&Set(L)); while(Test&Set(L)); Critical Section Critical Section Critical Section L=0; L=0; L = 0; Suppose that process P1 is in its Critical Section. Processes P2 and P3 are trying to Acquire the Lock in order to enter their Critical Sections

10

Busy Wait Lock with Test&Set

AcquireLock(L): while (Test&Set(L)) ;

ReleaseLock(L): L = 0;

P1 P2 P

while(Test&Set(L)); while(Test&Set(L)); while(Test&Set(L)); Critical Section Critical Section Critical Section L=0; L=0; L = 0;

11

More on Locks

 Other names for this kind of lock

 Mutex

 Spin wait lock

 Spinlock

 Busy wait lock

 There are also locks where instead of busy

waiting, an unsuccessful process gets

blocked by the operating system

 i.e., moved into the Waiting state until the lock

becomes available

13

Critical Section Problem & Semaphore

 Initialize a Semaphore S = 1

 Surround each critical section in the

concurrent program by calls to P(S) and V(S)

14

Critical Section Problem & Semaphore

 Initialize a Semaphore S = 1

 Surround each critical section in the

concurrent program by calls to P(S) and V(S)

Process P

P(S);

Critical Section code

V(S);

if (S != 0) S--; return / S== else block process until S!= S--; return / S== S++ / S== Unblock a process blocked on S

16

Semaphore Examples

 Semaphores can do more than mutex locks

 Example: Initialize semaphore S =

 Suppose that processes surround code by

P(S), V(S) as with the previous example

P(S);

code

V(S);

if (S != 0) S--; return else block process until S!= S--; return S++ Unblock a process blocked on S

17

Semaphore Examples

 Semaphores can do more than mutex locks

 Example: Initialize semaphore S =

 10 processes will be allowed to proceed  Processes beyond that will be blocked until one of the first 10 executes V(S)