110 likes | 199 Views
Slide C3. load(K); add(100); store(K); load(K); sub(200); store(K);. Kreditoperasjon: Debetoperasjon:. K = K + 100; K = K – 200;. load(K); load(K); sub(200); store(K); add(100); store(K);. load(K); sub(200); load(K); add(100); store(K); store(K);. Slide C9.
E N D
Slide C3 load(K); add(100); store(K); load(K); sub(200); store(K); Kreditoperasjon: Debetoperasjon: K = K + 100; K = K – 200; load(K); load(K); sub(200); store(K); add(100); store(K); load(K); sub(200); load(K); add(100); store(K); store(K);
Slide C9 public class MutualExclusion implements Runnable { private final static int n = <No of processes>; public void run() { do { enterCritical(<Region>); <Critical Region>; exitCritical(<Region>); <Remaining part> } while(true) } public static void main(String[] args) { MutualExclusion m = new MutualExclusion(); for(int i = 0; i < n; i++) { (new Thread(m)).start(); } } }
Slide C10 Dekkers korrekte: Peterson’s korrekte: int turn; // 0..1 boolean[] flag = new boolean[2];
Slide C11 public boolean testSet(Integer i) { if(i.getValue() == 0) { i.setValue(1); return true; } else return false; } public void exchange(Register r, Memory m) { int temp = m.getValue(); m.setValue(r.getValue()); r.setValue(temp); }
Slide C13 PRODUCER: do { <Produce item v>; buf[in] = v; in = (in +1) % n; } while(true); buf[n-1] … buf[3] buf[2] buf[1] buf[0] CONSUMER: do { w = buf[out]; out = (out + 1) % n; <Consume item w>; } while(true);
Slide C18 public class Region { private Semaphore s = 1; … } public void enterCritical(Region r) { wait(r.s); } public void exitCritical(Region r) { signal(r.s); } Semaphore b = n / 1; Semaphore q = 0; Semaphore a = 0; Klientprosess: do { <Generate Question>; wait(b); <Write Message>; signal(q); wait(a); <Read Message>; signal(b); <Apply Answer>; } while(true); Tjenerprosess: do { wait(q); <Read Message>; signal(b); <Apply Question>; <Generate Answer>; wait(b); <Write Message>; signal(a); } while(true); Helst: endre wait til sWait og signal til sSignal !?
Slide C19 public void wait(Semaphore s) { <Inhibit Interrupts>; s.count = s.count – 1; if(s.count < 0) { <Place This process in s.queue>; <Block This process &Allow Interrupts>; } else <Allow Interrupts>; } public void signal(Semaphore s) { <Inhibit Interrupts>; s.count = s.count + 1; if(s.count <= 0) { <Remove process p from s.queue>; <Place process p on Ready list>; } <Allow Interrupts>; } public void wait(Semaphore s) { while(! testSet(s.flag) ) { } s.count = s.count – 1; if(s.count < 0) { <Place This process in s.queue>; <Block This process & s.flag = 0>; } else s.flag = 0; } public void signal(Semaphore s) { while(! testSet(s.flag) ) { } s.count = s.count + 1; if(s.count <= 0) { <Remove process p from s.queue>; <Place process p on Ready list>; } s.flag = 0; }
Slide C22 public class Region { private Monitor m; … } public void enterCritical(Region r) { - - - } public void exitCritical(Region r) { - - - } public class Monitor { private Buffer questionBuffer, answerBuffer; public void putQuestion(Question q) { ... } public Question getQuestion() { ... } public void putAnswer(Answer a) { ... } public Answer getAnwer() { ... } } Klientprosess: do { <Generate Question q>; m.putQuestion(q); a = m.getAnswer(); <Apply Answer a>; } while(true); Tjenerprosess: do { q = m.getQuestion(); <Apply Question q>; <Generate Answer a>; m.putAnswer(a); } while(true);
Slide C23 private Semaphore m = 1; private Semaphore n = 0; private int nc = 0; Semaphore[] c = {0,0,0,...,0} // v 0’s Semaphore[] cc = {0,0,0,...,0} // v 0’s <Enter method>: wait(m); <Exit method>: if(nc > 0) signal(n); else signal(m); public void cWait(int i) { cc[i] = cc[i] + 1; if(nc > 0) signal(n); else signal(m); wait(c[i]); cc[i] = cc[i] - 1; } public void cSignal(int i) { if(cc[i] > 0) { nc = nc + 1; signal(c[i]); wait(n); nc = nc - 1; } }
Slide C26 public class Region { public Mailbox mailbox; … } Message[] msg = new Message[n]; send(r.mailbox, msg[0]); public void enterCritical(Region r) { msg[i] = receive(r.mailbox); } public void exitCritical(Region r) { send(r.mailbox, msg[i]); } private Port cPort, sPort; Klientprosess: do { <Generate Question q>; send(sPort, new Message(q)); a = receive(sPort).getAnswer(); <Apply Answer a>; } while(true); Tjenerprosess: do { q = receive(cPort).getQuestion(); <Apply Question q>; <Generate Answer a>; send(cPort, new Message(a)); } while(true);
Slide C27 Semaphore slot = n; Semaphore buffer = 1; Semaphore[] port = {0,0,...,0}; // m 0’s ItemList bufferQ; ItemList[] portQs = new ItemList[m]; for(int i = 0; i < n; i++) <Link Item into bufferQ>; public void send(Port destination, Message msg) { wait(slot); wait(buffer); <Unlink Item from bufferQ>; <Copy msg into Item>; <Link Item into portQs[destination]>; signal(port[destination]); signal(buffer); } public void receive(Port source, Message msg) { wait(port[this]); wait(buffer); <Unlink Item from portQs[this]>; <Copy msg from Item>; <Link Item into bufferQ>; signal(slot); signal(buffer); }