OS Lecture 05 Synchronization
OS Lecture 05 Synchronization
CSE-331
Prepared by-
Mst. Rashida Akhtar
Asst. Professor,
Dept of CSE, VU
Process Synchronization
Process
• Synchronization Hardware
• Peterson’s Solution (Mutex Locks)
• Semaphore
• Monitor
Synchronization Hardware
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Limitations of Semaphores
• Priority Inversion is a big limitation os
semaphores
• With improper use, a process may block
indefinitely. Such a situation is called
Deadlock.
• Semaphores permit more than one thread to
access the critical section
When to use Binary Semaphore?
A Monitor is a module
• that encapsulates shared data structure
• Procedures that operate on shared data
• Synchronization between concurrent access
Monitor
A PROCEDURE
P1
B
PROCEDURE
C block
P2
Shared Data PROCEDURE
A Monitor is a module
• that encapsulates shared data structure
• Procedures that operate on shared data
• Synchronization between concurrent access
Monitor
Single Shared Resource
Monitor
class Account {
private lock myLock public method deposit(int
private int balance := 0
amount)
invariant balance >= 0 precondition amount >= 0
public method boolean withdraw(int {
amount)
precondition amount >= 0 myLock.acquire()
{ try {
myLock.acquire()
try { balance := balance + amount
if balance < amount {
return false
} finally {
} else { myLock.release()
balance := balance - amount
return true }
}
} finally {
}
myLock.release() }
}
}