ESD (SEMAPHORES)
SEMAPHORES:
semaphores belongs to synchronization primitives the other types include: mutex locks, conditional variables, monitors barriers
semaphores helps coordinate between the multiple concurrently running threads or processes. concurrently means that multiple threads are running together to solve a particular issue so they have to share information, memory, data between them.
semaphore is basically a unsigned integer with some quirks one of the quirk is changes to the value of variable is atomic that is if one thread or process increments the value and other wants to decrement the value the increment and decrement operation cannot interrupt each other.
We can interact with semaphores with two ways:
- wait()
- post() or signal()
wait() try to decrement the value of semaphores if the value is greater than zero(0).if the value is zero it waits that's why its name is wait and it wait until the value becomes greater than zero.
post() try to increment the value of semaphore and returns it.
WHY SEMAPHORES USEFUL?
semaphores are useful to protect some critical shared resources: HOW DO THEY PROTECT suppose when a thread wants to a access some critical section of code it first call wait() which is grabbing a lock and when the thread is finished with the shared resources then it call post() which is like releasing the lock, when another want to access the code in between my wait() and post() it should wait.
Comments
Post a Comment