Conditions for a data race:
Locks and Data Races:
Conditions for a deadlock:
Avoiding deadlock:
A semaphore is an integer and two associated operations.
sem_wait: Attempt to decrement, but if this would bring the value below zero, block instad.
sem_post: Increment the semaphore. If there are processes blocked on the semaphore, give them a chance to try decrementing.
So if we start with a value > 0, that allows sem_wait to restrict access to a resource to that many processes. For the specific value 1, this is a lock.
If we start with a value of zero, processes waiting on the semaphore block immediately and can be woken up by a post operation.