semaphores Flashcards
(20 cards)
what does semaphores do
Synchronisation too
Simplifies synchronisation for the programmer
Does not require (much) busy waiting: We don’t busy-wait for the critical section, usually only to achieve atomicity to check
if we need to sleep or not, etc.
Can guarantee bounded waiting time and progress
what do semahpres consist of
A semaphore type S, that records a list of waiting processes and an integer
Two standard atomic (← very important) operations by which to modify S: wait() and signal()
Originally called P() and V() based on the equivalent Dutch words
what does semaphore type S do
records a list of waiting processes and an integer
what are the sutch equivalent to wait() and signal()
P() and V()
how does semaphores work?
the semaphore is initialised with a count value of the
maximum number of processes allowed in the critical section
at the same time.
When a process calls wait(), if count is zero, it adds itself to
the list of sleepers and blocks, else it decrements count and
enters the critical section
When a process exits the critical section it calls signal(),
which increments count and issues a wakeup call to the
process at the head of the list, if there is such a process
It is the use of ordered wake-ups (e.g. FIFO) that makes
semaphores support bounded (i.e. fair) waiting.
what are the 2 types of semaphores
binary semaphores
counting semaphores
what is a counting semaphore
integer value can range over an
unrestricted domain (e.g. allow at most N threads to read a
database, etc.)
what is a binary semaphore
integer value can range only between 0 and 1
Also known as mutex locks, since ensure mutual exclusion.
Basically, it is a counting semaphore initialised to 1
what else are binary semaphores known as and why
Also known as mutex locks, since ensure mutual exclusion
how can you implemey semaphore in a kernel using state and wait
(code)
typedef struct {
int count;
process_list; // Hold a list of waiting processes/threads
} Semaphore;
void wait(Semaphore *S) {
S->count–;
if (S->count < 0) {
add process to S->process_list;
sleep();
}
}
in our state and wait implementation we decrement the wait counter, why?
this does not alter functionality but has the useful side-effect that the negative count value reveals
how many processes are
currently blocked on the semaphore.
code for signal semaphore implementation
void signal(Semaphore *S) {
S->count++;
if (S->count <= 0) { // If at least one waiting process, let him in.
remove next process, P, from S->process_list
wakeup(P);
}
}
what is deadlock
Two or more processes are waiting indefinitely for
an event that can be caused by only one of the waiting
processes
what is priority inversion
Scheduling problem when lower-priority
process holds a lock needed by higher-priority process
what do we have to look out for in semaphores
deadlock
priority inversion
what are the 2 problems semaphores face
bounded buffer problem
readers-writers problem
describe the bounded buffer problem solution
The solution set-up:
A buffer to hold N items, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full slots initialized to the value 0
Semaphore empty slots initialized to the value N
describe readers-writers problem
A data set is shared among a number of concurrent processes
Readers - only read the data set; they do not perform any updates
Writers - can both read and write
Problem:
Allow multiple readers to read at the same time if there is no writer in there.
Only one writer can access the shared data at the same time