10 - Producer/Consumer Problem, Condition Variables, and Semaphores Flashcards

1
Q

What is the produce/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 Condition Variable?

A

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

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

What happens if no thread is waiting on the condition variable?

A

The signal is lost

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

What is a Semaphore?

A

A special integer value for signalling between processes.

The value could indicate the number of available units of some resource.

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

What is a major difference between a semaphore and a mutex?

A

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

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

What is an atomic operation?

A

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
7
Q

Compare semaphores to condition variables

A

CV signal is lost if no thread is waiting

cv_wait() always blocks, sem_wait() may or may not block.

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

Can semaphores be shared between processes?

A

YES - set this in sem_init

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

Race conditions are not a problem among processes, only among threads.
True or False?

A

False

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

What is the main difference between
pthread_cond_signal() for mutex and sem_post() for
semaphore?

A

Signals can be lost

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

A mutex is identical to binary semaphore.

True or False?

A

False - Mutex is under control of the person who entered it.

Even with a binary semaphore, another thread can increment or decrement (lock/unlock)

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

What does the value of the semaphore tell you?

A

Number tells you how many people can get past it (Not necessarily related to resource counting).

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