Synchronization tool that does not require busy waiting.
Semaphore S represents an integer variable that can only be accessed via two indivisible (atomic) operations.
semaphore는 다수의 thread 사이의 병행성 유지를 위해 OS 단위에서 제공되는 기법이다.
기본적인 작동 원리는 특정 thread가 특정 signal을 수신할 때까지 정해진 위치에서 waiting하도록 강제하는 것이다.
Counting semaphore – integer value can range over an unrestricted domain.
Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement than counting semaphore; also known as mutex lock.
mutex semaphore라고도 부르며 변수가 0 또는 1의 binary 값만 갖는 semaphore이다.
In general, busy waiting wastes CPU cycles that some other process might be able to use productively.
Spinlock – process spins while waiting for the lock.
Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes.
Let S and Q be two semaphores initialized to 1.
Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.
High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes (e.g., synchronized in Java).
To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y;
Condition variable can only be used with the operations wait and signal (e.g., wait and notify in Java).
Suppose that when the x.signal() operation is invoked by a process P, there is a suspended process Q associated with condition x (already in the monitor).
Possible solutions
This solution guarantees that no two neighbors are eating simultaneously.
However, it has the possibility of a deadlock.
Possible remedies to the deadlock problem.
A deadlock-free solution does not necessarily eliminates the possibility of starvation.