OS - semaphores Flashcards

1
Q

what’s wrong with busy waiting?

A
  1. wasting CPU time - but efficient if waiting-time is short because context switch takes time.
  2. May cause dead lock
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can busy waiting cause deadlock?

A

Process A’s priority is higher than process B’s

Process B enters the CS

Process A needs to enter the CS, busy-waits for B to exit the CS.

Process B cannot execute as long as the higher-priority process A is executing/ready.

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

What is a semaphore?

A

Semaphore is an object used to solve the mutual exclusion problem, which holds two atomic operations:

down(S) - if S=0, the process is blocked. else: S–;

up(S) - if there are blocked processes, wake-up one of them. else: S++

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

Is the following algorithm enough to provide mutex,deadlock- freedom and starvation-freedom?

down(S)

CS

up(S)

A

mutex - yes. Because the functions are atomic

deadlock - yes. Because the functions are atomic

starvation - depends.. we can play with it as in the attached image:

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

what is interesting about negative-valued semaphores? how is it implemented?

A

If S is negative, then there are -S blocked processes.

implementation attached.

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

How is a spinlock implemented using TSL(test-set-lock).

recall: spinlock does not use busy waiting.

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

what is the problem with the following implementation of Negative Sempahore with TSL?

A

In down(), resetting flag and sleeping should be atomic:

if sleep occurs first - no other process can enter test-and-set to wake up the process. If setting the flag occurs first, then we might have context switch right before we sleep. In that time some process might pass test-and-set to execute wakeup(p). then the former process would return to run to execute sleep without having someone to wake him up.

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

This code does not work. Why?

A

No counting of the waiting threads.

If 3 processes are waiting between line L1 and L2 while 3 other processes execute UP(S) completely, UP(S2) will be called three times, but since it is a binary operation - S2 = 1 by the end of the 3 calls.

When the 3 processes execute L2: Down(S2), only one should pass while the rest will be waiting even though they should have been sent free.

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

Can you recall the smallest change we did in the implementation of the counting sempahore with binary semaphores?

A

up(S): we added “else”.

down(S): we removed “else”

This changes assures up(S2) must be followed by down(S2) that must be followed by up(S2) and so on..

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

What is the “bounded buffer algorithm”?

clue: producer and consumer

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

The implementation of the counting semaphore using binary semaphores such that it alternates between up(S2) and down(S2) is problematic. with the bounded buffer, we don’t want to have producer->consumer->producer->consumer.. we want to enable a few producers/consumer to enter CS consecutively.

How this problem is fixed, remember?

A

Using the “wake” variable, we “remember” the number of times up(S2) has occured. this way, even if up(S2) occured a couple of times before down(S2) occured, we release all the processes sleeping on S2 according to the “wake” variable. Note that the increament and decreament of “wake” is done within the S1 semaphore, thus it is atomic like.

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

What is wrong about the Kearns algorithm that tries to fix the alternation problem?

A

Assume 8 processes execute down(S). that is, 7 sleep on down(S2). now assume another 4 processes execute up(S) making wake equal 4. now, 4 sleeping processes are released from S2, assume the first process calls up(S2) before the next one to wake up execute wake –. that is, another process has awaken if though we should have released only 4 in total.

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

How Hemmendinger fixed the “wake” issue?

A

Instead of waking all of the sleeping threads in up(S), he wakes up only one of them, but keeps the count.

Then, in down(S), the one released in responsible to free all the rest =]

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

In the following(working and simpler) algorithm, can we switch the order of downs in down(S)?

A

Of course we can’t! if down(S1) comes before down(S2), once some process is waiting on S2, we also wait on S1!

this way we can’t execute up(S2) and we’re stuck.

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

Does this simpler,working counting semaphore prevent starvation?

A

Absolutely no! there is no order for the waiting processes. process might enter down(S2) and call up(S2) and then enter again while some other process is waiting long for his turn

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

Event Counter E is an integer counter with three operations, what are they?

A

Advance(E): increment E by 1, wake up relevant sleepers.

Await(E,v): wait until E>=v. sleep if E<v></v>

<p>Read(E): return the current value of E</p>

</v>

17
Q

Why should we use Message Passing?

A

If we have a multiprocessor system without shared memory, we can implement synchronization by message passing.