Sync w/o Busy Waiting Flashcards
Race Condition
If final program result depends on the execution/scheduling sequence
Critical Section
Program region that has to access shared data in a synchronized way or else race condition will occur
Mutual Exclusion
Preventing two or more processes from being inside a critical section simultaneously
lock() code
p = 0 #shared between processes
void lock( int *p ) {
while( test_and_set(&p) == 1 );
}
Semaphore
- has counter to indicate the number of available resources
- has queue holding processes waiting for the semaphore
- has a busy-waiting lock ( based on test_and_set() )
- has two functions: DOWN() and UP()
unlock() code
p = 0 #shared between processes
void unlock( int *p ) {
*p = 0 }
test_and_set() code
int test_and_set( int *p ) {
int t= *p
*p = 0
return t
down() code
semaphore S
down(S) {
if( S.num > 0 ) S.num –;
else { put current process in wait queue; }
}
up() code
semaphore S
up(S) {
if( any processes in wait queue) { pop process from wait queue; resume process; }
else { S.num ++; }
}
Binary Semaphore
Semaphore whose counter value is restricted to 0 or 1
Monitor
Programming language construct that supports controlled access to shared data. Has mutex lock and queue. Encapsulates:
- shared data
- shared data procedures
- synchronization between concurrent processes
Monitor Java Method
- uses “Synchronized” label to implement Monitor object
- inserts lock() and unlock() at entry and exit of each procedure
CV
Condition Variable. Used to show events like availability of shared variables. Three operations:
- wait(c, lock): puts process in wait queue and unlock
- signal(c): move process from CV queue to monitor queue
- broadcast(c): wake all processes from CV queue to monitor queue
CV vs Semaphore
- semaphore has counter and wait queue
- CV only has wait queue
Look at semaphore use examples