semaphores Flashcards

(20 cards)

1
Q

what does semaphores do

A

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

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

what do semahpres consist of

A

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

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

what does semaphore type S do

A

records a list of waiting processes and an integer

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

what are the sutch equivalent to wait() and signal()

A

P() and V()

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

how does semaphores work?

A

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.

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

what are the 2 types of semaphores

A

binary semaphores
counting semaphores

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

what is a counting semaphore

A

integer value can range over an
unrestricted domain (e.g. allow at most N threads to read a
database, etc.)

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

what is a binary semaphore

A

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

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

what else are binary semaphores known as and why

A

Also known as mutex locks, since ensure mutual exclusion

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

how can you implemey semaphore in a kernel using state and wait
(code)

A

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();
}
}

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

in our state and wait implementation we decrement the wait counter, why?

A

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.

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

code for signal semaphore implementation

A

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);
}
}

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

what is deadlock

A

Two or more processes are waiting indefinitely for
an event that can be caused by only one of the waiting
processes

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

what is priority inversion

A

Scheduling problem when lower-priority
process holds a lock needed by higher-priority process

17
Q

what do we have to look out for in semaphores

A

deadlock
priority inversion

18
Q

what are the 2 problems semaphores face

A

bounded buffer problem
readers-writers problem

19
Q

describe the bounded buffer problem solution

A

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

20
Q

describe readers-writers problem

A

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