Only one process in CS at a time. Otherwise, we get race condition
-- --
Progress
--------
If no process is currently executing in their critical section, and one or more processes want to execute their critical section, then only the processes not in their remainder sections can participate in the decision, and the decision cannot be postponed indefinitely. I.e. processes cannot be blocked forever waiting to get into their critical sections.
-- --
Bounded Waiting
---------------
There exists a limit as to how many other processes can get into their critical sections after a process requests entry into their critical section and before that request is granted. ( I.e. a process requesting entry into their critical section will get a turn eventually, and there is a limit as to how many other processes get to go first. )
On single processors this can be implemented by disabling interrupts during the execution of wait and signal; Multiprocessor systems have to use more complex methods, including the use of spinlocking.
1. Prevention: Deadlocks can be prevented by preventing at least one of the four required conditions:
- Mutual Exclusion
- Shared resources such as read-only files do not lead to deadlocks.
- Unfortunately some resources, such as printers and tape drives, require exclusive access by a single process.
- Hold and Wait
- Require that all processes request all resources at one time. This can be wasteful of system resources
- Require that processes holding resources must release them before requesting new resources, and then re-acquire the released resources along with the new ones in a single new request.
- above can lead to starvation if a process requires one or more popular resources.
- No preemption
- difficult to save resource state
- Circular Wait
- One way to avoid circular wait is to number all resources, and to require that processes request resources only in strictly increasing ( or decreasing ) order.
- In other words, in order to request resource Rj, a process must first release all Ri such that i >= j.
- One big challenge in this scheme is determining the relative ordering of the different resources
2. Avoidance - Banker's algorithm
3. Detection
4. Recovery
- terminate
- preempt
Unix, Windows simply ignores!
-- --
Bounded Buffer Problem
----------------------
-- --
Dining Philosophers Problem
---------------------------
-- --
Reader Writer Problem
---------------------
-- --
Implementing Locks in Code
--------------------------
Java
import java.util.concurrent.*;
Semaphore(int num)
Semaphore(int num, boolean inorder)
sem.acquire()
sem.release()
[Semaphore (Java Platform SE 7 )](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html)