1 / 12

Resource Allocation

Resource Allocation. Centralized Mutex Algorithm. Send requests to Leader Leader maintains a pending queue of events Requests are granted in the order they are received. // Centralized mutex algorithm public class CentMutex extends Process implements Lock { . . .

chaka
Télécharger la présentation

Resource Allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Resource Allocation

  2. Centralized 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 publicclass CentMutex extends Process implements Lock { . . . publicsynchronizedvoid requestCS() { sendMsg(leader, "request"); while (!haveToken) myWait(); } publicsynchronizedvoid releaseCS() { sendMsg(leader, "release"); haveToken = false; } publicsynchronizedvoid handleMsg(Msg m, int src, String tag) { if (tag.equals("request")) { if (haveToken){ sendMsg(src, "okay"); haveToken = false; } else pendingQ.add(src); } elseif (tag.equals("release")) { if (!pendingQ.isEmpty()) { int pid = pendingQ.removeHead(); sendMsg(pid, "okay"); } else haveToken = true; } elseif (tag.equals("okay")) { haveToken = true; notify(); } } }

  4. 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

  5. // Lamport’s mutual exclusion algorithm publicclass LamportMutex extends Process implements Lock { publicsynchronizedvoid requestCS() { v.tick(); q[myId] = v.getValue(myId); broadcastMsg("request", q[myId]); while (!okayCS()) myWait(); } publicsynchronizedvoid 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)) returnfalse; if(isGreater(q[myId], myId, v.getValue(j), j))return false; } returntrue; } publicsynchronizedvoid 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)); } elseif (tag.equals("release")) q[src] = Symbols.Infinity; notify(); // okayCS() may be true now } }

  6. 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

  7. publicclass RAMutex extends Process implements Lock { publicsynchronizedvoid requestCS() { c.tick(); myts = c.getValue(); broadcastMsg("request", myts); numOkay = 0; while (numOkay < N-1) myWait(); } publicsynchronizedvoid releaseCS() { myts = Symbols.Infinity; while (!pendingQ.isEmpty()) { int pid = pendingQ.removeHead(); sendMsg(pid, "okay", c.getValue()); } } publicsynchronizedvoid 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); } elseif (tag.equals("okay")) { numOkay++; if (numOkay == N - 1) notify(); // okayCS() may be true now } } }

  8. 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

  9. publicclass DinMutex extends Process implements Lock { publicsynchronizedvoid 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(); } publicsynchronizedvoid 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]) returnfalse; returntrue; } publicsynchronizedvoid 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; } } } elseif (tag.equals("Fork")) { fork[src] = true; dirty[src] = false; if (haveForks()) { myState = eating; notify(); } } } }

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

  11. publicclass CircToken extends Process implements Lock { publicsynchronizedvoid initiate() { if (haveToken) sendToken(); } publicsynchronizedvoid requestCS() { wantCS = true; while (!haveToken) myWait(); } publicsynchronizedvoid releaseCS() { wantCS = false; sendToken(); } void sendToken() { . . . } publicsynchronizedvoid handleMsg(Msg m, int src, String tag) { if (tag.equals("token")) { haveToken = true; if (wantCS) notify(); else { Util.mySleep(1000); sendToken(); } } } }

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

More Related