Slides 10 Flashcards
What is the producer/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 a condition variable?
- condition variables are another type of synchronization primitive
- used together with mutexes
- perfect for implementing critical sections containing loops waiting for some ‘condition’
When is it a good idea to use a condition variable?
- a thread enters its critical section (by locking a mutex)
- inside CS, thread needs to wait for some condition to become true
- but the condition can only become true by allowing some other thread to lock the mutex
- to facilitate this, the thread puts itself to sleep and simultaneously releases the mutex
What are the methods of a condition variable?
• pthread_cond_wait(&cond, &mutex);
• atomically releases mutex and causes the calling thread to block, until
some other thread calls pthread_cond_signal(&cond)
• after returning, the mutex is automatically re-acquired
• after returning, the condition must be rechecked !!! (spurious wakeups)
• pthread_cond_signal(&cond); • wakes up one thread waiting on cond
• if no threads waiting on cond, the signal is lost
• must be followed by pthread_mutex_unlock() if the blocked thread
uses the same mutex
• pthread_cond_init(& cond, & attr) • creates condition variable
• pthread_cond_destroy(& cond) • destroys a condition variable
• pthread_cond_broadcast(& cond)
• wakes up all threads waiting on the condition
What is a semaphore?
another synchronization primitive
• think of semaphore as a special integer variable used for
signalling among processes
• the value could indicate number of available units of some resource
What are the 3 operations a semaphore supports?
initialization
• can be initialized with any value (0 … max)
• decrement
• reduce semaphore by 1,
• blocks the calling process if going below 0
down(s), wait(S) or sem_wait(S)
• increment
• increase value by 1;
• and possibly unblock another blocked process
up(S), signal(S) or sem_post(S)
What is a binary semaphore?
special type of semaphore with value either 0 or 1 • possible pseudocode implementation where bodies are executed atomically (is an operation that appears to execute instantaneously
with respect to the rest of the system
• eg. cannot be interrupted by signals, threads, interrupts, )
What is a counting semaphore?
is a general semaphore or semaphore
represents an int value s where
s > 0 is the value of S is the number of threads that can issue wait() without blocking
s == 0 all resources are busy, any thread that calls wait() will block
s < 0 some implementations allow this… |S| then represents the number of threads that are blocked on wait()
What are the semaphore methods?
• int sem_init (sem_t *sem, int pshared, unsigned int value) initializes semaphore to value, pshared=0/1 shared by threads/processes
• int sem_destroy (sem_t * sem)
destroys the semaphore, fails if some threads are waiting on it
• int sem_wait (sem_t * sem)
suspends the calling thread until the semaphore is non-zero, then atomically decreases the semaphore count
• int sem_post (sem_t * sem)
atomically increases the semaphore, never blocks, may unblock blocked threads, safe to use in signal handlers in Linux on 486+ hardware
• int sem_getvalue (sem_t * sem, int * sval) returns the value of semaphore via sval
• int sem_trywait (sem_t * sem) non blocking version of sem_wait()