Semaphores Flashcards

1
Q

Semaphores

A

synchronization primitive
-can use as both locks and condition variables
- object with an integer value that we can manipulate with two routines:
1. sem_wait()
2. sem_post()
must initialize it to some value

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

Initializing a semaphore

A

sem_t s;
sem_init(&s, 0, 1)
sem_init(sem_t s, 0, value) // 0 to indicate that the semaphore is shared between threads in the same process

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

sem_wait

A

decrement the value of semaphore s by one

wait if value of semaphore is negative

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

sem_post()

A

-increments the value of the semaphore and if there is a thread waiting to be woken, wakes one of them up

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

Value of semaphore

A
  • 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
6
Q

Binary Semaphores (Locks)

A

-locks only have two states (held and not held), sometimes call a semaphore a binary semaphore

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

Scheduler State of each thread

A

Run: Thread is running
Read: Runnable but not running
Sleep: Thread is blocked

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