Tom Kelliher, CS 311
Mar. 21, 2012
Announcements:
From last time:
Outline:
Assignment:
Assumptions:
int turn = 0; // Shared control variable. // mutexbegin: while (turn != i) ; // Busy wait. // mutexend: turn = 1 - i;
Remove strict alternation requirement.
int flag[2] = { FALSE, FALSE } // flag[i] indicates that Pi is in its
// critical section.
// mutexbegin:
while (flag[1 - i])
;
flag[i] = TRUE;
// mutexend:
flag[i] = FALSE;
Restore mutual exclusion.
int flag[2] = { FALSE, FALSE } // flag[i] indicates that Pi wants to
// enter its critical section.
// mutexbegin:
flag[i] = TRUE;
while (flag[1 - i])
;
// mutexend:
flag[i] = FALSE;
Attempt to remove the deadlock.
int flag[2] = { FALSE, FALSE } // flag[i] indicates that Pi wants to
// enter its critical section.
// mutexbegin:
flag[i] = TRUE;
while (flag[1 - i])
{
flag[i] = FALSE;
delay; // Sleep for some time.
flag[i] = TRUE;
}
// mutexend:
flag[i] = FALSE;
int flag[2] = { FALSE, FALSE } // flag[i] indicates that Pi wants to
// enter its critical section.
int turn = 0; // turn indicates which process has
// priority in entering its critical
// section.
// mutexbegin:
flag[i] = TRUE;
turn = 1 - i;
while (flag[1 - i] && turn == 1 - i)
;
// mutexend:
flag[i] = FALSE;
Lamport's Bakery algorithm.
Assumptions:
// Global initialization:
int choosing[NPROCS] = { FALSE };
int number[NPROCS] = { 0 };
// mutexbegin:
choosing[i] = TRUE;
number[i] = max(number) + 1;
choosing[i] = FALSE;
for (j = 0; j < NPROCS; ++j)
{
while (choosing[j])
;
while (number[j] != 0 && (number[j] < number[i] ||
number[j] == number[i] && j < i) )
;
}
// mutexend:
number[i] = 0;