31. Semaphores Flashcards

1
Q

What is a semaphore?

A

A semaphore is an object with integer value that we can manipulate with two routines: sem_wait() and sem_post() .

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

What does semaphore equal to when negative?

A

The value of the semaphore, when negative, is equal to the number of waiting threads.

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

How is semaphore called when it’s used as a lock? What does its value equal to?

A

It’s called binary semaphore and its value is equal to 1.

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

What value should semaphore have if it’s used for ordering?

A

0

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

What value should semaphore be initialized to when used for consumer/producer problem?

A

One semaphore should be initialized to 0 (to identify emptiness) and the other one should be initialized to the size of buffer.

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

What is other use case of semaphore?

A

Thread throttling. By surrounding resource-intensive region of code with semaphore, we can limit the amount of thread entering this area at the same time.

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