Docsity

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

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)