Semaphores Flashcards
(24 cards)
⭐️ Give the definition:
An object with an integer value that we can manipulate with 2 routines
Semaphore
⭐️ What 2 routines can be used to manipulate a semaphore?
sem_wait & sem_post
⭐️ The motivation for semaphores is to block a processes execution until som condition is satisfied, so that we avoid…
busy waiting
⭐️ What routine for manipulation of a semaphore is described below?
Decrements sem value by 1. Waits if value of sem is < 0
wait
⭐️ What routine for manipulation of a semaphore is described below?
Increments sem value by 1. Then wakes up a single waiter if one exists.
post
⭐️ What routine for manipulation of a semaphore is described below?
Determines a semaphores behavior - the value of the semaphore when negative = number of waiting threads
initialization
⭐️ The negative value of a semaphore =
The number of waiting threads
Give the definition:
Binary semaphore
Lock
Which two operations can be used in combination with sem_init() with semaphores to solve the concurrency problem of ordering?
thr_join() & thr_exit()
Does the semaphore variable below represent the producer or the consumer in the p/c problem?
sem_t empty;
producer
Does the semaphore variable below represent the producer or the consumer in the p/c problem?
sem_t full;
consumer
What value is the sem_t value initialized to for the producer value, empty, in the p/c problem?
MAX
What value is the sem_t value initialized to for the consumer value, full, in the p/c problem?
0
When using semaphores in the p/c problem, the mutex locks are put just around the critical section to ensure that what state doesn’t occur?
Deadlock state - Avoids circular wait
⭐️ Give the definition:
A set of threads are said to be in this state when every thread in the set is waiting for an event that can be caused only by another thread in the set.
Deadlock state
⭐️ Which condition for a deadlock state is described below?
Threads claim exclusive control of resources that require e.g. a thread grabs a lock
Mutual exclusion
⭐️ Which condition for a deadlock state is described below?
Threads hold resources allocated to them while waiting for additional resources
Hold-and-wait
⭐️ Which condition for a deadlock state is described below?
Resources cannot be forcibly removed from threads that are holding them
No preemption
⭐️ Which condition for a deadlock state is described below?
There exists a circular chain of threads such that each holds one/more resources that are being requested by the next thread in the chain.
Circular wait
⭐️ A counting semaphore is initialized to 5. What does this imply?
1. Only 5 threads can exist in the system
2. Up to 5 threads can access the critical section simultaneously
3. The semaphore can only be locked 5 times before failing
4. It is equivalent to 5 binary semaphores
2.
⭐️ What happens if a thread executes wait() on a semaphore with a value of 0?
1. The thread terminates immediately
2. The thread blocks until another thread calls signal()
3. The OS throws an exception
4. The semaphore value becomes 1
2.
⭐️ In the producer-consumer problem with only semaphores, how many semaphores are typically used?
1. 1
2. 2
3. 3
4. 4
- In order to guarantee mutex when accessing the shared buffer, a binary semaphore is needed in addition to empty & full semaphores
⭐️ What is the key difference between a semaphore & a condition variable?
1. Semaphores are faster
2. Semaphores maintain state (the counter), while CVs do not
3. Conditional variables can ensure mutual exclusion
4. Semaphores needs to be used with mutexes
2.
⭐️ Two semaphores are typically used to implement a correct solution for the
producer-consumer problem (T/F)
True