31. Semaphores Flashcards
What is a semaphore?
A semaphore is an object with integer value that we can manipulate with two routines: sem_wait() and sem_post() .
What does semaphore equal to when negative?
The value of the semaphore, when negative, is equal to the number of waiting threads.
How is semaphore called when it’s used as a lock? What does its value equal to?
It’s called binary semaphore and its value is equal to 1.
What value should semaphore have if it’s used for ordering?
0
What value should semaphore be initialized to when used for consumer/producer problem?
One semaphore should be initialized to 0 (to identify emptiness) and the other one should be initialized to the size of buffer.
What is other use case of semaphore?
Thread throttling. By surrounding resource-intensive region of code with semaphore, we can limit the amount of thread entering this area at the same time.