cs586 distributed computing tutorial 4
play

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.


  1. CS586: Distributed Computing Tutorial 4 Professor: Panagiota Fatourou TA: Eleftherios Kosmas CSD – November 2011

  2. Anderson’s Algorithm: The Array-Based Lock Initially Process pi Tai Tail = 0 = 0 int slot = -1; int slot = -1; 1. 1. fl flag[n] = { ag[n] = {TRUE, F RUE, FALSE, F SE, FALSE, …, F LSE, …, FALSE} SE} void lock(void) { void lock(void) { 2. 2. slot = Fetch&Inc slot = Fetch Inc(&Tail &Tail); 3. 3. while (!Flag[slot % n]) noop; while (!Flag[slot % n]) noop; 4. 4. Anderson’s Algorithm is still � } 5. 5. correct if we exchange lines 7 and 8? void unlock(void) { void unlock(void) { 6. 6. flag[(slot + 1) % n] = TRUE; flag[(slot + 1) % n] = TRUE; 7. 7. flag[slot % n] = FALSE; flag[slot % n] = FALSE; 8. 8. } 9. 9. Tail flag array: FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE 1 n-1 0 CS586 Tutorial 4 by Eleftherios Kosmas 2

  3. CLH Lock Process pi Initially void lock(void) { void lock(void) { Shared Memo Shared M mory ry 1. 1. MyNode.locked = TRUE; MyNode.locked = TRUE; Tail : points to a NODE n with n.locked == FALSE 2. 2. MyPred = Get&Set My Pred = Get&Set(&T &Tail, MyNode); ail, MyNode); 3. 3. while (MyPred->locked == TRUE) noop; while (MyPred- >locked == TRUE) noop; Global L Global Local Memory l Memory 4. 4. } MyNode : points to a struct NODE 5. 5. MyPred = NULL void unlock(void) { void unlock(void) { 6. 6. MyNode->locked = F MyNode- locked = FALSE; LSE; 7. 7. typedef struct { } 8. 8. boolean locked; } NODE NODE; CLH lock is still correct if a thread reuses its own node, instead of its � predecessor node? CS586 Tutorial 4 by Eleftherios Kosmas 3

  4. TTAS Lock Process pi Initially void lock(MemoryB void lock(MemoryByte *pB) { yte *pB) { *pB *pB = FALS = FALSE 1. 1. while (TRUE) { while (TRUE) { 2. 2. while (*pB == TRUE) noop; while (*pB == TRUE) noop; 3. 3. if (Test&Set(pB) == F if (Test&Set(pB) == FALSE) LSE) 4. 4. return; ret rn; 5. 5. } 6. 6. } 7. 7. void unlock(MemoryByte *pB) { void unlock(MemoryByte *pB { 8. 8. reset reset(pB); pB); 9. 9. } 10. 10. Design an isLocked() method that tests whether a thread is holding a lock � but does not acquire the lock � CS586 Tutorial 4 by Eleftherios Kosmas 4

  5. CLH Lock Process pi Initially void lock(void) { void lock(void) { Shared M Shared Memo mory ry 1. 1. MyNode.locked = TRUE; MyNode.locked = TRUE; Tail : points to a NODE n with n.locked == FALSE 2. 2. MyPred = Get&Set My Pred = Get&Set(&T &Tail, MyNode); ail, MyNode); 3. 3. while (MyPred- while (MyPred->locked == TRUE) noop; >locked == TRUE) noop; Global Local Memory Global L l Memory 4. 4. } MyNode : points to a struct NODE 5. 5. MyPred = NULL void unlock(void) { void unlock(void) { 6. 6. MyNode->locked = F MyNode- locked = FALSE; LSE; 7. 7. typedef struct { MyNode = MyPred; MyNode = MyPred; 8. 8. boolean locked; } 9. 9. } NODE NODE; Design an isLocked() method that tests whether a thread is holding a lock � but does not acquire the lock � CS586 Tutorial 4 by Eleftherios Kosmas 5

  6. MCS Lock Process pi Initially void lock { void lock { Shared M Shared Memo mory ry 1. 1. MyPred = Get&Set(&Tail, MyNode); MyPred = Get&Set(&Tail, MyNode) 2. 2. Tail = NULL if (MyPred != NULL) { if (MyPred != NULL) { 3. 3. MyNode->locked = TRUE; MyNode ->locked = TRUE; 4. 4. Global L Global Local Memory l Memory MyPred->next = MyNode; MyPred->n xt = MyNode; 5. 5. MyNode : points to a struct NODE while (MyNode->locked) noop; while (MyNode->locked) noop; 6. 6. MyPred = NULL } 7. 7. } 8. 8. typedef struct { void unlock { void unlock { 9. 9. if (My if (MyNode->n ode->next == NULL) { xt == NULL) { boolean locked; 10. 10. if (Compare&Swap(&Tai if (Compare&Swa p(&Tail, My l, MyNode, NULL) == TRUE) return; Node, NULL) == TRUE) return; 11. 11. struct NODE *next; while (MyNode->n while (MyNode->next == NULL) noop; xt == NULL) noop; 12. 12. } NODE NODE; } 13. 13. MyNode MyNode->n ->next->locked = FALSE; xt->locked = FALSE; 14. 14. MyNode MyNode->n ->next = NULL; xt = NULL; 15. 15. } 16. 16. Design an isLocked() method that tests whether a thread is holding a lock � but does not acquire the lock � CS586 Tutorial 4 by Eleftherios Kosmas 6

  7. Mutual Exclusion - Exercise 9 Prove that the order of lines 11 and 12 in the Black-White Bakery algorithm is crucial for � correctness. Process pi choosing[i] = TRUE; choosing[i ] = TRUE; 1. 1. mycolor[i] = col mycol r[i] = color; r; 2. 2. 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]); ); 3. 3. choosing[i choosing[i] = FALSE ] = FALSE; 4. 4. for (j = 1; j <= NUM_THREADS; j++) do for (j = 1; j <= NUM _THREADS; j++) do 5. 5. Initially wait unt wa it until ( l (choosing[j hoosing[j] == F == FALSE) 6. 6. number[ number[i] = 0 ] = 0 if (myc if (mycolor[i] olor[i] == myc == mycolor[j lor[j]) then ) then 7. 7. choosing[i] = 0 choosing[i ] = 0 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 8. 8. mycolor[j] mycolor[j] ≠ myc mycolor[ lor[i] i] ); ); else await ( number[j] else await ( number[j] == 0 OR mycolor[i] == 0 OR mycolor[i] ≠ co colo lor O r OR 9. 9. mycolor[j] == mycolor[i] ) mycolor[j] == mycolor[i] ) critical s critical section; ction; 10. 10. number[i] = 0; number[i] = 0; 11. 11. if if (myc (mycolor[i] olor[i] == bl == black) ack) then col then color = white; r = white; 12. 12. else color = bl else color = black; ack; remainder section; CS586 Tutorial 4 by Eleftherios Kosmas 7

  8. The End - Questions CS586 Tutorial 4 by Eleftherios Kosmas 8

Recommend


More recommend