CS586: Distributed Computing Tutorial 4 Professor: Panagiota - - PowerPoint PPT Presentation

cs586 distributed computing tutorial 4
SMART_READER_LITE
LIVE PREVIEW

CS586: Distributed Computing Tutorial 4 Professor: Panagiota - - PowerPoint PPT Presentation

CS586: Distributed Computing Tutorial 4 Professor: Panagiota Fatourou TA: Eleftherios Kosmas CSD November 2011 Andersons Algorithm: The Array-Based Lock Initially Process pi Tai Tail = 0 = 0 int slot = -1; int slot = -1; 1. 1.


slide-1
SLIDE 1

CS586: Distributed Computing Tutorial 4

Professor: Panagiota Fatourou TA: Eleftherios Kosmas CSD – November 2011

slide-2
SLIDE 2

Anderson’s Algorithm: The Array-Based Lock

Initially Tai Tail = 0 = 0 fl flag[n] = { ag[n] = {TRUE, F RUE, FALSE, F SE, FALSE, …, F LSE, …, FALSE} SE}

FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE

1 n-1 flag array: Tail

CS586 Tutorial 4 by Eleftherios Kosmas 2

  • Anderson’s Algorithm is still

correct if we exchange lines 7 and 8?

Process pi

1. 1.

int slot int slot = -1; = -1;

2. 2.

void lock(void) { void lock(void) {

3. 3.

slot = Fetch slot = Fetch&Inc Inc(&Tail &Tail);

4. 4.

while (!Flag[slot % n]) noop; while (!Flag[slot % n]) noop;

5. 5.

}

6. 6.

void unlock(void) { void unlock(void) {

7. 7.

flag[(slot + 1) % n] = TRUE; flag[(slot + 1) % n] = TRUE;

8. 8.

flag[slot % n] = FALSE; flag[slot % n] = FALSE;

9. 9.

}

slide-3
SLIDE 3

CLH Lock

Initially Shared M Shared Memo mory ry Tail : points to a NODE n with n.locked == FALSE Global L Global Local Memory l Memory MyNode : points to a struct NODE MyPred = NULL

CS586 Tutorial 4 by Eleftherios Kosmas 3

  • CLH lock is still correct if a thread reuses its own node, instead of its

predecessor node?

Process pi

1. 1.

void lock(void) { void lock(void) {

2. 2.

MyNode.locked = TRUE; MyNode.locked = TRUE;

3. 3.

My MyPred = Get&Set Pred = Get&Set(&T &Tail, MyNode); ail, MyNode);

4. 4.

while (MyPred- while (MyPred->locked == TRUE) noop; >locked == TRUE) noop;

5. 5.

}

6. 6.

void unlock(void) { void unlock(void) {

7. 7.

MyNode- MyNode->locked = F locked = FALSE; LSE;

8. 8.

} typedef struct { boolean locked; } NODE NODE;

slide-4
SLIDE 4

TTAS Lock

Initially *pB *pB = FALS = FALSE

CS586 Tutorial 4 by Eleftherios Kosmas 4

  • Design an isLocked() method that tests whether a thread is holding a lock
  • but does not acquire the lock

Process pi

1. 1.

void lock(MemoryB void lock(MemoryByte *pB) { yte *pB) {

2. 2.

while (TRUE) { while (TRUE) {

3. 3.

while (*pB == TRUE) noop; while (*pB == TRUE) noop;

4. 4.

if (Test&Set(pB) == F if (Test&Set(pB) == FALSE) LSE)

5. 5.

ret return; rn;

6. 6.

}

7. 7.

}

8. 8.

void unlock(MemoryByte *pB void unlock(MemoryByte *pB) { {

9. 9.

reset reset(pB); pB);

10. 10.

}

slide-5
SLIDE 5

CLH Lock

Initially Shared M Shared Memo mory ry Tail : points to a NODE n with n.locked == FALSE Global L Global Local Memory l Memory MyNode : points to a struct NODE MyPred = NULL

CS586 Tutorial 4 by Eleftherios Kosmas 5

  • Design an isLocked() method that tests whether a thread is holding a lock
  • but does not acquire the lock

Process pi

1. 1.

void lock(void) { void lock(void) {

2. 2.

MyNode.locked = TRUE; MyNode.locked = TRUE;

3. 3.

My MyPred = Get&Set Pred = Get&Set(&T &Tail, MyNode); ail, MyNode);

4. 4.

while (MyPred- while (MyPred->locked == TRUE) noop; >locked == TRUE) noop;

5. 5.

}

6. 6.

void unlock(void) { void unlock(void) {

7. 7.

MyNode- MyNode->locked = F locked = FALSE; LSE;

8. 8.

MyNode = MyPred; MyNode = MyPred;

9. 9.

} typedef struct { boolean locked; } NODE NODE;

slide-6
SLIDE 6

MCS Lock

Initially Shared M Shared Memo mory ry Tail = NULL Global L Global Local Memory l Memory MyNode : points to a struct NODE MyPred = NULL

CS586 Tutorial 4 by Eleftherios Kosmas 6

  • Design an isLocked() method that tests whether a thread is holding a lock
  • but does not acquire the lock

Process pi

1. 1.

void lock { void lock {

2. 2.

MyPred = Get&Set(&Tail, MyNode) MyPred = Get&Set(&Tail, MyNode);

3. 3.

if (MyPred != NULL) { if (MyPred != NULL) {

4. 4.

MyNode MyNode->locked = TRUE;

  • >locked = TRUE;

5. 5.

MyPred->n MyPred->next = MyNode; xt = MyNode;

6. 6.

while (MyNode->locked) noop; while (MyNode->locked) noop;

7. 7.

}

8. 8.

}

9. 9.

void unlock { void unlock {

10. 10.

if (My if (MyNode->n

  • de->next == NULL) {

xt == NULL) {

11. 11.

if (Compare&Swa if (Compare&Swap(&Tai p(&Tail, My l, MyNode, NULL) == TRUE) return; Node, NULL) == TRUE) return;

12. 12.

while (MyNode->n while (MyNode->next == NULL) noop; xt == NULL) noop;

13. 13.

}

14. 14.

MyNode MyNode->n

  • >next->locked = FALSE;

xt->locked = FALSE;

15. 15.

MyNode MyNode->n

  • >next = NULL;

xt = NULL;

16. 16.

}

typedef struct { boolean locked; struct NODE *next; } NODE NODE;

slide-7
SLIDE 7

Mutual Exclusion - Exercise 9

  • Prove that the order of lines 11 and 12 in the Black-White Bakery algorithm is crucial for

correctness.

Initially number[ number[i] = 0 ] = 0 choosing[i choosing[i] = 0 ] = 0 Process pi

1. 1.

choosing[i choosing[i] = TRUE; ] = TRUE;

2. 2.

mycol mycolor[i] = col r[i] = color; r;

3. 3.

number[i] = 1 + max (number[j] | 1 number[i] = 1 + max (number[j] | 1≤j≤n AND mycol n AND mycolor[ r[i] == mycol i] == mycolor[j] r[j]); );

4. 4.

choosing[i choosing[i] = FALSE ] = FALSE;

5. 5.

for (j = 1; j <= NUM for (j = 1; j <= NUM_THREADS; j++) do _THREADS; j++) do

6. 6.

wa wait unt it until ( l (choosing[j hoosing[j] == F == FALSE)

7. 7.

if if (myc (mycolor[i]

  • lor[i] == myc

== mycolor[j lor[j]) then ) then

8. 8.

await ( n await ( number[j] == 0 OR (num mber[j] == 0 OR (number[j], j ber[j], j) > (number[i] > (number[i], i) O , i) OR mycolor[j] mycolor[j] ≠ myc mycolor[ lor[i] i] ); );

9. 9.

else await ( number[j] else await ( number[j] == 0 OR mycolor[i] == 0 OR mycolor[i] ≠ co colo lor O r OR mycolor[j] == mycolor[i] ) mycolor[j] == mycolor[i] )

10. 10.

critical s critical section; ction;

11. 11.

number[i] = 0; number[i] = 0;

12. 12.

if if (myc (mycolor[i]

  • lor[i] == bl

== black) ack) then col then color = white; r = white; else color = bl else color = black; ack; remainder section;

CS586 Tutorial 4 by Eleftherios Kosmas 7

slide-8
SLIDE 8

The End - Questions

CS586 Tutorial 4 by Eleftherios Kosmas 8