10 - Producer/Consumer Problem, Condition Variables, and Semaphores Flashcards
What is the produce/consumer problem?
• simplest case: one consumer and one producer
processes/threads
• the two processes or threads share a fixed-size buffer, used as a
queue
• producer puts data into buffer, must wait if buffer full
• consumer takes data out of the buffer, must wait if buffer empty
What is Condition Variable?
A type of synchronization primitive
Used together with mutexes
Prevents deadlock by allowing another thread into a critical section that is required to change some state that allows the first thread to continue
What happens if no thread is waiting on the condition variable?
The signal is lost
What is a Semaphore?
A special integer value for signalling between processes.
The value could indicate the number of available units of some resource.
What is a major difference between a semaphore and a mutex?
When a semaphore is locked by a thread, it can be unlocked by any thread as opposed to mutex, where a locking/unlocking must be done by the same thread
What is an atomic operation?
An operation that appears to execute instantaneously
with respect to the rest of the system
eg. cannot be interrupted by signals, threads, interrupts, …
Compare semaphores to condition variables
CV signal is lost if no thread is waiting
cv_wait() always blocks, sem_wait() may or may not block.
Can semaphores be shared between processes?
YES - set this in sem_init
Race conditions are not a problem among processes, only among threads.
True or False?
False
What is the main difference between
pthread_cond_signal() for mutex and sem_post() for
semaphore?
Signals can be lost
A mutex is identical to binary semaphore.
True or False?
False - Mutex is under control of the person who entered it.
Even with a binary semaphore, another thread can increment or decrement (lock/unlock)
What does the value of the semaphore tell you?
Number tells you how many people can get past it (Not necessarily related to resource counting).