Title: Resource Allocation
1Resource Allocation
2Centralized Mutex Algorithm
- Send requests to Leader
- Leader maintains a pending queue of events
- Requests are granted in the order they are
received
3// Centralized mutex algorithm public class
CentMutex extends Process implements Lock . .
. public synchronized void requestCS()
sendMsg(leader, "request") while
(!haveToken) myWait() public
synchronized void releaseCS()
sendMsg(leader, "release") haveToken
false public synchronized void
handleMsg(Msg m, int src, String tag)
if (tag.equals("request")) if
(haveToken) sendMsg(src,
"okay") haveToken false
else pendingQ.add(src)
else if (tag.equals("release"))
if (!pendingQ.isEmpty()) int
pid pendingQ.removeHead()
sendMsg(pid, "okay") else
haveToken true else if
(tag.equals("okay")) haveToken
true notify()
4Lamports Algorithm
- Ensures that processes enter the critical section
in the order of timestamps of their requests - Requires 3(N-1) messages per invocation of the
critical section
5// Lamports mutual exclusion algorithm public
class LamportMutex extends Process implements
Lock public synchronized void requestCS()
v.tick() qmyId
v.getValue(myId) broadcastMsg("request",
qmyId) while (!okayCS()) myWait()
public synchronized void releaseCS()
qmyId Symbols.Infinity
broadcastMsg("release", v.getValue(myId))
boolean okayCS() for (int j 0 j lt
N j) if(isGreater(qmyId, myId,
qj, j)) return false
if(isGreater(qmyId, myId, v.getValue(j),
j))return false return true
public synchronized void handleMsg(Msg m,
int src, String tag) int timeStamp
m.getMessageInt() v.receiveAction(src,
timeStamp) if (tag.equals("request"))
qsrc timeStamp sendMsg(src,
"ack", v.getValue(myId)) else if
(tag.equals("release")) qsrc
Symbols.Infinity notify() // okayCS()
may be true now
6Ricart and Agrawalas algorithm
- Combines the functionality of acknowledgement and
release messages - Uses only 2(N-1) messages per invocation of the
critical section
7public class RAMutex extends Process implements
Lock public synchronized void requestCS()
c.tick() myts c.getValue()
broadcastMsg("request", myts)
numOkay 0 while (numOkay lt N-1)
myWait() public synchronized void
releaseCS() myts Symbols.Infinity
while (!pendingQ.isEmpty())
int pid pendingQ.removeHead()
sendMsg(pid, "okay", c.getValue())
public synchronized void handleMsg(Msg m,
int src, String tag) int timeStamp
m.getMessageInt() c.receiveAction(src,
timeStamp) if (tag.equals("request"))
if ((myts Symbols.Infinity )
(timeStamp lt myts) ((timeStamp
myts)(srcltmyId)))//not interested in CS
sendMsg(src, "okay", c.getValue())
else pendingQ.add(src) else
if (tag.equals("okay")) numOkay
if (numOkay N - 1) notify() //
okayCS() may be true now
8Dining Philosopher Algorithm
- Eating rule A process can eat only when it is a
source - Edge reversal After eating, reverse orientations
of all the outgoing edges
9public class DinMutex extends Process implements
Lock public synchronized void requestCS()
myState hungry if
(haveForks()) myState eating else
for (int i 0 i lt N i)
if (requesti !forki)
sendMsg(i, "Request") requesti false
while (myState ! eating)
myWait() public synchronized void
releaseCS() myState thinking
for (int i 0 i lt N i)
dirtyi true if (requesti)
sendMsg(i, "Fork") forki false
boolean haveForks() for
(int i 0 i lt N i) if (!forki)
return false return true
public synchronized void handleMsg(Msg m, int
src, String tag) if (tag.equals("Request
")) requestsrc true
if ((myState ! eating) forksrc
dirtysrc) sendMsg(src,
"Fork") forksrc false if
(myState hungry)
sendMsg(src, "Request") requestsrc false
else if
(tag.equals("Fork")) forksrc
true dirtysrc false if
(haveForks()) myState eating notify()
10Token based algorithm
- Use a token for resource allocation problems
- A process needs the token to access the critical
section
11 public class CircToken extends Process
implements Lock public synchronized void
initiate() if (haveToken) sendToken()
public synchronized void requestCS()
wantCS true while (!haveToken)
myWait() public synchronized void
releaseCS() wantCS false
sendToken() void sendToken() . .
. public synchronized void
handleMsg(Msg m, int src, String tag)
if (tag.equals("token")) haveToken
true if (wantCS) notify()
else Util.mySleep(1000)
sendToken()
12Quorum based algorithms
- Request permission from a subset of processes
- Crumbling walls