Algorithm Concurrency - Lock Variables Flashcards
Lock
A shared data item
Processes must acquire such a lock before entering a critical section
Processes have to release a lock when exiting critical section
Synonym for the Lock Variables
Mutex
Syntax
process ()
{
acquire lock
critical_section() ;
release lock
remainder_section();
}
True vs False Lock
True - Critical Section Locked
False - Critical Section Unlocked
while(lock == TRUE) {
// wait
}
This is a loop that keeps the process waiting (doing nothing) until the lock variable becomes FALSE
this while loop continuously checks the lock condition, as long as the condition is true (meaning the shared resource is being accessed by another process) the loop doesn’t allow the process to move forward.It stays stuck in the loop effectively waiting
once the lock variable is set to false (by the other process releasing the resource) the condition lock == TRUE becomes FALSE
Issues with Lock variable
As the process continuously evaluates whether a lock has become available
Token - Shared variable
Usually process ID
Indicated which process is next to enter critical section, set by previous process
p0
while(TRUE) {
while(turn !=0) {
//wait
}
critical_section
turn= 1
non critical section
turn is a shared variable
turn=0 means P0 has the right to execute the critical section
turn=1 means P1 has the right to execute the critical section
each process alternates between the critical and non-critical section
P0 waits in the loop until turn==0 which means it is P0 to execute the critical section
once the critical section completed the process changes the turn variable to the next number to allow P1 to proceed to the critical section too