Slides 10 Flashcards

1
Q

What is the producer/consumer problem?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a condition variable?

A
  • condition variables are another type of synchronization primitive
  • used together with mutexes
  • perfect for implementing critical sections containing loops waiting for some ‘condition’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When is it a good idea to use a condition variable?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the methods of a condition variable?

A

• 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a semaphore?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 3 operations a semaphore supports?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a binary semaphore?

A

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, )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a counting semaphore?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the semaphore methods?

A

• 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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly