mutual exclusion – sw & hw by oded regev. outline: short review on the bakery algorithm short...

43
Mutual Exclusion Mutual Exclusion – SW & HW – SW & HW By Oded Regev By Oded Regev

Upload: travis-wilkenson

Post on 31-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Mutual Exclusion – Mutual Exclusion – SW & HWSW & HW

By Oded RegevBy Oded Regev

Page 2: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

OutlineOutline::

• Short review on the Bakery Short review on the Bakery algorithmalgorithm

• Black & White AlgorithmBlack & White Algorithm• Bounded timestampBounded timestamp• L-exclusionL-exclusion• Mutex and semaphore in HWMutex and semaphore in HW

Page 3: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Starvation-free Starvation-free AlgorithmsAlgorithms• Bakery andBakery and Black & White algorithms Black & White algorithms

are both acting as solutions to enable are both acting as solutions to enable the program to be starvation freethe program to be starvation free

• Starvation freedom: “if a process is Starvation freedom: “if a process is trying to enter its critical section, then trying to enter its critical section, then this process must eventually enter its this process must eventually enter its critical section”critical section”

• Lets review some attributes of SF Lets review some attributes of SF Algorithms…Algorithms…

Page 4: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

• Bounded-waitingBounded-waiting: “There exists a positive : “There exists a positive integer r for which the algorithm is r-bounded integer r for which the algorithm is r-bounded waiting. That is, if a given process is in its entry waiting. That is, if a given process is in its entry code, then there is a bound on the number of code, then there is a bound on the number of times any other process is able to enter its times any other process is able to enter its critical section before the given process does so”.critical section before the given process does so”.

• Linear-waitingLinear-waiting: “actually 1-bounded-waiting, : “actually 1-bounded-waiting, meaning no process can execute its critical meaning no process can execute its critical section twice while some other process is kept section twice while some other process is kept waiting”.waiting”.

• FIFOFIFO: “no beginning process can pass an already : “no beginning process can pass an already waiting process (sometimes called 0-Bounded-waiting process (sometimes called 0-Bounded-waiting)”.waiting)”.

• r-Fairnessr-Fairness: “A waiting process will be able to : “A waiting process will be able to enter its CS before all the other processes enter its CS before all the other processes collectively are able to enter their CS r+1 times”.collectively are able to enter their CS r+1 times”.

Page 5: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Bakery AlgorithmBakery Algorithm

The idea is similar to a line at the The idea is similar to a line at the bakerybakery

A customer takes a number greater A customer takes a number greater than numbers of other customersthan numbers of other customers

Each of the threads gets a unique Each of the threads gets a unique identifieridentifier

Page 6: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Bakery AlgorithmBakery Algorithm

Thread iThread i

number[i] = -1number[i] = 1 + max {number[j] | (1 j n) ,

0}for j = 1 to n { await number[j] -1 await (number[j] 0) (number[j],j)

(number[i],i)}critical sectionnumber[i] = 0

Page 7: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Bakery Attributes:Bakery Attributes:

Deadlock-freedom: If a thread is Deadlock-freedom: If a thread is trying to enter its critical section, trying to enter its critical section, then some thread, not necessarily then some thread, not necessarily the same one, eventually enters its the same one, eventually enters its critical section.critical section.

Starvation-free (each process Starvation-free (each process waiting to get into its CS part will waiting to get into its CS part will eventually get there)eventually get there)

FIFOFIFO

Page 8: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

What are the problems with What are the problems with this algorithm??this algorithm?? Computing the MaximumComputing the Maximum The size of the register number[i] The size of the register number[i]

must be unbounded.must be unbounded. Is it possible to fix this problem Is it possible to fix this problem

while using only one additional while using only one additional shared bit??shared bit??

Page 9: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The Black-White Bakery The Black-White Bakery AlgorithmAlgorithmBounding the space of the Bakery

Algorithm

Bakery (FIFO, unbounded)

The Black-White Bakery Algorithm

FIFOBounded space+ one bit

Page 10: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The Black-White Bakery The Black-White Bakery AlgorithmAlgorithm

Every process i gets a colored ticket - color(i) and Every process i gets a colored ticket - color(i) and number(i) in its entry section. The color is the same number(i) in its entry section. The color is the same as the current value of the shared bit color. The as the current value of the shared bit color. The number is greater than the maximum between the number is greater than the maximum between the processes who share the same color as process i.processes who share the same color as process i.

The order between the colored tickets is defined as The order between the colored tickets is defined as follows:follows: If 2 tickets have different colors – the ticket with the color If 2 tickets have different colors – the ticket with the color

different from the shared bit color is smaller.different from the shared bit color is smaller. If 2 tickets have the same color, the ticket with the smaller If 2 tickets have the same color, the ticket with the smaller

number is smaller.number is smaller. If 2 tickets have the same color and number (how is it If 2 tickets have the same color and number (how is it

possible?) the process with the smaller identifier enters possible?) the process with the smaller identifier enters the CS first.the CS first.

Page 11: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

How does the shared color bit is How does the shared color bit is written?written?

• The first thing process i does when it The first thing process i does when it leaves its critical section is to set the leaves its critical section is to set the color bit to a value which is different color bit to a value which is different from the color of its ticket.from the color of its ticket.

• 3 data structures are used:3 data structures are used:• A single shared bit named “color”A single shared bit named “color”• A boolean array choosing[1…n] A boolean array choosing[1…n] • An array with n-entries where each An array with n-entries where each

entry is a colored ticket.entry is a colored ticket.

Page 12: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

time

The Black-White Bakery Algorithm

0 0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

0

1

0 0

2 2

1

1

0

2

2

0

1

2

2

0

2waiting

en

try

remainder

1 20 201 2

1

1

00

color bit

Page 13: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The Black-White Bakery AlgorithmThe Black-White Bakery Algorithmcode of process i , i {1 ,..., n}

choosing[i] = truemycolor[i] = colornumber[i] = 1 + max{number[j] | (1 j n) (mycolor[j] = mycolor[i])}choosing[i] = falsefor j = 0 to n do await choosing[j] = false if mycolor[j] = mycolor[i] then await (number[j] = 0) (number[j],j) (number[i],i) (mycolor[j] mycolor[i]) else await (number[j] = 0) (mycolor[i] color) (mycolor[j] = mycolor[i]) fi odcritical sectionif mycolor[i] = black then color = white else color = black finumber[i] = 0

Page 14: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Algorithm correctness: Algorithm correctness:

Lemma 1: Assume that at time t, the value Lemma 1: Assume that at time t, the value of the color bit is c(black|white). Then, of the color bit is c(black|white). Then, any process which at time t is in its any process which at time t is in its entry section and holds a ticket with a entry section and holds a ticket with a color different than c must enter its CS color different than c must enter its CS before any process with a ticket of color before any process with a ticket of color c can enter its CS.c can enter its CS.

Lemma 2: Assume that at time t the value Lemma 2: Assume that at time t the value of the color bit has changed from c to of the color bit has changed from c to the other value. Then, at time t, every the other value. Then, at time t, every process that is in its entry section has a process that is in its entry section has a ticket of color c.ticket of color c.

Page 15: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Summary – Black & WhiteSummary – Black & White

mutual exclusionmutual exclusion deadlock-freedomdeadlock-freedom FIFOFIFO Uses finite number of bounded size Uses finite number of bounded size

registers, the numbers taken by registers, the numbers taken by waiting processes can grow only up waiting processes can grow only up to n, where n is the number of to n, where n is the number of processes.processes.

Page 16: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Bounded TimestampsBounded Timestamps

• Black & White is a private case Black & White is a private case solution to a much bigger problem.solution to a much bigger problem.

• The numbers in the Bakery (and in The numbers in the Bakery (and in B&W) actually represented a B&W) actually represented a timestamp.timestamp.

• the time is infinite of course, so how the time is infinite of course, so how can we make sure that our time can we make sure that our time labeling will not over flow in time??labeling will not over flow in time??

• How important is the overflow problem in the real world?

Page 17: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

We will focus on a sequential timestamping system, in which we assume that a thread can scan and label in a single atomic step.

Think of the set of timestamps as nodes of a directed graph (called a precedence graph). There is an edge from node a to node b if a is a later timestamp than b.

Think of assigning a timestamp to a thread as placing that thread’s token on a node.

The precedence graph is irreflexive, antisymmetric, not necessarily transitive.

Page 18: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

A thread performs a scan by locating the other threads tokens, and it performs a label by moving its own token to a node a such that there is an edge from a to every other thread’s node.

Page 19: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The precedence graph for the unbounded counter used in the Bakery array appears in the last figure. Not surprisingly, the graph is infinite

Let’s view a solution for 2 threads Let’s view a solution for 2 threads only:only:

Page 20: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The only cycle in the graph T2 has length three, and there are only two threads, so the order among the threads is never ambiguous.

Let G be a precedence graph, and A and B subgraphs of G (possibly single nodes). We say that A dominates B in G if every node of A has edges directed to every node of B.

Let graph multiplication be the following composition operator for graphs: G * H, for graphs G and H, is the following non-commutative operation:

Page 21: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Replace every node v of G by a copy of H (denoted Hv), and let Hv dominate Hu in G * H if v dominates u in G.

Define the graph Tk inductively to be: 1. T1 is a single node. 2. T2 is the three-node graph defined

above. 3. For k > 2, Tk = T2 * Tk-1.

Page 22: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

How can we scale this to more than How can we scale this to more than 2 threads?2 threads?

Page 23: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Summary - Bounded Summary - Bounded TimestampsTimestamps The key to understanding the n-thread labeling

algorithm is that the nodes covered by tokens can never form a cycle. As mentioned, two thread can never form a cycle on T2, because the shortest cycle in T2 requires three nodes.

How does the label method work for three threads? When A calls the label method, if both of the other threads have tokens on the same T2 subgraph, then move to a node on the next highest T2 subgraph, the one whose nodes dominate that T2 subgraph.

Page 24: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

L-exclusion problem:L-exclusion problem:

How to design an algorithm which guarantees How to design an algorithm which guarantees that up to L processes and no more may that up to L processes and no more may simultaneously access some shared resource simultaneously access some shared resource (their CS).(their CS).

A solution is required to withstand the slow-A solution is required to withstand the slow-down or even crash of up to L-1 processes down or even crash of up to L-1 processes (more explicitly, a single failure of a process (more explicitly, a single failure of a process at the head of the queue should not tie up all at the head of the queue should not tie up all the resources).the resources).

The bus ride story…The bus ride story… Teller in a bank story…Teller in a bank story…

Page 25: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Some definitions:Some definitions: L-exclusion: “no more than l processes L-exclusion: “no more than l processes

are at their CS at the same time.”are at their CS at the same time.” L-deadlock-freedom: “if strictly fewer L-deadlock-freedom: “if strictly fewer

than l processes fail, then if a process is than l processes fail, then if a process is trying to enter its CS, then some process, trying to enter its CS, then some process, not necessarily the same one, eventually not necessarily the same one, eventually enters its CS.”enters its CS.”

L- starvation-freedom: “if strictly fewer L- starvation-freedom: “if strictly fewer than l processes fail, then any correct than l processes fail, then any correct process that is trying to enter its CS must process that is trying to enter its CS must eventually enter it.”eventually enter it.”

Page 26: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Explain why the FIFO property and Explain why the FIFO property and the L-deadlock-freedom property the L-deadlock-freedom property cannot be mutually satisfied when cannot be mutually satisfied when L>1 ??L>1 ??

Lets see an example of a L-Lets see an example of a L-starvation-free algorithm…starvation-free algorithm…

Page 27: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Peterson L-starvation-freePeterson L-starvation-free algorithm algorithm

Initially b[1..n] all 0, turn[1…n-L] all 1

for level = 1 to n-L dob[i] = level;turn[level] = i;repeat

counter = 0;’for k = 1 to n do

if b[k] >= level then counter = counter + 1;until (counter <= n-level or turn[level] != i)

od;Critical section;B[i] = 0;

Page 28: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Properties of the algorithm:Properties of the algorithm:

Satisfied L-exclusion and L-starvation-freedom.Satisfied L-exclusion and L-starvation-freedom. L-exclusion: exactly one process waits at each L-exclusion: exactly one process waits at each

level. Once L processes enter their CS any level. Once L processes enter their CS any other active process must be waiting on the n-other active process must be waiting on the n-L levels.L levels.

L-deadlock-free: assume to the contrary… L-deadlock-free: assume to the contrary… The fact that the algorithm is L-deadlock-free The fact that the algorithm is L-deadlock-free

and the fact that at any level a new process and the fact that at any level a new process always releases (by setting turn[level] to its always releases (by setting turn[level] to its value) all the other processes at the level that value) all the other processes at the level that came before it to climb up to the next level came before it to climb up to the next level ensures L-starvation-freedomensures L-starvation-freedom

Page 29: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Mutex and semaphore in Mutex and semaphore in HWHW HW features can make any programming HW features can make any programming

task easier and improve system task easier and improve system efficiency.efficiency.

In general we can state that any solution In general we can state that any solution to the CS problem requires that critical to the CS problem requires that critical regions be protected by locks.regions be protected by locks.

in this section we will explore some in this section we will explore some simple HW instructions that are avilable simple HW instructions that are avilable on many systems and show how they can on many systems and show how they can be used effectively to solve the CS be used effectively to solve the CS problemproblem

Page 30: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The CS could be solved simply in a The CS could be solved simply in a uniprocessor environment if we could uniprocessor environment if we could prevent interrupts from occurring prevent interrupts from occurring while a shared variable was being while a shared variable was being modified. This is the approach taken modified. This is the approach taken by nonpreemptive kernels. This by nonpreemptive kernels. This approach is extremly inefficient inapproach is extremly inefficient in multi processor environment. multi processor environment.

We will explore 4 HW based atomic We will explore 4 HW based atomic instructions:instructions: TestAndSet()TestAndSet() Swap()Swap() Wait()Wait() Signal() Signal()

Page 31: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Boolean TestAndSet (boolean *target) {Boolean TestAndSet (boolean *target) { boolean rv = *target;boolean rv = *target;

*target = TRUE;*target = TRUE;return rv;return rv;

}}

Mutex implementation with TestAndSet()Mutex implementation with TestAndSet()Do {Do {

while (TestAndSet(&lock) )while (TestAndSet(&lock) ); // do nothing; // do nothing

// critical section// critical sectionlock = FALSE;lock = FALSE;//remainder section//remainder section

}while(TRUE)}while(TRUE)Is this implementation is good enough to fulfill our Is this implementation is good enough to fulfill our

needs??needs??

Page 32: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

void Swap (boolean *a, boolean *b) {void Swap (boolean *a, boolean *b) {boolean temp = *a;boolean temp = *a;*a = *b;*a = *b;*b = temp;*b = temp;

}}Mutex implementation with Swap()Mutex implementation with Swap()do {do {

key = TRUE;key = TRUE;while (key == TRUE)while (key == TRUE)

Swap(&lock, &key);Swap(&lock, &key);// critical section// critical sectionlock = FALSE;lock = FALSE;

//remainder section//remainder section} while (TRUE)} while (TRUE)

Page 33: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Although the last 2 algorithm satisfy Although the last 2 algorithm satisfy the mutual-exclusion requirement, the mutual-exclusion requirement, they do not satisfy the bounded-they do not satisfy the bounded-waiting requirement.waiting requirement.

The next algorithm will stasfied all The next algorithm will stasfied all the CS requirments:the CS requirments:

The common data-structures are:The common data-structures are: Boolean waiting[n];Boolean waiting[n]; Boolean lock;Boolean lock;

Process i can enter its CS only if Process i can enter its CS only if either (waiting[i] == false) or (key either (waiting[i] == false) or (key == false)== false)

Page 34: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

do {do {waiting[i] = TRUE;waiting[i] = TRUE;key = TRUE;key = TRUE;while (waiting[i] && key)while (waiting[i] && key)

key = TestAndSet(&lock);key = TestAndSet(&lock);waiting[i] = FALSE;waiting[i] = FALSE;

//critical section//critical sectionj = (i+1) % n;j = (i+1) % n;while ((j != i) && !waiting[j])while ((j != i) && !waiting[j])

j = (j+1) % n;j = (j+1) % n;

if (j==i)if (j==i)lock = FALSE;lock = FALSE;

elseelsewaiting[j] = FALSE:waiting[j] = FALSE:

//remainder section//remainder section} while (TRUE)} while (TRUE)

Page 35: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Semaphores:Semaphores: A semaphore S is an integer variable that, A semaphore S is an integer variable that,

apart from initialization, is accessed only apart from initialization, is accessed only through 2 standard atomic operations: through 2 standard atomic operations: wait() and signal().wait() and signal().

wait(S) {wait(S) {while (S <= 0)while (S <= 0)

; //no-op; //no-opS--;S--;

}}

Signal(s) {Signal(s) {S++;S++;

}}

Page 36: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Usage:Usage: Operating systems often distinguish between Operating systems often distinguish between

counting and binay semaphores.counting and binay semaphores. The value of a The value of a counting semaphorecounting semaphore can range can range

over unrestricted domain.over unrestricted domain. The value of a The value of a binary semaphorebinary semaphore can range only can range only

between 0 and 1.between 0 and 1. Binary semaphore can be used to solve the critical Binary semaphore can be used to solve the critical

section problem.section problem. Counting semaphore can be used to control access Counting semaphore can be used to control access

to a given resource consisting of a finite number of to a given resource consisting of a finite number of instances.instances.

We can also use semaphores to solve various We can also use semaphores to solve various synchronization problems.synchronization problems.

Page 37: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

For example, consider 2 running For example, consider 2 running processes P1 with a statement S1 processes P1 with a statement S1 and P2 with S2. suppose we require and P2 with S2. suppose we require that S2 be executes only after S1 has that S2 be executes only after S1 has completed.completed.

In P1In P1S1;S1;Signal(S)Signal(S)

In P2In P2Wait(S); Wait(S); S2;S2;

S is initialized to 0.S is initialized to 0.

Page 38: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

ImplementationImplementation The main disadvantage of the semaphore The main disadvantage of the semaphore

definition given here is that it requires definition given here is that it requires busy waitingbusy waiting..

Busy waiting wastes CPU cycles that Busy waiting wastes CPU cycles that some other process might be able to use some other process might be able to use productively.productively.

This type of semaphore is also called a This type of semaphore is also called a spinlockspinlock

We can modify the definition of the wait() We can modify the definition of the wait() and signal() operations as follows…and signal() operations as follows…

Page 39: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Instead of engaging in busy-waiting the Instead of engaging in busy-waiting the process can block itself.process can block itself.

The block() operation places a process The block() operation places a process into a waiting queue associated with into a waiting queue associated with the semaphore, and the state of the the semaphore, and the state of the process is switched to waiting state.process is switched to waiting state.

A process that is blocked, waiting on a A process that is blocked, waiting on a semaphore S, should be restarted when semaphore S, should be restarted when some other process execute a signal() some other process execute a signal() operation.operation.

The process is restarted by wakeup() The process is restarted by wakeup() operation, which changes the state of at operation, which changes the state of at most one process from waiting to ready.most one process from waiting to ready.

Page 40: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

To implement semaphores under this definition To implement semaphores under this definition we define a semaphore as a C struct.we define a semaphore as a C struct.

typedef struct {typedef struct {int value;int value;struct process *list;struct process *list;

} semaphore;} semaphore;

• The wait() operation can now be defined as:The wait() operation can now be defined as:wait(semaphore *S) {wait(semaphore *S) {

S->value--;S->value--;if (S->value < 0) {if (S->value < 0) {

add this process to S->list;add this process to S->list;block();block();

}}}}

Page 41: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

The signal() operation can now be defined as:The signal() operation can now be defined as:signal(semaphore *S) {signal(semaphore *S) {

S->value++;S->value++;if (S->value <= 0) {if (S->value <= 0) {remove a process P from S->listremove a process P from S->listwakeup(P);wakeup(P);}}

}}• The block() operation suspends the process The block() operation suspends the process

that invokes it. The wakeup(P) operation that invokes it. The wakeup(P) operation resumes the execution of a blocked process resumes the execution of a blocked process P. P.

• These 2 operations are provided by the These 2 operations are provided by the operating system as a basic system calls.operating system as a basic system calls.

Page 42: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

Some notesSome notes:: In the definition with the busy-waiting, the In the definition with the busy-waiting, the

value of the semaphore is never negative.value of the semaphore is never negative. In the second definition, if the value is In the second definition, if the value is

negative it represent the number of negative it represent the number of processes waiting on that semaphore.processes waiting on that semaphore.

The critical aspect of semaphores is that The critical aspect of semaphores is that they must be executed atomically.they must be executed atomically.

We must guarantee that no 2 processes We must guarantee that no 2 processes can execute wait() and signal() on the can execute wait() and signal() on the same semaphore at the same time.same semaphore at the same time.

How can we do it in a single processor How can we do it in a single processor environment?environment?

What about multiprocessor environment??What about multiprocessor environment??

Page 43: Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black

In conclusion:In conclusion:

• Short review on the Bakery Short review on the Bakery algorithmalgorithm

• Black & White AlgorithmBlack & White Algorithm• Bounded timestampBounded timestamp• L-exclusionL-exclusion• Mutex and semaphore in HWMutex and semaphore in HW

Thank YouThank You