chapter 31 - semaphores Flashcards

(11 cards)

1
Q

What is a semaphore

A

A semaphore holds a counter representing the number of allowed accesses to a resource

sem_wait() and sem_post() operations

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

what does a semaphore value say

A

semaphore’s value represents how many “resources” are available

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

What does sem_wait() do?

A

If the value > 0, it decrements and continues. If ≤ 0, the thread blocks and waits

Wait until the resource is available, then take one

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

What does a semaphore value < 0 mean?

A

That many threads are blocked waiting

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

What does sem_post() do?

A

Increments the value; if threads are waiting, one is unblocked

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

What’s a binary semaphore used for?

A

As a lock for mutual exclusion

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

How are semaphores initialized?

A

With sem_init(). Third argument sets initial value; second is 0 for use between threads

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

How can semaphores be used as locks?

A

Binary semaphores (1=unlocked) act like mutexes
sem_wait() = lock
sem_post() = unlock

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

What happens when two threads use a binary semaphore as a lock?

A

One thread enters at a time.

Others wait (block) until it’s released.

When the holder calls sem_post(), the next waiting thread is woken up

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

Producer/Consumer: What does the empty semaphore track?

A

How many empty slots are left in the buffer

Initialized to buffer size

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

Producer/Consumer: What does the full semaphore track?

A

How many filled slots are in the buffer - initialized to 0

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