CST334 - Week 6
Week 6 Reflection
This week, we learned more about semaphores, and the bugs that are commonly faced in concurrency, such as deadlocks.
Semaphores are a synchronization primitive that has a modifiable integer value. The behavior of the semaphore is determined by this value. They can be used as both locks and condition variables for concurrency.
sem_wait() and sem_post() are used to interact with a semaphore. sem_wait() waits for a condition to be met, while sem_post() increments the value of the semaphore, then wakes a waiting thread. If a semaphore's value is negative, this number is equal to the number of waiting threads. For example, -4 equals four waiting threads, incrementing by 1 will change it to -3, waking one thread and now resulting in three waiting threads.
Concurrency creates bugs that are difficult or costly to fix correctly. Non-deadlock bugs include atomicity violations and order violations. Atomicity violations occur when instructions that should be executed all at once were not. Order violations occur when instructions are not executed in the correct order, leading to bugs (like a pointer not being initialized).
Deadlock bugs are created when four conditions are fulfilled:
- when a thread is holding a lock (mutual exclusion), and
- when one or more threads are holding resources, but are waiting for more resources that other threads may be using (hold-and-wait), and
- resources cannot be removed once a thread obtains it (no preemption), and
- a loop of threads exist where threads require resources that the next-in-line thread also need (circular wait)
Preventative measures are less commonly used - deadlock avoidance is preferred.
Comments
Post a Comment