chapter08

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Resource Allocation : 

Resource Allocation

Centralized Mutex Algorithm : 

Centralized Mutex Algorithm Send requests to Leader Leader maintains a pending queue of events Requests are granted in the order they are received

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

Lamport’s Algorithm : 

Lamport’s 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

Slide 5: 

// Lamport’s mutual exclusion algorithm public class LamportMutex extends Process implements Lock { public synchronized void requestCS() { v.tick(); q[myId] = v.getValue(myId); broadcastMsg("request", q[myId]); while (!okayCS()) myWait(); } public synchronized void releaseCS() { q[myId] = Symbols.Infinity; broadcastMsg("release", v.getValue(myId)); } boolean okayCS() { for (int j = 0; j < N; j++){ if(isGreater(q[myId], myId, q[j], j)) return false; if(isGreater(q[myId], 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")) { q[src] = timeStamp; sendMsg(src, "ack", v.getValue(myId)); } else if (tag.equals("release")) q[src] = Symbols.Infinity; notify(); // okayCS() may be true now } }

Ricart and Agrawala’s algorithm : 

Ricart and Agrawala’s algorithm Combines the functionality of acknowledgement and release messages Uses only 2(N-1) messages per invocation of the critical section

Slide 7: 

public class RAMutex extends Process implements Lock { public synchronized void requestCS() { c.tick(); myts = c.getValue(); broadcastMsg("request", myts); numOkay = 0; while (numOkay < 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 < myts) ||((timeStamp == myts)&&(src<myId)))//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 } } }

Dining Philosopher Algorithm : 

Dining 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

Slide 9: 

public class DinMutex extends Process implements Lock { public synchronized void requestCS() { myState = hungry; if (haveForks()) myState = eating; else for (int i = 0; i < N; i++) if (request[i] && !fork[i]) { sendMsg(i, "Request"); request[i] = false; } while (myState != eating) myWait(); } public synchronized void releaseCS() { myState = thinking; for (int i = 0; i < N; i++) { dirty[i] = true; if (request[i]) { sendMsg(i, "Fork"); fork[i] = false; } } } boolean haveForks() { for (int i = 0; i < N; i++) if (!fork[i]) return false; return true; } public synchronized void handleMsg(Msg m, int src, String tag) { if (tag.equals("Request")) { request[src] = true; if ((myState != eating) && fork[src] && dirty[src]) { sendMsg(src, "Fork"); fork[src] = false; if (myState == hungry){ sendMsg(src, "Request"); request[src] = false; } } } else if (tag.equals("Fork")) { fork[src] = true; dirty[src] = false; if (haveForks()) { myState = eating; notify(); } } } }

Token based algorithm : 

Token based algorithm Use a token for resource allocation problems A process needs the token to access the critical section

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

Quorum based algorithms : 

Quorum based algorithms Request permission from a subset of processes Crumbling walls

authorStream Live Help