PROGRAM 1
/* Warnings 1. BusyBees is not fully programmed. However, I can't find this problem in the OS literature. 2. CanvasGUI is not fully programmed. However, this class appears to be very difficult to implement since there isn't one place where the information needed can be found (state and color). 3. Port is not tested. The ports at school are temporarily unavailable. */ /* Copyright (c) Edward A. Billard, 1997 SOURCE: BusyBeesBAD.java 1. problem : implement according to specs 2. test : java BusyBeesOK 3. test : java BusyBeesBAD 4. fix : 5. compile : javac BusyBeesBAD.java 6. test : java BusyBeesBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.Semaphore; import JOS.Proc; import JOS.Priority; import JOS.JOS; class BusyBeesBAD { public static void main (String args []) { JOS.useOSmenu = false; new BusyBeesBAD (); } public BusyBeesBAD () { Semaphore aSemaphore1 = new Semaphore ("BusyBeeSemaphore1", 1); Semaphore aSemaphore2 = new Semaphore ("BusyBeeSemaphore2", 1); Semaphore aSemaphore3 = new Semaphore ("BusyBeeSemaphore3", 1); new BusyBees ("BeeOK #1", 10, 5, aSemaphore1).Start (); new BusyBees ("BeeOK #2", 10, 5, aSemaphore2).Start (); new BusyBees ("BeeOK #3", 10, 5, aSemaphore3).Start (); } } class BusyBees extends Proc { private int steps; private Semaphore aSemaphore; public BusyBees (String name, int steps, int priority, Semaphore aSemaphore) { super (name, priority, true); this.steps = steps; this.aSemaphore = aSemaphore; } public void run () { JOS.out.println (getName () + " running " + ", prio = " + getPrio ()); while (steps > 0) { aSemaphore.Wait (); JOS.out.println (getName () + " in critical section " + ", prio = " + getPrio ()); steps--; aSemaphore.Signal (); } Stop (); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: ClientServerBAD.java 1. problem : two clients and a server become deadlocked using messages 2. test : java ClientServerOK 3. test : java ClientServerBAD 4. fix : ServerBAD so the processes do not get deadlocked 5. compile : javac ClientServerBAD.java 6. test : java ClientServerBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Message; import JOS.Port; class ClientServerBAD { public static void main (String args []) { JOS.useOSmenu = false; new ClientServerBAD (); } public ClientServerBAD () { Port serverPort = new Port ("ServerPort"); Port client1Port= new Port ("Client1Port"); Port client2Port= new Port ("Client2Port"); new ServerBAD ("ServerBAD #1", JOS.steps, serverPort).Start (); new ClientOK ("ClientOK #1", JOS.steps / 2, client1Port, serverPort).Start (); new ClientOK ("ClientOK #2", JOS.steps / 2, client2Port, serverPort).Start (); } } class ClientOK extends Proc { private int steps; private Port myPort; private Port serverPort; public ClientOK (String id, int steps, Port myPort, Port serverPort) { super (id); this.steps = steps; this.myPort = myPort; this.serverPort = serverPort; } public void run () { Message clientMsg = new Message(getName () + " requests block"); JOS.out.println (getName () + " Running STUDENT VERSION"); clientMsg.setSenderPort (myPort); while (steps > 0) { JOS.out.println (getName () + " sends [" + clientMsg.getValue () + "] at step " + steps); serverPort.Send (clientMsg); Message serverMsg = myPort.Receive (); JOS.out.println(getName () + " recvs [" + serverMsg.getValue () + "] at step " + steps); steps--; } Stop (); } } class ServerBAD extends Proc { private int steps; private Port serverPort; public ServerBAD (String id, int steps, Port serverPort) { super(id); this.steps = steps; this.serverPort = serverPort; } public void run () { Message serverMsg = new Message(getName () + " returns block"); JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { Message clientMsg = serverPort.Receive (); JOS.out.println (getName ()+ " recvs [" + clientMsg.getValue () + "] at step " + steps); Port clientPort = clientMsg.getSenderPort (); JOS.out.println (getName () + " sends [" + serverMsg.getValue () + "] at step " + steps); clientPort.Send (serverMsg); steps--; } Stop (); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: DeadlockBAD.java 1. problem : two processes become deadlocked using semaphores and messages 2. test : java DeadlockOK 3. test : java DeadlockBAD 4. fix : DeadlockProc2BAD so the processes do not get deadlocked 5. compile : javac DeadlockBAD.java 6. test : java DeadlockBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Semaphore; import JOS.Port; import JOS.Message; class DeadlockBAD { public static void main (String args []) { JOS.useOSmenu = false; new DeadlockBAD (); } public DeadlockBAD () { Port aPort = new Port ("DeadlockPort"); Semaphore aSemaphore = new Semaphore ("DeadlockSem", 0); new DeadlockProc1OK ("DeadlockOK #2", JOS.steps, aPort, aSemaphore).Start (); new DeadlockProc2BAD ("DeadlockBAD #1", JOS.steps, aPort, aSemaphore).Start (); } } class DeadlockProc2BAD extends Proc { private int steps; private Port myPort; private Semaphore aSemaphore; public DeadlockProc2BAD (String id, int steps, Port myPort, Semaphore aSemaphore) { super (id); this.steps = steps; this.myPort = myPort; this.aSemaphore = aSemaphore; } public void run () { Message recvMsg; JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { aSemaphore.Signal (); recvMsg = myPort.Receive (); JOS.out.println (getName () + " recvs [" + recvMsg.getValue () + "] at step " + steps); steps--; } Stop(); } } class DeadlockProc1OK extends Proc { private int steps; private Port yourPort; private Semaphore aSemaphore; public DeadlockProc1OK (String id, int steps, Port yourPort, Semaphore aSemaphore) { super (id); this.steps = steps; this.yourPort = yourPort; this.aSemaphore = aSemaphore; } public void run () { Message sendMsg = new Message(getName () + " message"); JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { aSemaphore.Wait (); JOS.out.println(getName () + " sends [" + sendMsg.getValue () + "] at step " + steps); yourPort.Send (sendMsg); steps--; } Stop(); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: DiningPhilosophersBAD.java 1. problem : two processes become deadlocked using semaphores 2. test : java DiningPhilosophersOK 3. test : java DiningPhilosophersBAD 4. fix : DiningPhilosophersBAD so the processes do not get deadlocked 5. compile : javac DiningPhilosophersBAD.java 6. test : java DiningPhilosophersBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.Semaphore; import JOS.Proc; import JOS.JOS; class DiningPhilosophersBAD { public static void main (String args []) { JOS.useOSmenu = false; new DiningPhilosophersBAD (); } public DiningPhilosophersBAD () { Semaphore One = new Semaphore ("PhilOneSem", 1); Semaphore Two = new Semaphore ("PhilTwoSem", 1); new PhilosopherOK ("PhilosopherOK #1", JOS.steps, One, Two).Start (); new PhilosopherOK ("PhilosopherOK #2", JOS.steps, One, Two).Start (); } } class PhilosopherOK extends Proc { private int steps; private Semaphore A; private Semaphore B; public PhilosopherOK (String id, int steps, Semaphore A, Semaphore B) { super (id); this.steps = steps; this.A = A; this.B = B; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { A.Wait (); B.Wait (); JOS.out.println (getName () + " eating at step " + steps); steps--; A.Signal (); B.Signal (); } Stop (); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: ProducerConsumer.java 1. problem : two processes become deadlocked using semaphores 2. test : java ProducerConsumerOK 3. test : java ProducerConsumerBAD 4. fix : ConsumerBAD so the processes do not get deadlocked 5. compile : javac ProducerConsumerBAD.java 6. test : java ProducerConsumerBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Semaphore; import JOS.Buffer; import JOS.Counter; class ProducerConsumerBAD { public static void main (String args []) { JOS.useOSmenu = false; new ProducerConsumerBAD (); } public ProducerConsumerBAD () { Semaphore not_empty = new Semaphore ("P/CnotEmpty", 0); Semaphore not_full = new Semaphore("P/CnotFull", 5); Buffer aBuf = new Buffer ("P/CBuffer", 5); new ProducerOK ("ProducerOK #1", JOS.steps, not_empty, not_full, aBuf).Start (); new ConsumerBAD ("ConsumerBAD #1", JOS.steps, not_empty, not_full, aBuf).Start (); } } class ProducerOK extends Proc { private int steps; private Semaphore not_empty; private Semaphore not_full; private Buffer aBuf; public ProducerOK (String id, int steps, Semaphore not_empty, Semaphore not_full, Buffer aBuf) { super (id); this.steps = steps; this.not_empty = not_empty; this.not_full = not_full; this.aBuf = aBuf; } public void run () { Counter i = new Counter (0); JOS.out.println(getName () + " Running"); while (steps > 0) { not_full.Wait (); aBuf.put (i); JOS.out.println (getName () + " puts " + i.getCount () + " at step " + steps--); i = new Counter (i.getCount ()); i.incrCount (); not_empty.Signal (); } Stop (); } } class ConsumerBAD extends Proc { private int steps; private Semaphore not_empty; private Semaphore not_full; private Buffer aBuf; public ConsumerBAD (String id, int steps, Semaphore not_empty, Semaphore not_full, Buffer aBuf) { super (id); this.steps = steps; this.not_empty = not_empty; this.not_full = not_full; this.aBuf = aBuf; } public void run () { Counter i; JOS.out.println (getName () + " Running"); while (steps > 0) { not_empty.Wait (); i = (Counter) aBuf.get (); JOS.out.println (getName () + " gets " + i.getCount() + " at step " + steps); steps--; not_full.Signal (); } Stop (); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: ReadersWriterBAD.java 1. problem : readers and write operate at the same time 2. test : java ReadersWriterOK 3. test : java ReadersWriterBAD 4. fix : WriterBAD so the writer does not operate at same time 5. compile : javac ReadersWriterBAD.java 6. test : java ReadersWriterBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Semaphore; import JOS.Counter; class ReadersWriterBAD { public static void main (String args []) { JOS.useOSmenu = false; new ReadersWriterBAD (); } public ReadersWriterBAD () { Semaphore writer = new Semaphore ("Writer", 1); Semaphore semReadCount = new Semaphore ("ReadCount", 1); Counter readCount = new Counter (0); new ReaderOK ("ReaderOK #1", JOS.steps, writer, semReadCount, readCount).Start (); new ReaderOK ("ReaderOK #2", JOS.steps, writer, semReadCount, readCount).Start (); new WriterBAD ("WriterBAD #1", JOS.steps, writer).Start (); } } class ReaderOK extends Proc { private int steps; private Semaphore writer; private Semaphore semReadCount; private Counter readCount; public ReaderOK (String id, int steps, Semaphore writer, Semaphore semReadCount, Counter readCount) { super (id); this.steps = steps; this.writer = writer; this.semReadCount= semReadCount; this.readCount = readCount; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { JOS.out.println (getName () + " tests reading at step " + steps); semReadCount.Wait (); readCount.incrCount (); if (readCount.isEqual (1)) { writer.Wait (); } semReadCount.Signal(); JOS.out.println (getName ()+ " begins reading at step " + steps); JOS.out.println (getName ()+ " ends reading at step " + steps); semReadCount.Wait (); readCount.decrCount (); if (readCount.isEqual (0)) { writer.Signal (); } semReadCount.Signal (); steps--; } Stop (); } } class WriterBAD extends Proc { private int steps; private Semaphore writer; public WriterBAD (String id, int steps, Semaphore writer) { super (id); this.steps = steps; this.writer = writer; } public void run () { JOS.out.println(getName () + " Running STUDENT VERSION"); while (steps > 0) { writer.Wait (); JOS.out.println (getName () + " begins writing at step " + steps); JOS.out.println (getName () + " ends writing at step " + steps); steps--; writer.Signal (); } Stop(); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: RingBAD.java 1. problem : processes on a ring are suspended waiting for messages 2. test : java RingOK 3. test : java RingBAD 4. fix : complete the code in NodeBAD to send along the message 5. compile : javac RingBAD.java 6. test : java RingBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Port; import JOS.Message; import JOS.NetGraph; class RingBAD { public static final int RING_SIZE = 4; public static void main (String args []) { JOS.useOSmenu = false; new RingBAD (); } public RingBAD () { NetGraph ng = new NetGraph (); Port neighbor; for (int i = 1; i <= RING_SIZE; i++) { if (i < RING_SIZE) { neighbor = ng.ports [i+1]; } else { neighbor = ng.ports [1]; } if (ng.isLocal (i)) { new NodeBAD ("RingNodeBAD # " + i, ng.ports [i], neighbor).Start (); } } Message msg = new Message (new String (NetGraph.encode ("", 0))); ng.ports [2].Send (msg); } } class NodeBAD extends Proc { private Port myPort; private Port neighbor; public NodeBAD (String id, Port myPort, Port neighbor) { super (id); this.myPort = myPort; this.neighbor = neighbor; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); Message msg = myPort.Receive (); String s = msg.getValue (); int c = NetGraph.decode (s, 0); JOS.out.println (getName () + " recvs " + c); c++; /* complete the code here */ if (c < RingBAD.RING_SIZE) { msg = new Message (new String (NetGraph.encode ("", c))); neighbor.Send (msg); } else if (c == RingBAD.RING_SIZE) { JOS.out.println ("Message finished at " + getName ()); } Stop(); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: SmokersBAD.java 1. problem : four processes become deadlocked using semaphores 2. test : java SmokersOK 3. test : java SmokersBAD 4. fix : SmokerBAD so the processes do not get deadlocked 5. compile : javac SmokersBAD.java 6. test : java SmokersBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Semaphore; class SmokersBAD { public static void main (String args []) { JOS.useOSmenu = false; new SmokersBAD (); } public SmokersBAD () { Semaphore tobacco = new Semaphore ("Tobacco", 0); Semaphore paper = new Semaphore ("Paper", 0); Semaphore matches = new Semaphore ("Matches", 0); new SmokerBAD ("SmokerBAD #1", JOS.steps, tobacco).Start (); new SmokerBAD ("SmokerBAD #2", JOS.steps, paper).Start (); new SmokerBAD ("SmokerBAD #3", JOS.steps, matches).Start (); new AgentOK ("AgentOK #1", JOS.steps, tobacco, paper, matches).Start (); } } class SmokerBAD extends Proc { private int steps; private Semaphore need; public SmokerBAD (String id, int steps, Semaphore need) { super (id); this.steps = steps; this.need = need; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { need.Wait (); JOS.out.println (getName () + " smokes at step " + steps); steps--; need.Signal (); } Stop (); } } class AgentOK extends Proc { private int steps; private Semaphore tobacco; private Semaphore paper; private Semaphore matches; public AgentOK (String id, int steps, Semaphore tobacco, Semaphore paper, Semaphore matches) { super (id); this.steps = steps; this.tobacco = tobacco; this.paper = paper; this.matches = matches; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); while (steps > 0) { tobacco.Signal (); tobacco.Wait (); paper.Signal (); paper.Wait (); matches.Signal (); matches.Wait (); steps--; } Stop (); } } /* Copyright (c) Edward A. Billard, 1997 SOURCE: StarBAD.java 1. problem : processes on rim are suspended waiting for messages from center 2. test : java StarOK 3. test : java StarBAd 4. fix : CenterBAD to send messages to rim processes which have not received the message 5. compile : javac StarBAD.java 6. test : java StarBAD Student Name: Anthony F. Ortiz Student ID : xxx-xx-2871 Email : aortiz1@haywire.csuhayward.edu */ import JOS.JOS; import JOS.Proc; import JOS.Port; import JOS.Message; class StarBAD { public static final int STAR_SIZE = 8; public static void main (String args []) { JOS.useOSmenu = false; new StarBAD (); } public StarBAD () { int i; Port ports [] = new Port [STAR_SIZE + 1]; Message msg = new Message ("Hello From The Outside World"); ports [1] = new Port ("CenterPort1"); for (i = 2; i <= STAR_SIZE; i++) { ports [i] = new Port ("RimPort"+i); } new CenterBAD ("CenterBAD #1", ports, STAR_SIZE).Start (); for (i = 2; i <= STAR_SIZE; i++) { new RimOK ("RimOK #" + i, ports[i], ports[1], STAR_SIZE).Start (); } ports [5].Send (msg); } } class RimOK extends Proc { private Port myPort; private Port center; private int star_size; public RimOK (String id, Port myPort, Port center, int star_size) { super (id); this.myPort = myPort; this.center = center; this.star_size = star_size; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); Message msg = myPort.Receive (); Port sendPort = msg.getSenderPort (); JOS.out.println (getName ()+ " recvs " + msg.getValue ()); if (!center.equals (sendPort)) { msg.setSenderPort (myPort); center.Send (msg); } Stop (); } } class CenterBAD extends Proc { private Port ports []; private int star_size; public CenterBAD (String id, Port ports [], int star_size) { super (id); this.ports = ports; this.star_size = star_size; } public void run () { JOS.out.println (getName () + " Running STUDENT VERSION"); Message msg = ports [1].Receive (); Port sendPort = msg.getSenderPort (); JOS.out.println (getName () + " recvs " + msg.getValue ()); /* complete the code here */ for (int j = 2; j <= star_size; j++) { if (!ports [j].equals (sendPort)) { msg = new Message ("Hello From the Center"); ports [j].Send (msg); } } Stop(); } } /* This is the applet version of JOS. */ import java.util.*; import java.awt.*; import java.io.*; import JOS.JOS; import java.applet.*; import JOS.Butler; import JOS.BufPool; import JOS.Buffer; import JOS.CanvasGUI; import JOS.CanvasProcs; import JOS.CheckPanel; import JOS.Counter; import JOS.Debug; import JOS.DiskLower; import JOS.DiskUpper; import JOS.DiskReq; import JOS.Dispatcher; import JOS.InGUI; import JOS.InConsumer; import JOS.InProducer; import JOS.Message; import JOS.NetGraph; import JOS.Null; import JOS.OutConsumer; import JOS.OutProducer; import JOS.OutGUI; import JOS.Port; import JOS.Priority; import JOS.Proc; import JOS.Queue; import JOS.Scheduler; import JOS.Semaphore; import JOS.Transit; public class OSmenu extends Applet { private OSframe f; public static void main (String args []) { } public void init () { System.out.println ("INIT"); f = new OSframe (true); f.resize (600, 60); f.show (); } } class OSframe extends Frame { private boolean isApplet = true; private CheckboxMenuItem prio; private CheckboxMenuItem fifo; private CheckboxMenuItem sjf; public OSframe (boolean isApplet) { this.isApplet = isApplet; JOS.useOSmenu = true; MenuBar mb = new MenuBar (); setTitle ("Operating Systems Classical Problems"); Menu mOK = new Menu ("EXECUTABLES (OK)"); mOK.add (new MenuItem ("1) DiningPhilosophersOK.class")); mOK.add (new MenuItem ("2) ProducerConsumerOK.class")); mOK.add (new MenuItem ("3) ReadersWriterOK.class")); mOK.add (new MenuItem ("4) SmokersOK.class")); mOK.add (new MenuItem ("5) DeadlockOK.class")); mOK.add (new MenuItem ("6) ClientServerOK.class")); mOK.add (new MenuItem ("7) RingOK.class")); mOK.add (new MenuItem ("8) StarOK.class")); mOK.add (new MenuItem ("9) BusyBeesOK.class")); mOK.add (new MenuItem ("10) DiskTestOK.class")); mOK.add (new MenuItem ("Exit")); mb.add (mOK); Menu mBAD = new Menu ("EXECUTABLES (BAD)"); mBAD.add (new MenuItem ("1) DiningPhilosophersBAD.class")); mBAD.add (new MenuItem ("2) ProducerConsumerBAD.class")); mBAD.add (new MenuItem ("3) ReadersWriterBAD.class")); mBAD.add (new MenuItem ("4) SmokersBAD.class")); mBAD.add (new MenuItem ("5) DeadlockBAD.class")); mBAD.add (new MenuItem ("6) ClientServerBAD.class")); mBAD.add (new MenuItem ("7) RingBAD.class")); mBAD.add (new MenuItem ("8) StarBAD.class")); mBAD.add (new MenuItem ("9) BusyBeesBAD.class")); mBAD.add (new MenuItem ("10) Butler Display")); mBAD.add (new MenuItem ("Exit")); mb.add (mBAD); Menu msBAD = new Menu ("SOURCES (BAD)"); msBAD.add (new MenuItem ("1) DiningPhilosophersBAD.java")); msBAD.add (new MenuItem ("2) ProducerConsumerBAD.java")); msBAD.add (new MenuItem ("3) ReadersWriterBAD.java")); msBAD.add (new MenuItem ("4) SmokersBAD.java")); msBAD.add (new MenuItem ("5) DeadlockBAD.java")); msBAD.add (new MenuItem ("6) ClientServerBAD.java")); msBAD.add (new MenuItem ("7) RingBAD.java")); msBAD.add (new MenuItem ("8) StarBAD.java")); msBAD.add (new MenuItem ("9) BusyBeesBAD.java")); msBAD.add (new MenuItem ("Exit")); mb.add (msBAD); Menu mSTUB = new Menu ("SOURCES (JOS)"); mSTUB.add (new MenuItem ("1) Buffer.java")); mSTUB.add (new MenuItem ("2) BufPool.java")); mSTUB.add (new MenuItem ("3) CanvasGUI.java")); mSTUB.add (new MenuItem ("4) CanvasProcs.java")); mSTUB.add (new MenuItem ("5) CheckPanel.java")); mSTUB.add (new MenuItem ("6) Counter.java")); mSTUB.add (new MenuItem ("7) Debug.java")); mSTUB.add (new MenuItem ("8) DiskLower.java")); mSTUB.add (new MenuItem ("9) DiskReq.java")); mSTUB.add (new MenuItem ("10) DiskUpper.java")); mSTUB.add (new MenuItem ("11) Dispatcher.java")); mSTUB.add (new MenuItem ("12) InConsumer.java")); mSTUB.add (new MenuItem ("13) InGUI.java")); mSTUB.add (new MenuItem ("14) InProducer.java")); mSTUB.add (new MenuItem ("15) JOS.java")); mSTUB.add (new MenuItem ("16) Message.java")); mSTUB.add (new MenuItem ("17) Null.java")); mSTUB.add (new MenuItem ("18) OutConsumer.java")); mSTUB.add (new MenuItem ("19) OutGUI.java")); mSTUB.add (new MenuItem ("20) OutProducer.java")); mSTUB.add (new MenuItem ("21) Port.java")); mSTUB.add (new MenuItem ("22) Priority.java")); mSTUB.add (new MenuItem ("23) Proc.java")); mSTUB.add (new MenuItem ("24) Queue.java")); mSTUB.add (new MenuItem ("25) Scheduler.java")); mSTUB.add (new MenuItem ("26) Semaphore.java")); mSTUB.add (new MenuItem ("27) Transit.java")); mSTUB.add (new MenuItem ("Exit")); mb.add(mSTUB); Menu msch = new Menu ("SCHEDULING"); prio = new CheckboxMenuItem ("PRIO"); msch.add (prio); fifo = new CheckboxMenuItem ("FIFO"); msch.add (fifo); sjf = new CheckboxMenuItem ("SJF"); msch.add (sjf); msch.add (new MenuItem ("-")); msch.add (new MenuItem ("1) SchedulingOK.class")); msch.add (new MenuItem ("Exit")); mb.add (msch); setMenuBar (mb); } public boolean action (Event event, Object arg) { String l; if (event.target instanceof CheckboxMenuItem) { prio.setState (false); fifo.setState (false); sjf.setState (false); if (event.target.equals (prio)) { prio.setState (true); JOS.schedulingPolicy = JOS.PRIORITY_POLICY; } else if (event.target.equals (fifo)) { fifo.setState (true); JOS.schedulingPolicy = JOS.FIFO_POLICY; } else if (event.target.equals (sjf)) { sjf.setState (true); JOS.schedulingPolicy = JOS.SJF_POLICY; } } else if (event.target instanceof MenuItem) { l = (String) event.arg; if (l.equals ("1) SchedulingOK.class")) new SchedulingOK (JOS.schedulingPolicy); else if (l.equals ("1) DiningPhilosophersOK.class")) new DiningPhilosophersBAD (); else if (l.equals ("1) DiningPhilosophersBAD.class")) new DiningPhilosophersBAD (); else if (l.equals ("2) ProducerConsumerOK.class")) new ProducerConsumerBAD (); else if (l.equals ("2) ProducerConsumerBAD.class")) new ProducerConsumerBAD (); else if (l.equals ("3) ReadersWriterOK.class")) new ReadersWriterBAD (); else if (l.equals ("3) ReadersWriterBAD.class")) new ReadersWriterBAD (); else if (l.equals ("4) SmokersOK.class")) new SmokersBAD (); else if (l.equals ("4) SmokersBAD.class")) new SmokersBAD (); else if (l.equals ("5) DeadlockOK.class")) new DeadlockBAD (); else if (l.equals ("5) DeadlockBAD.class")) new DeadlockBAD (); else if (l.equals ("6) ClientServerOK.class")) new ClientServerBAD (); else if (l.equals ("6) ClientServerBAD.class")) new ClientServerBAD (); else if (l.equals ("7) RingOK.class")) new RingBAD (); else if (l.equals ("7) RingBAD.class")) new RingBAD (); else if (l.equals ("8) StarOK.class")) new StarBAD (); else if (l.equals ("8) StarBAD.class")) new StarBAD (); else if (l.equals ("9) BusyBeesOK.class")) new BusyBeesBAD (); else if (l.equals ("9) BusyBeesBAD.class")) new BusyBeesBAD (); else if (l.equals ("10) DiskTestOK.class")) new DiskTestOK (); else if (l.equals ("10) Butler Display")) Butler.display (); else if (l.equals ("1) DiningPhilosophersBAD.java")) DisplayFile ("DiningPhilosophersBAD.java"); else if (l.equals ("2) ProducerConsumerBAD.java")) DisplayFile ("ProducerConsumerBAD.java"); else if (l.equals ("3) ReadersWriterBAD.java")) DisplayFile ("ReadersWriterBAD.java"); else if (l.equals ("4) SmokersBAD.java")) DisplayFile ("SmokersBAD.java"); else if (l.equals ("5) DeadlockBAD.java")) DisplayFile ("DeadlockBAD.java"); else if (l.equals ("6) ClientServerBAD.java")) DisplayFile ("ClientServerBAD.java"); else if (l.equals ("7) RingBAD.java")) DisplayFile ("RingBAD.java"); else if (l.equals ("8) StarBAD.java")) DisplayFile ("StarBAD.java"); else if (l.equals ("9) BusyBeesBAD.java")) DisplayFile ("BusyBeesBAD.java"); else if (l.equals ("1) Buffer.java")) DisplayFile ("JOS/Buffer.java"); else if (l.equals ("2) BufPool.java")) DisplayFile ("JOS/BufPool.java"); else if (l.equals ("3) CanvasGUI.java")) DisplayFile ("JOS/CanvasGUI.java"); else if (l.equals ("4) CanvasProcs.java")) DisplayFile ("JOS/CanvasProcs.java"); else if (l.equals ("5) CheckPanel.java")) DisplayFile ("JOS/CheckPanel.java"); else if (l.equals ("6) Counter.java")) DisplayFile ("JOS/Counter.java"); else if (l.equals ("7) Debug.java")) DisplayFile ("JOS/Debug.java"); else if (l.equals ("8) DiskLower.java")) DisplayFile ("JOS/DiskLower.java"); else if (l.equals ("9) DiskReq.java")) DisplayFile ("JOS/DiskReq.java"); else if (l.equals ("10) DiskUpper.java")) DisplayFile ("JOS/DiskUpper.java"); else if (l.equals ("11) Dispatcher.java")) DisplayFile ("JOS/Dispatcher.java"); else if (l.equals ("12) InConsumer.java")) DisplayFile ("JOS/InConsumer.java"); else if (l.equals ("13) InGUI.java")) DisplayFile ("JOS/InGUI.java"); else if (l.equals ("14) InProducer.java")) DisplayFile ("JOS/InProducer.java"); else if (l.equals ("15) JOS.java")) DisplayFile ("JOS/JOS.java"); else if (l.equals ("16) Message.java")) DisplayFile ("JOS/Message.java"); else if (l.equals ("17) Null.java")) DisplayFile ("JOS/Null.java"); else if (l.equals ("18) OutConsumer.java")) DisplayFile ("JOS/OutConsumer.java"); else if (l.equals ("19) OutGUI.java")) DisplayFile ("JOS/OutGUI.java"); else if (l.equals ("20) OutProducer.java")) DisplayFile ("JOS/OutProducer.java"); else if (l.equals ("21) Port.java")) DisplayFile ("JOS/Port.java"); else if (l.equals ("22) Priority.java")) DisplayFile ("JOS/Priority.java"); else if (l.equals ("23) Proc.java")) DisplayFile ("JOS/Proc.java"); else if (l.equals ("24) Queue.java")) DisplayFile ("JOS/Queue.java"); else if (l.equals ("25) Scheduler.java")) DisplayFile ("JOS/Scheduler.java"); else if (l.equals ("26) Semaphore.java")) DisplayFile ("JOS/Semaphore.java"); else if (l.equals ("27) Transit.java")) DisplayFile ("JOS/Transit.java"); else if (l.equals ("Exit")) exiter (); } return true; } private void DisplayFile (String filename) { String str = ReadFile (filename); Frame frame = new ShowFrame (filename, str); frame.resize (100, 200); frame.pack (); frame.show (); } private String ReadFile (String filename) { int begin = 0; int end = 10000; char c [] = new char [end]; String s = ""; try { BufferedReader r = new BufferedReader (new FileReader (filename)); r.read (c, begin, end); s = new String (c); r.close (); } catch (FileNotFoundException e) { } catch (IOException e) { } return s; } private void exiter () { if (isApplet) { this.dispose (); } else { } System.exit (0); } public boolean handleEvent (Event event) { if (event.id == Event.WINDOW_DESTROY) { exiter(); } else if (event.id == Event.WINDOW_ICONIFY) { exiter (); } return super.handleEvent (event); } } class ShowFrame extends Frame { public ShowFrame (String filename, String s) { setTitle (filename); TextArea textArea = new TextArea (s, 30, 70); textArea.setEditable (false); setLayout(new BorderLayout ()); add ("Center", textArea); } public boolean handleEvent (Event event) { if (event.id == Event.WINDOW_DESTROY) { dispose (); return true; } return super.handleEvent (event); } } /* This is the application version of JOS. */ import JOS.JOS; import JOS.Butler; import JOS.BufPool; import JOS.Buffer; import JOS.CanvasGUI; import JOS.CanvasProcs; import JOS.CheckPanel; import JOS.Counter; import JOS.Debug; import JOS.DiskLower; import JOS.DiskUpper; import JOS.DiskReq; import JOS.Dispatcher; import JOS.InGUI; import JOS.InConsumer; import JOS.InProducer; import JOS.Message; import JOS.NetGraph; import JOS.Null; import JOS.OutConsumer; import JOS.OutProducer; import JOS.OutGUI; import JOS.Port; import JOS.Priority; import JOS.Proc; import JOS.Queue; import JOS.Scheduler; import JOS.Semaphore; import JOS.Transit; import java.io.*; class OSmenu1 { public static void main (String args []) { DataInputStream in = new DataInputStream (System.in); topMenu (in); } static int getInt (DataInputStream in) { int i = -1; String s = "-1"; System.out.print ("Select: "); System.out.flush (); try { s = in.readLine (); } catch (IOException e) { System.out.println ("Bad String"); } try { i = Integer.parseInt (s); } catch (NumberFormatException e) { System.out.println ("Enter Integer"); } return i; } static void pause () { try { synchronized (JOS.cntrl) { JOS.cntrl.wait (); } } catch (InterruptedException e) { } } static void topMenu (DataInputStream in) { int i; do { System.out.println ("Classical Problems in Operating Systems"); System.out.println ("0) Exit"); System.out.println ("1) EXECUTABLES (OK)"); System.out.println ("2) EXECUTABLES (BAD)"); System.out.println ("3) SCHEDULING"); i = getInt (in); switch (i) { case 1 : OKmenu (in); break; case 2 : BADmenu (in); break; case 3 : SCHmenu (in); break; } } while (i != 0); System.exit (0); } static void OKmenu (DataInputStream in) { int i; do { System.out.println ("EXECUTABLES (OK)"); System.out.println (" 0) Return to Main Menu"); System.out.println (" 1) DiningPhilosophersOK.class"); System.out.println (" 2) ProducerConsumerOK.class"); System.out.println (" 3) ReadersWriterOK.class"); System.out.println (" 4) SmokersOK.class"); System.out.println (" 5) DeadlockOK.class"); System.out.println (" 6) ClientServerOK.class"); System.out.println (" 7) RingOK.class"); System.out.println (" 8) StarOK.class"); System.out.println (" 9) BusyBeesOK.class"); i = getInt (in); switch (i) { case 1 : new DiningPhilosophersBAD (); pause (); break; case 2 : new ProducerConsumerBAD (); pause(); break; case 3 : new ReadersWriterBAD (); pause(); break; case 4 : new SmokersBAD (); pause (); break; case 5 : new DeadlockBAD (); pause(); break; case 6 : new ClientServerBAD (); pause(); break; case 7 : new RingBAD (); pause(); break; case 8 : new StarBAD (); pause (); break; case 9 : new BusyBeesBAD (); pause (); break; } } while (i != 0); } static void BADmenu (DataInputStream in) { int i; do { System.out.println ("EXECUTABLES (BAD) WARNING: MOST WILL HANG"); System.out.println (" 0) Return to Main Menu"); System.out.println (" 1) DiningPhilosophersBAD.class"); System.out.println (" 2) ProducerConsumerBAD.class"); System.out.println (" 3) ReadersWriterBAD.class"); System.out.println (" 4) SmokersBAD.class"); System.out.println (" 5) DeadlockBAD.class"); System.out.println (" 6) ClientServerBAD.class"); System.out.println (" 7) RingBAD.class"); System.out.println (" 8) StarBAD.class"); System.out.println (" 9) BusyBeesBAD.class"); System.out.println (" 10) Butler Display"); i = getInt (in); switch (i) { case 1 : new DiningPhilosophersBAD (); pause (); break; case 2 : new ProducerConsumerBAD (); pause (); break; case 3 : new ReadersWriterBAD (); pause (); break; case 4 : new SmokersBAD (); pause (); break; case 5 : new DeadlockBAD (); pause (); break; case 6 : new ClientServerBAD (); pause (); break; case 7 : new RingBAD (); pause (); break; case 8 : new StarBAD (); pause (); break; case 9 : new BusyBeesBAD (); pause (); break; case 10: Butler.display (); break; } } while (i != 0); } static void SCHmenu (DataInputStream in) { int i; do { System.out.println ("SCHEDULING (BAD)"); System.out.println (" 0) Return to Main Menu"); System.out.println (" 1) Set to PRIO"); System.out.println (" 2) Set to FIFO"); System.out.println (" 3) Set to SJF"); System.out.println (" 4) SchedulingOK.class"); i = getInt (in); switch (i) { case 1 : JOS.schedulingPolicy = JOS.PRIORITY_POLICY; break; case 2 : JOS.schedulingPolicy = JOS.FIFO_POLICY; break; case 3 : JOS.schedulingPolicy = JOS.SJF_POLICY; break; case 4 : new SchedulingOK(JOS.schedulingPolicy); pause (); break; } } while (i != 0); } } /* Test BufPool, DiskUpper, DiskLower, and DiskReq. */ import JOS.JOS; import JOS.Proc; import JOS.BufPool; import java.util.Random; class DiskTestOK { public static void main (String args []) { JOS.useOSmenu = false; new DiskTestOK (); } public DiskTestOK () { new DiskTestProc ("DiskTestOK #1", JOS.steps).Start (); } } class DiskTestProc extends Proc { private int steps; public DiskTestProc (String id, int steps) { super (id); this.steps = steps; } private void fillBuf (int block, byte [] buf) { String s = new String ("BLOCK #" + String.valueOf (block) + "\n"); s.getBytes(0, s.length (), buf, 0); } public void run () { int i, block; Random r = new Random (); byte[] buf = new byte [BufPool.BUFSIZE]; JOS.out.println (getName () + " Running"); i = steps; r.setSeed (1); while (i > 0) { //LEAVE AS A COMMENT: JOS.out.println (getName () + " at step " + i); block = Math.abs (r.nextInt ()) % JOS.DISKSIZE; fillBuf (block, buf); JOS.disk.write(block, buf); i--; } JOS.out.println(getName () + " wasting some time"); JOS.out.println(getName () + " wasting more time"); i = steps; while (i > 0) { //LEAVE AS A COMMENT: JOS.out.println (getName () + " at step " + i); block = Math.abs (r.nextInt ()) % JOS.DISKSIZE; fillBuf (block, buf); JOS.disk.write (block, buf); i--; } i = steps; r.setSeed (1); while (i > 0) { //LEAVE AS A COMMENT: JOS.out.println (getName () + " at step " + i); block = Math.abs (r.nextInt ()) % JOS.DISKSIZE; fillBuf (block, buf); JOS.disk.read(block, buf); i--; } Stop (); } } /* Tests the scheduling algorithms, PRIO, FIFO, and SJF. */ import JOS.JOS; import JOS.Proc; class SchedulingOK { public static void main (String args []) { int policy = JOS.PRIORITY_POLICY; String s; if (args.length > 0) { s = args [0]; if (s.equals ("FIFO")) { policy = JOS.FIFO_POLICY; } else if (s.equals ("SJF")) { policy = JOS.SJF_POLICY; } } JOS.useOSmenu = false; new SchedulingOK (policy); } public SchedulingOK (int policy) { JOS.schedulingPolicy = policy; new SchedulingExampleOK ("MedPrio/LongBurst", JOS.steps, 50, 1000).Start (); new SchedulingExampleOK("HighPrio/MedBurst", JOS.steps, 100, 500).Start (); new SchedulingExampleOK("LowPrio/ShortBurst", JOS.steps, 5, 1).Start (); } } class SchedulingExampleOK extends Proc { private int steps; private int burst; public SchedulingExampleOK (String id, int steps, int priority, int burst) { super(id, priority); this.steps = steps; this.burst = burst; } public void run () { JOS.out.println (getName () + " Running"); while (steps > 0) { try { sleep (burst); } catch (Exception e) { } JOS.out.println(getName () + " printing/yielding"); steps--; } Stop (); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Buffer.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Buffer.java 4. test : java ProducerConsumerOK */ package JOS; import java.util.*; import JOS.Semaphore; /* Semaphore : constructor: Semaphore (String name, int count) methods : Wait (), Signal () */ public class Buffer { private Semaphore semaphore; private Vector array; private String name; // create a buffer of specified size public Buffer (String name, int size) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " Buffer Constructor STUDENT VERSION"); this.name = name; this.semaphore = new Semaphore ("BufferSemaphore", 1); this.array = new Vector (size); } // use a semaphore to synchronize the access public void put (Object obj) { semaphore.Wait (); array.addElement (obj); semaphore.Signal (); } // use a semaphore to synchronize the access public Object get () { semaphore.Wait (); Object obj = array.firstElement (); array.removeElementAt (0); semaphore.Signal (); return obj; } } /* Test BufPool, DiskUpper, DiskLower, DiskReq together. */ package JOS; import JOS.JOS; import JOS.Queue; import JOS.Semaphore; // Maintain a buffer pool of free buffers for DiskUpper to allocate // for asynchronous writes. // Since there are only a finite number of free buffers, // requests for buffers may be delayed. // Important: who gives the buffer back and under what condition? public class BufPool { public final static int BUFSIZE = 512; private Semaphore bufSemaphore = new Semaphore ("BufPoolSemaphore", 1); private Queue bufPool; private String name; // make a pool with JOS.NUMBUFS new byte buffers, each of size BUFSIZE public BufPool (String name) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " BufPool Constructor"); this.name = name; this.bufPool = new Queue ("BufPoolQueue", Queue.FIFO); for (int i = 0; i < JOS.NUMBUFS; i++) { putBlockBuf (new byte [BUFSIZE]); } } // get a free buffer from the pool public byte [] getBlockBuf () { bufSemaphore.Wait (); byte [] block = (byte []) bufPool.dequeue (); return block; } // put a buffer back onto the pool public void putBlockBuf (byte [] buf) { bufSemaphore.Signal (); bufPool.enqueue (buf); } // copy the contents of buf to buf1 public static void copyBlockBuf (byte [] buf, byte [] buf1) { for (int i = 0; i < BUFSIZE; i++) { buf1 [i] = buf [i]; } } } /* CS 6560 Operating Systems Design: The Butler -------------------------------------------- The Butler is usually a process in the operating system that responds to messages to do general housekeeping: kill the system print a process table snapshot print a tty structure snapshot print a disk snapshot For example, on Unix the ps command (not necessarily a Butler) displays: gold% ps -ca PID CLS PRI TTY TIME CMD 8988 TS 58 console 0:00 ttsessio 8977 TS 48 console 0:00 xinit 8983 TS 58 console 0:00 xterm 8978 TS 58 console 3:34 Xsun 9008 TS 56 ttyp2 0:00 sh 8979 TS 0 console 0:00 sh 8999 TS 25 ttyp2 6:01 netscape 8984 TS 58 console 0:03 mailtool 9565 TS 48 ttyp2 0:00 sh 8980 TS 59 console 0:00 xclock 8985 TS 58 console 0:01 xterm 9017 TS 55 ttyp2 0:00 sh 8986 TS 58 console 0:02 twm 9585 TS 38 ttyp0 0:00 ps In JOS, we don't have a Butler process, instead we have a static display method in the Butler class (not message based). In our case, we would like to see: PROCESS NAME, PRIORITY, CPU USAGE Who has this information? How can they provide it to the Butler so it can display upon request? When does the information about a process change? Do processes leave? What data structures will you need? When and how are they updated? Depending on your time, you might want to do this in steps. The Butler should use System.out.println() To Test: Run BusyBeesOK on slow and then select Butler from the "BAD" menu. */ package JOS; import JOS.JOS; import JOS.Queue; import java.util.Vector; public class Butler { private static Vector names; public Butler () { display (); } public static void display () { System.out.println ("PID Name"); names = CanvasProcs.getProcs (); for (int i = 0; i < names.size (); i++) { System.out.println ((String) names.elementAt (i)); } } } /* Copyright (c) Edward A. Billard, 1997 STUB: CanvasGUI.stub (move to .java) 0. rename : move Port.stub Port.java 1. declare : private variables 2. implement: public methods 3. compile : javac CanvasGUI.java 4. test : java OSmenu */ package JOS; import java.awt.*; import java.util.Vector; public class CanvasGUI extends Canvas { private Graphics g = null; // draw all of the state ovals and transition lines // display all of the current dots in the appropriate state and color public void paint (Graphics g) { g.drawRect (40, 10, 80, 140); g.drawRect (120, 10, 80, 140); g.drawRect (200, 10, 80, 140); g.drawRect (280, 10, 80, 140); g.drawRect (360, 10, 80, 140); g.drawRect (440, 10, 80, 140); g.drawRect (520, 10, 80, 140); g.drawString ("New", 50, 75); g.drawString ("Dead", 130, 75); g.drawString ("Suspended", 210, 75); g.drawString ("Ready", 290, 75); g.drawString ("Current", 370, 75); g.drawString ("Sending", 450, 75); g.drawString ("Receiving", 530, 75); //appropriate state and color! } // make a dot of the specified color leave state 1 and move to state s2 public void transition (Color c, int s1, int s2) { if (g == null) { g = this.getGraphics (); } if (s1 == Proc.READY_BUT_NOT_STARTED) { s1 = Proc.READY; } if (s2 == Proc.READY_BUT_NOT_STARTED) { s2 = Proc.READY; } //move from state1 to state2! } } /* Copyright (c) Edward A. Billard, 1997 STUB: CanvasProcs.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac CanvasProcs.java 4. test : java OSmenu */ package JOS; import java.awt.*; import java.util.Vector; public class CanvasProcs extends Canvas { private static Vector names = new Vector (); private static Vector colors = new Vector (); // maintain Vectors for names and colors // display rows and columns of colored dots and associated names public void paint (Graphics g) { for (int i = 0; i < names.size (); i++) { g.setColor ((Color) colors.elementAt (i)); g.drawRect (10, 10 * (i + 1), 1, 1); g.drawString ((String) names.elementAt (i), 20, 10 * (i + 1)); } } // add name and color to appropriate Vectors and then paint public void registerProc (String name, Color c) { names.addElement (name); colors.addElement (c); paint (this.getGraphics ()); } public static Vector getProcs () { return names; } } /* Copyright (c) Edward A. Billard, 1997 STUB: CheckPanel.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac CheckPanel.java 4. test : java OSmenu */ package JOS; import java.awt.*; public class CheckPanel extends Panel { private static final int STOP = 0; private static final int SLOW = 1; private static final int FAST = 2; private int preference = SLOW; private Label title; private CheckboxGroup speed; private Checkbox [] cboxes = new Checkbox [3]; // maintain three checkboxes (for STOP, SLOW, FAST) // add these to ``this'' panel // maintain a recent preference between SLOW and FAST public CheckPanel () { title = new Label ("Speed"); title.reshape (10, 10, 100, 25); add (title); speed = new CheckboxGroup (); cboxes [STOP] = new Checkbox ("Stop", speed, false); cboxes [STOP].reshape (20, 35, 100, 25); add (cboxes [STOP]); cboxes [SLOW] = new Checkbox ("Slow", speed, true); cboxes [SLOW].reshape (20, 60, 100, 25); add (cboxes [SLOW]); cboxes [FAST] = new Checkbox ("Fast", speed, false); cboxes [FAST].reshape (20, 85, 100, 25); add (cboxes [FAST]); } // (cntrlFlag == true => Procs wait for a notify) // notify using JOS's cntrl object // then clear the flag private void notifyProcs () { if (JOS.cntrlFlag == true) { synchronized (JOS.cntrl) { JOS.cntrl.notify (); JOS.cntrlFlag = false; } } } // set the state of the boxes so that its the preference for SLOW or FAST // (new Procs cannot be Started in the STOP mode) // then notifyProcs public void resetBoxes () { cboxes [preference].setState (true); notifyProcs (); } public boolean handleEvent (Event event) { if (event.target == cboxes [STOP]) { // this is what makes the Procs wait when // the STOP box is clicked cboxes [STOP].setState (true); JOS.cntrlFlag = true; } else if (event.target == cboxes [SLOW]) { // set JOS's slowness and dotslowness // set the preference JOS.slowness = JOS.maxSlowness; JOS.dotSlowness = JOS.maxdotSlowness; cboxes [SLOW].setState (true); preference = SLOW; notifyProcs (); } else if (event.target == cboxes [FAST]) { // set JOS's slowness and dotslowness // set the preference JOS.slowness = JOS.maxSlowness / 100; JOS.dotSlowness = JOS.maxdotSlowness / 100; cboxes [FAST].setState (true); preference = FAST; notifyProcs (); } return super.handleEvent (event); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Counter.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Counter.java 4. test : java ProducerConsumerOK */ package JOS; import JOS.JOS; public class Counter { private int i; public Counter (int i) { JOS.dbug.println (JOS.CONSTRUCTOR, "Counter Constructor STUDENT VERSION"); this.i = i; } public int getCount () { return i; } public void incrCount () { i++; } public void decrCount () { i--; } public boolean isEqual (int i) { if (this.i == i) { return true; } else { return false; } } } /* Copyright (c) Edward A. Billard, 1997 STUB: Debug.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Debug.java 4. test : java DiningPhilosophersOK */ package JOS; import JOS.JOS; import java.awt.Color; import java.util.Random; public class Debug { private OutGUI outGUI; private Transit transit; // maintain a OutGUI and a Transit public Debug () { if (JOS.useOutGUI) { // create a new OutGUI for the debug console // create a new Transit for JOS state transitions this.outGUI = new OutGUI ("OutGUI"); this.transit = new Transit ("Transit"); } } // return a new color for the dots public Color getColor () { int r = (int) (Math.random () * 128); int g = (int) (Math.random () * 128); int b = (int) (Math.random () * 128); Color color = new Color (r, g, b); return color; } public void println (int lvl, String s) { if (JOS.debugLvl >= lvl) { if (JOS.useOutGUI) { // use the OutGUI to println outGUI.println (s); } else { // use System.out.println System.out.println (s); } } } public void printTransition (String name, Color c, int s1, int s2) { println (JOS.TRANSITION, name + " State " + Proc.stateNames [s1] + " -> " + Proc.stateNames [s2]); if (JOS.useOutGUI) { transit.transition(c, s1, s2); } } public void registerProc (String name, Color c) { if (JOS.useOutGUI) { transit.registerProc(name, c); } } } /* Test BufPool, DiskUpper, DiskLower, DiskReq together. */ package JOS; import JOS.JOS; import JOS.Queue; import JOS.Proc; import JOS.Priority; import JOS.Port; import JOS.DiskReq; import java.io.*; // Lower-Half Disk Driver similar to XINU and does both I/O // Be sure to consider the available synchronization mechanisms // Use one big JOSDISK file for block IO public class DiskLower extends Proc { private Port diskPort; private Queue diskQ; private BufPool bufPool; private RandomAccessFile r; // do a DiskInit () public DiskLower (String name, Port diskPort, Queue diskQ, BufPool bufPool) { super (name, Priority.NORM_PRIORITY, false); JOS.dbug.println (JOS.CONSTRUCTOR, name + " DiskLower Constructor STUDENT VERSION"); this.diskPort = diskPort; this.diskQ = diskQ; this.bufPool = bufPool; DiskInit (); } // process both read and write disk requests from the queue // use DiskIO to do the actual Java I/O public void run () { int size; while (true) { if (diskQ.isEmpty ()) { diskPort.Receive (); } size = diskQ.size (); for (int i = 0; i < size; i++) { DiskIO ((DiskReq) diskQ.dequeue ()); } } } // if file named JOSDISK does not exist, // then make a new JOSDISK and fill it with JOS.DISKSIZE blocks // open an existing JOSDISK file private void DiskInit () { try { String fileName = new String ("JOSDISK"); File JOSDISK = new File (fileName, "rw"); if (!JOSDISK.exists ()) { r = new RandomAccessFile (fileName, "rw"); for (int i = 0; i < JOS.DISKSIZE; i++) { r.write (new byte [BufPool.BUFSIZE]); } } else { r = new RandomAccessFile (fileName, "rw"); } } catch (Exception e) { System.out.println (e); } } // do Java I/O: seek and then read or write to the JOSDISK file private void DiskIO (DiskReq dr) { try { r.seek ((long) dr.getBlock () * BufPool.BUFSIZE); if (dr.getType () == DiskReq.READ) { JOS.dbug.println (JOS.IPC, getName () + " read from disk block #" + dr.getBlock ()); r.read (dr.getBlockBuf ()); Proc currentProc = dr.getProc (); currentProc.Resume (); } else if (dr.getType () == DiskReq.WRITE) { JOS.dbug.println (JOS.IPC, getName () + " write to disk block #" + dr.getBlock ()); r.write (dr.getBlockBuf ()); bufPool.putBlockBuf (dr.getBlockBuf ()); } } catch (Exception e) { } } } /* Test BufPool, DiskUpper, DiskLower, DiskReq together. */ package JOS; import JOS.JOS; // This acts as a record structure for disk requests public class DiskReq { public final static int WRITE = 0; public final static int READ = 1; private int ioType; private int block; private byte [] buf; private Proc proc; public DiskReq(int ioType, int block, byte [] buf, Proc proc) { this.ioType = ioType; this.block = block; this.buf = buf; this.proc = proc; } public Proc getProc () { return proc; } public int getBlock () { return block; } public byte [] getBlockBuf () { return buf; } public int getType () { return ioType; } } /* Test BufPool, DiskUpper, DiskLower, DiskReq together. */ package JOS; import JOS.JOS; import JOS.Queue; import JOS.Proc; import JOS.DiskReq; // Upper-Half of Disk Driver similar to XINU (but does both I/O) // Maintain the proper synchronization with the lower half public class DiskUpper { private DiskLower diskLower; private Port diskPort = new Port ("BufferPort"); private Queue diskQ = new Queue ("DiskQ", Queue.DISK); private BufPool bufPool = new BufPool ("BufferPool"); private String name; public DiskUpper(String name) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " DiskUpper Constructor STUDENT VERSION"); this.name = name; this.diskLower = new DiskLower ("DiskLower", diskPort, diskQ, bufPool); diskLower.Start (); } // enqueue a new disk request private void diskEnqueue (int ioType, int block, byte [] buf) { if (diskQ.isEmpty ()) { diskPort.Send (new Message ("I/O")); } diskQ.enqueue (new DiskReq (ioType, block, buf, diskLower.currentProc ())); } // asynchronous write: make a copy of the buffer public void write (int block, byte [] buf) { byte [] buf1 = bufPool.getBlockBuf (); bufPool.copyBlockBuf (buf, buf1); JOS.dbug.println (JOS.IPC, name + " write to disk block # " + block); diskEnqueue (DiskReq.WRITE, block, buf1); } // synchronous read: do not make a copy of the buffer public void read (int block, byte [] buf) { JOS.dbug.println (JOS.IPC, name + " read from disk block # " + block); diskEnqueue (DiskReq.READ, block, buf); Proc currentProc = diskLower.currentProc (); currentProc.Suspend (); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Dispatcher.stub (move to .java) 1. declare : no private variables 2. implement: public ctxsw method 3. compile : javac Dispathcer.java 4. test : java DiningPhilosophersOK */ package JOS; import JOS.JOS; import JOS.Proc; public class Dispatcher { private int count = 0; // maintain a count of the number of switches // this constructor is complete public Dispatcher () { JOS.dbug.println (JOS.CONSTRUCTOR, "Dispatcher Constructor STUDENT VERSION"); } // context switch by resuming new current and suspending old current. // only one Java thread is runnable, therefore it runs the one JOS wants. // get the previous state of the new current process // set the new state for the new current process // if the processes are not the same // if the previous state was READY_BUT_NOT_STARTED // start the thread // else // resume the thread // if the old process is DEAD // stop the thread // else // suspend the thread // possible Windows 95 bug: suspended process may come alive again // if it does: if you aren't the currentProc, suspend yourself again protected void ctxsw (Proc oldProc, Proc currProc) { int oldState; count++; JOS.dbug.println (JOS.METHOD, "CTXSW #" + count + " for " + oldProc.getName () + " to " + currProc.getName ()); oldState = currProc.getState (); currProc.setState (Proc.CURRENT); if (!currProc.equals (oldProc)) { if (oldState == Proc.READY_BUT_NOT_STARTED) { currProc.start (); } else { currProc.resume (); } if (oldProc.getState () == Proc.DEAD) { oldProc.stop (); } else { oldProc.suspend (); } } } } /* Copyright (c) Edward A. Billard, 1997 STUB: InConsumer.stub (move to .java) 1. declare : private variables are already declared 2. implement: public methods 3. compile : javac InConsumer.java 4. test : try different settings of JOS.useInGUI and JOS.useInController modify DiningPhilosophersOK to do a JOS.in.readln() */ package JOS; import JOS.JOS; import JOS.Semaphore; import JOS.Buffer; import JOS.InProducer; import JOS.InGUI; import java.io.*; public class InConsumer { static final int N = 5; private Semaphore readSem = new Semaphore ("ReadSem", 1); private String name; private InGUI inGUI; public InConsumer (String name) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " Constructor STUDENT VERSION"); this.name = name; } // create a new InGUI for the system keyboard // do a readln and get the String // dispose the InGUI // return the String private String launchGUI () { inGUI = new InGUI ("InGUI"); String aStr = inGUI.readln (); inGUI.dispose (); return aStr; } // make a new Java StringBuffer // maintain LOCAL Buffer and Semaphores for Producer/Consumer // create and Start a LOCAL InProducer (just to handle this one input line) // do // use the Semaphores appropriately for a Consumer process // get an Integer from the buffer (character buffering) // convert to an int, convert to a char // if not -1 append to the StringBuffer // while its not a -1 // return the StringBuffer as a String private String launchController () { Integer I; int i; char ch; StringBuffer stringBuf = new StringBuffer (); Buffer buf = new Buffer ("Buffer", N); Semaphore notEmpty = new Semaphore ("NotEmpty", 0); Semaphore notFull = new Semaphore ("NotFull", N); InProducer inProducer = new InProducer ("InProducer", buf, notEmpty, notFull); inProducer.Start (); try { do { notEmpty.Wait (); I = (Integer) buf.get (); i = I.intValue (); ch = (char) i; if (i != -1) { stringBuf.append (ch); } notFull.Signal (); } while (i != -1); } catch (Exception e) { } return stringBuf.toString (); } // convert System.in to a DataInputStream (Java 1.0) // convert System.in to a InputStreamReader to a BufferedReader (Java 1.1) // use try and catch to do a readLine private String systemReadln () { String str = ""; InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader (isr); try { str = br.readLine (); } catch (Exception e) { } return str; } public String readln () { // use readSem so that only ONE process at a time is getting // a line from the keyboard readSem.Wait (); String s; if (JOS.useInGUI) { s = launchGUI (); } else { if (JOS.useInController) { s = launchController (); } else { s = systemReadln (); } } readSem.Signal (); return s; } } /* Copyright (c) Edward A. Billard, 1997 STUB: InGUI.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac InGUI.java 4. test : set JOS.useInGUI to true modify DiningPhilosophersBAD.java to do JOS.in.readln() */ package JOS; import JOS.Semaphore; import java.awt.*; public class InGUI extends Frame { private TextField input; private Button ok; private Semaphore readSem = new Semaphore ("ReadSem", 1); private String title; // maintain a TextField, OK Button, and Semaphore // setLayout and add the TextField and Button to this Frame public InGUI (String title) { this.title = title; this.resize (640, 480); this.setTitle (title); this.show (); setBackground (Color.lightGray); setForeground (Color.black); //Font font = new Font ("Courier", Font.BOLD, 12); //setFont (font); setLayout (null); input = new TextField (); input.reshape (100, 100, 100, 25); add (input); ok = new Button ("OK"); ok.reshape (100, 440, 100, 25); add (ok); readSem.Wait (); } // wait on the semaphore and then return the text of the field // this causes the readln to suspend until there is input public String readln () { readSem.Wait (); return input.getText (); } // if the event.target is the button, then signal the semaphore // this lets the readln return public boolean handleEvent (Event event) { if (event.id == Event.ACTION_EVENT) { readSem.Signal (); return super.handleEvent(event); } else { return false; } } } // I put NetGraph.java in seperate file. /* Copyright (c) Edward A. Billard, 1997 STUB: InProducer.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac InProducer.java 4. test : set JOS.useInGUI to false set JOS.useInController to true modify DiningPhilosophersBAD.java to do JOS.in.readln() */ package JOS; import JOS.Proc; import JOS.Semaphore; import JOS.Priority; import java.io.*; // represents the slow keyboard input controller process public class InProducer extends Proc { private Buffer buf; private Semaphore notEmpty; private Semaphore notFull; private static final int LF = 10; // maintain Buffer and Semaphores // this Proc is NOT a userprocess public InProducer (String name, Buffer buf, Semaphore notEmpty, Semaphore notFull) { super (name, Priority.NORM_PRIORITY, false); JOS.dbug.println (JOS.CONSTRUCTOR, name + " InProducer Constructor STUDENT VERSION"); this.buf = buf; this.notEmpty = notEmpty; this.notFull = notFull; } // use a try and a catch // do // use System.in.read () to get a single int from the keyboard // if the input is a LF (10) then map this to a -1 // use the Semaphores appropriately for a Producer process // put the single int into the Buffer as an Integer object // (this input controller is doing a character buffer) // while not LF public void run () { int ch, ch2; try { do { ch = (int) System.in.read (); ch2 = ch; if (ch == LF) { ch = -1; } notFull.Wait (); buf.put (new Integer (ch)); notEmpty.Signal (); } while (ch2 != LF); } catch (Exception e) { } Stop (); } } /* Copyright (c) Edward Billard, 1997 */ /* This must be installed on a directory called JOS Use this class to set defaults Compile: javac JOS */ package JOS; public class JOS { // THE USER NEVER CHANGES THESE CONSTANTS // debug levels public static final int NODEBUG = 0; public static final int USER = 1; public static final int CONSTRUCTOR = 2; public static final int IPC = 3; public static final int TRANSITION = 4; public static final int METHOD = 5; // scheduling policies public static final int PRIORITY_POLICY = 1; public static final int FIFO_POLICY = 2; public static final int SJF_POLICY = 3; // number of blocks for JOSDISK public static final int DISKSIZE = 100; // number of buffers in buffer pool public static final int NUMBUFS = 20; // THE USER CAN TYPICALLY SET THESE VALUES // set this to false for REMOTE LOGIN and get output to console. // OSmenu should not be used. Instead the user executes each // application individually, e.g., java DiningPhilosophersOK public static boolean useOutGUI = true; // set this to false for remote login if there is a readln public static boolean useInGUI = true; // if useOutGUI is false, then OSmenu will write to console // and exit with each selection. set this to true so that // OSmenu does not exit. public static boolean useOSmenu = true; // set the amount of debug public static int debugLvl = NODEBUG; // set the number of iterations for the applications public static int steps = 6; // THE USER SOMETIMES SETS THESE VALUES // set the maximum slowness of the display. large numbers mean slow. // units are in msec for each state transition (1000 = 1 sec) public static int maxSlowness = 100; // a large number means the dots move slower. must be > 0 public static int maxdotSlowness = 100; // set the policy based on the constants above public static int schedulingPolicy = PRIORITY_POLICY; // set the alpha for the SJF formula (0..1.0) public static double alpha = 0.9; // THE USER RARELY SETS THESE VALUES // use a thread to do buffering of output public static boolean useOutController = true; // use a thread to do buffering of input public static boolean useInController = true; // set to false to avoid scheduling and let Java do it public static boolean useScheduler = true; // THE USER SHOULD NEVER MODIFY ANYTHING BELOW public static Debug dbug; public static OutProducer out; //ttyout public static InConsumer in; //ttyin public static DiskUpper disk; //disk in and out protected static Scheduler sch; public static Integer cntrl = new Integer (0); public static boolean cntrlFlag = false; protected static int slowness = maxSlowness; protected static int dotSlowness = maxdotSlowness; static { dbug = new Debug (); if (useScheduler) { sch = new Scheduler (); } out = new OutProducer ("OutProducer"); in = new InConsumer ("InConsumer"); disk = new DiskUpper ("DiskUpper"); // for block I/O } } /* Copyright (c) Edward A. Billard, 1997 STUB: Message.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Message.java 4. test : java RingOK */ package JOS; import JOS.JOS; import JOS.Port; public class Message { private Object obj; private Port senderPort; // save this object public Message (Object obj) { JOS.dbug.println (JOS.CONSTRUCTOR, "Message Constructor STUDENT VERSION"); this.obj = obj; } // save port of the sender of this message. used for return messages. public void setSenderPort (Port senderPort) { this.senderPort = senderPort; } // return the previously saved port public Port getSenderPort () { return senderPort; } // convert the object to a string public String getValue () { return obj.toString (); } // store a new object in the message public void setObject (Object obj) { this.obj = obj; } // get the object in the message public Object getObject () { return obj; } } /* This is used with the Message and Port classes. */ package JOS; import JOS.Port; import JOS.JOS; import java.net.*; public class NetGraph { public static int N = 6; public static int BASE_PORT = 5800; public static final int INF = 99999; public static int nextPort = BASE_PORT; public static final int DECODE_ERR = -99999; public Port ports [] = new Port [N + 1]; public int w [][] = new int [N + 1][N + 1]; public String hosts [] = new String [N + 1]; public int portNums [] = new int [N + 1]; public static boolean useNet = false; public static int debug = 1; public static int wake_up_id = 1; private String localHost; public NetGraph () { localHost = getLocalHost (); initIP (); initPortNum (); initWeights (); initPorts (); pause (); } public String getLocalHost () { String s = "UNKNOWN"; if (useNet) { try { InetAddress inet = InetAddress.getLocalHost (); s = inet.getHostName(); } catch (Exception e) { } } return s; } private void initIP () { hosts[1] = "palazzo"; //"gold"; hosts[2] = "palazzo"; //"chuzhi"; hosts[3] = "palazzo"; //"gold"; hosts[4] = "palazzo"; //"chuzhi"; hosts[5] = "palazzo"; //"gold"; hosts[6] = "palazzo"; //"chuzhi"; } private void initPortNum () { for (int i = 1; i <= N; i++) { portNums [i] = nextPort++; } } private void initWeights () { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { w [i][j] = INF; } } w[1][2] = 7; w[2][1] = 7; w[1][3] = 0; w[3][1] = 0; w[1][4] = 6; w[4][1] = 6; w[2][3] = 4; w[3][2] = 4; w[2][5] = 2; w[5][2] = 2; w[3][4] = 5; w[4][3] = 5; w[3][6] = 3; w[6][3] = 3; w[4][6] = 1; w[6][4] = 1; w[5][6] = 8; w[6][5] = 8; } private void initPorts () { for (int i = 1; i <= N; i++) { if (useNet) { ports [i] = new Port ("NetPort" + i, hosts [i], portNums [i]); } else { ports [i] = new Port ("Port" + i); } } } public boolean isLocal (int i) { if (useNet) { return (localHost.equals (hosts [i])); } else { return true; } } private void pause () { if (useNet) { System.out.println ("Type after all network systems are up."); try { int i = System.in.read (); } catch (Exception e) { } } } public static String encode (String s, int i) { return (s + i + ","); } public static int decode (String s, int index) { int count = 0; int lastindex = 0; int nextindex, result; do { nextindex = s.indexOf (',', lastindex); if (nextindex < 0) { return DECODE_ERR; } else if (count == index) { try { String substr = s.substring (lastindex, nextindex); result = Integer.parseInt (substr); } catch (Exception e) { result = DECODE_ERR; } return result; } else { count++; lastindex = nextindex+1; } } while (true); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Null.stub (move to .java) 1. declare : no private variables 2. implement: public methods 3. compile : javac Null.java 4. test : java DiningPhilosophers */ package JOS; import JOS.JOS; import JOS.Scheduler; import JOS.Priority; public class Null extends Proc { // Null process is not a user process public Null () { super ("Null", Priority.NULL_PRIORITY, false); JOS.dbug.println (JOS.CONSTRUCTOR, "Null Constructor STUDENT VERSION"); } // in the beginning, you may need to handle a race condition // between the scheduler, who creates Null, and Null, who // calls the scheduler. Hint: the scheduler is still null // until its "constructor" is complete. // then do infinite loop: do a thread yield and a reSched // see JOS.java : to get handle on the Scheduler public void run () { JOS.dbug.println (JOS.METHOD, "Null Run Method"); while (true) { Yield (); JOS.sch.reSched (); } } } /* Copyright (c) Edward A. Billard, 1997 STUB: OutConsumer.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac OutConsumer.java 4. test : set JOS.useOutContoller to be true java OSmenu */ package JOS; import JOS.JOS; import JOS.Proc; import JOS.Semaphore; import JOS.Priority; // represents the slow console output controller process public class OutConsumer extends Proc { private Buffer buf; private Semaphore notEmpty; private Semaphore notFull; // maintain Buffer and Semaphores // this Proc is NOT a userprocess public OutConsumer (String name, Buffer buf, Semaphore notEmpty, Semaphore notFull) { super (name, Priority.NORM_PRIORITY, false); JOS.dbug.println (JOS.CONSTRUCTOR, name + " OutConsumer Constructor STUDENT VERSION"); this.buf = buf; this.notEmpty = notEmpty; this.notFull = notFull; } // loop forever: use the Semaphores appropriately for a Consumer // access the Buffer where each element is an entire String // use System.out.println public void run () { String str; try { while (true) { notEmpty.Wait (); str = (String) buf.get (); System.out.println (str); notFull.Signal (); } } catch (Exception e) { } } } /* Copyright (c) Edward A. Billard, 1997 STUB: OutGUI.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac OutGUI.java 4. test : java OSmenu */ package JOS; import java.awt.*; public class OutGUI extends Frame { private TextArea output; private String title; // create and maintain a TextArea // setLayout and add the TextArea public OutGUI (String title) { this.title = title; this.resize (640, 480); this.setTitle (title); this.show (); setBackground (Color.lightGray); setForeground (Color.black); Font font = new Font ("Courier", Font.BOLD, 12); setFont (font); setLayout (null); output = new TextArea (); output.reshape (100, 100, 440, 280); add (output); } // append the string to the TextArea public void println (String s) { output.setText (output.getText () + s + "\n"); } public boolean handleEvent (Event event) { if (event.id == Event.WINDOW_DESTROY) { System.exit (0); } else if (event.id == Event.WINDOW_ICONIFY) { System.exit (0); } return super.handleEvent (event); } } /* Copyright (c) Edward A. Billard, 1997 STUB: OutProducer.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac OutProducer.java 4. test : try different settings of JOS.useOutGUI and JOS.useOutController */ package JOS; import JOS.Buffer; import JOS.OutConsumer; import JOS.OutGUI; public class OutProducer { private static final int N = 5; private String name; private Buffer buf; private Semaphore notEmpty; private Semaphore notFull; private OutGUI outGUI; private OutConsumer outConsumer; // maintain the appropriate Semaphores for a Producer/Consumer // maintain N element Buffer, each element will be an entire string public OutProducer (String name) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " Constructor STUDENT VERSION"); this.name = name; if (JOS.useOutController) { // create a new OutConsumer Proc and Start it this.buf = new Buffer ("Buffer", N); this.notEmpty = new Semaphore ("NotEmpty", 0); this.notFull = new Semaphore ("NotFull", N); this.outConsumer = new OutConsumer ("OutConsumer", buf, notEmpty, notFull); outConsumer.Start (); } if (JOS.useOutGUI) { // create a new OutGUI for the system console this.outGUI = new OutGUI ("OutGUI"); } } public void println (String s) { try { if (JOS.useOutGUI) { // use the OutGUI to print it outGUI.println (s); } if (JOS.useOutController) { // use the Semaphores appropriately and put into the buffer notFull.Wait (); buf.put (s); notEmpty.Signal (); } if ((!JOS.useOutGUI) && (!JOS.useOutController)) { System.out.println (s); } } catch (Exception e) { } // find out which Proc you are and Yield // this means that a println gives up the CPU Proc currProc = JOS.sch.currentProc (); currProc.Yield (); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Port.stub (move to .java) 0. rename : move Port.stub Port.java 1. declare : private variables 2. implement: methods a. do Receive and Send b. later do network constructor, NetSend, run 3. compile : javac Port.java 4. test : java RingOK (and different settings in JOS/NetGraph.java) */ package JOS; import JOS.JOS; import JOS.Queue; import JOS.Message; import java.io.*; import java.net.*; public class Port extends Thread { // a Thread, not a Proc // maintain msgQ for messages and recvIDQ for suspended receiver Procs private Queue msgQ = new Queue ("MsgQueue", Queue.FIFO); private Queue recvIDQ =new Queue ("RecvIDQueue", Queue.FIFO); private String host = ""; private int portNum = 0; private boolean flag = false; public Port (String name) { super (name); JOS.dbug.println (JOS.CONSTRUCTOR, getName () + " Port Constructor STUDENT VERSION"); } // create a default Port public Port () { super (""); } // special constructor for messages across a network // start the thread that listens for messages public Port (String name, String host, int portNum) { super (name); this.host = host; this.portNum = portNum; this.flag = true; run (); } // if msgQ is empty, // the receiver is the current Proc // change the state for the receiver // add the receiver to the appropriate queue // reSched (see JOS.java for the handle) // return the message from the appropriate queue public Message Receive () { Proc receiver; JOS.dbug.println (JOS.IPC, Proc.who () + " " + getName () + ".Receive"); if (msgQ.isEmpty ()) { receiver = JOS.sch.currentProc (); receiver.setState (Proc.RECEIVING); recvIDQ.enqueue (receiver); JOS.sch.reSched (); } return (Message) msgQ.dequeue (); } // special function for messages across a network // make a Socket and convert OutputStream to PrintStream (1.0) // (try to use PrintWriter in 1.1) // println the msg private void NetSend (String msg) { try { Socket connection = new Socket (host, portNum); PrintStream out = new PrintStream (connection.getOutputStream ()); out.println (msg); } catch (Exception e) { } } // if the network constructor was used, then call NetSend, otherwise: // add the message to the appropriate queue // if there are receivers waiting for a message // get the longest waiting receiver // ready the receiver // reSched (see JOS.java for the handle) public void Send (Message msg) { Proc longest; JOS.dbug.println (JOS.IPC, Proc.who () + " " + getName () + ".Send"); if (flag == true) { NetSend (msg.toString ()); } else if (flag == false) { msgQ.enqueue (msg); } if (!recvIDQ.isEmpty ()) { longest = (Proc) recvIDQ.dequeue (); JOS.sch.ready (longest); JOS.sch.reSched (); } } // run method handles all incoming network traffic to this Port (portNum) // create ServerSocket and then loop: // accept; convert InputStream to DataInputStream and do readLine // enqueue the received string as a Message // if a receiver is waiting, dequeue it and ready it public void run () { try { ServerSocket serverSocket = new ServerSocket (portNum); while (true) { Socket connection = serverSocket.accept (); DataInputStream in = new DataInputStream (connection.getInputStream ()); String message = in.readLine (); msgQ.enqueue (message); if (!recvIDQ.isEmpty ()) { Proc receiver = (Proc) recvIDQ.dequeue (); JOS.sch.ready (receiver); } } } catch (Exception e) { } } } /* Copyright (c) Edward A. Billard, 1997 STUB: Priority.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Priority.java 4. test : java SchedulingOK PRIO java SchedulingOK FIFO java SchedulingOK SJF */ package JOS; import JOS.JOS; public class Priority { public static final int MAX_PRIORITY = 10000; public static final int MIN_PRIORITY = 2; public static final int NULL_PRIORITY = 1; public static final int NORM_PRIORITY = 5; private int ThreadPriority; private int ProcPriority; private String name; // maintain a ThreadPriority for Java // maintain a ProcPriority for JOS // this constructor is complete public Priority (String name, int priority) { this.ThreadPriority = Thread.NORM_PRIORITY; this.name = name; if (name.equals ("Null")) { this.ProcPriority = NULL_PRIORITY; if ((JOS.useOutGUI) || (JOS.useOSmenu)) { this.ThreadPriority = Thread.NORM_PRIORITY; } else { this.ThreadPriority = Thread.MIN_PRIORITY; } } else { switch (JOS.schedulingPolicy) { case JOS.PRIORITY_POLICY: this.ProcPriority = priority; break; case JOS.FIFO_POLICY : this.ProcPriority = NORM_PRIORITY; break; case JOS.SJF_POLICY : this.ProcPriority = MAX_PRIORITY; break; } } JOS.dbug.println (JOS.CONSTRUCTOR, "Priority Constructor for " + name + " ProcPriority = " + ProcPriority + " ThreadPriority = " + ThreadPriority + " STUDENT VERSION"); } public int getProcPriority () { return ProcPriority; } public int getThreadPriority () { return ThreadPriority; } // if JOS's schedulingPolicy is SJF // if the process name is not Null // get a new prediction for tau // compute a new ProcPriority in MIN_PRIORITY..MAX_PRIORITY public void updatePriority (long inCurrent) { if (JOS.schedulingPolicy == JOS.SJF_POLICY) { if (!name.equals ("Null")) { double tau = predict (inCurrent, getProcPriority (), JOS.alpha); int priority = (int) tau; if (priority > MAX_PRIORITY) { priority = MAX_PRIORITY; } priority = MAX_PRIORITY - priority + MIN_PRIORITY; setProcPriority (priority); } } } // compute a prediction for a new tau private double predict (long burst, double tau, double alpha) { return (alpha * burst + (1 - alpha) * tau); } // save the value public void setProcPriority (int priority) { ProcPriority = priority; } } /* Copyright (c) Edward A. Billard, 1997 STUB: Proc.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Proc.java 4. test : java DiningPhilosophersOK */ package JOS; import JOS.JOS; import JOS.Scheduler; import JOS.Priority; import java.awt.Color; import java.util.Date; public class Proc extends Thread { public static final int NEW = 0; public static final int DEAD = 1; public static final int SUSPENDED = 2; public static final int READY = 3; public static final int READY_BUT_NOT_STARTED = 4; public static final int CURRENT = 5; public static final int WAITING = 6; public static final int RECEIVING = 7; public static final String stateNames [] = { "NEW", "DEAD", "SUSPENDED", "READY", "READY_BUT_NOT_STARTED", "CURRENT", "WAITING", "RECEIVING" }; private static int procCount = 0; // number of processes // this is the Process Control Block (PCB) private Priority prio; // contains both Java and JOS priorities private int state = NEW; // current state private long enterCurrent = 0; // clock time on entry to CPU private Color myColor; // color for my dot // let the Thread remember the name // count only user processes // get a new Priority for this Proc // set the Java priority for this Thread // get a color from Debug // register me with Debug public Proc (String name, int priority, boolean userProcess) { super (name); JOS.dbug.println(JOS.CONSTRUCTOR, getName () + " Proc Constructor STUDENT VERSION"); if (userProcess) { procCount++; } prio = new Priority (name, priority); myColor = JOS.dbug.getColor (); JOS.dbug.registerProc (name, myColor); } // default constructor public Proc (String name, boolean userProcess) { this (name, Priority.NORM_PRIORITY, userProcess); } // default constructor public Proc (String name, int priority) { this (name, priority, true); } // default constructor public Proc (String name) { this (name, Priority.NORM_PRIORITY, true); } // default constructor public Proc () { this ("", Priority.NORM_PRIORITY, true); } // ready this in the READY_BUT_NOT_STARTED state // see JOS.java for Scheduler handle public void Start () { JOS.dbug.println(JOS.METHOD, getName () + " Start Method"); JOS.sch.ready ((Proc) getThread (), READY_BUT_NOT_STARTED); } public void Stop () { JOS.dbug.println(JOS.METHOD, getName () + " Stop Method, procCount = " + procCount); setState (DEAD); procCount--; if (procCount == 0) { if ((JOS.useOutGUI) || (JOS.useOSmenu)) { // let the system continue // MODIFIED: 7/20/98 synchronized (JOS.cntrl) { JOS.cntrl.notify (); } } else { System.exit (0); } } if (JOS.useScheduler) { JOS.sch.reSched(); } super.stop (); } // if its READY or CURRENT // set the state to SUSPENDED // if its READY // unready it // else // reSched public void Suspend () { int s; JOS.dbug.println (0, getName () + " Suspend Method"); s = getState (); if (s == READY || s == CURRENT) { setState (SUSPENDED); if (s == READY) { JOS.sch.unready ((Proc) getThread ()); } else { JOS.sch.reSched (); } } } // if its SUSPENDED // ready it // reSched public void Resume () { JOS.dbug.println (JOS.METHOD, getName () + " Resume Method"); if (getState () == SUSPENDED) { JOS.sch.ready ((Proc) getThread ()); JOS.sch.reSched (); } } // ready it and reSched public void Yield () { JOS.sch.ready ((Proc) getThread ()); JOS.sch.reSched (); } protected void setState (int state) { // get the current time // if the old state is CURRENT // compute how much time its been on the CPU // update the Priority // else // if the new state is CURRENT // remember the time on entry to the CPU Date d = new Date (); long time = d.getTime (); if (this.state == CURRENT) { time = time - enterCurrent; prio.updatePriority ((int) time); } else if (state == CURRENT) { enterCurrent = time; } if (JOS.cntrlFlag) { try { // MODIFIED: 7/20/98 synchronized (JOS.cntrl) { JOS.cntrl.wait (); JOS.cntrlFlag = false; } } catch(InterruptedException e) { } } if (JOS.slowness > 0) { try { sleep (JOS.slowness); // sleep for the specified number of msec } catch (Exception e) { } } JOS.dbug.printTransition (getName (), myColor, this.state, state); // save the current state this.state = state; } // return the state variable public int getState () { return state; } // get the ProcPriority from the Priority object public int getPrio () { return prio.getProcPriority (); } // set the ProcPriority in the Priority object and possibly fix ready queue public void setPrio (int priority) { prio.setProcPriority (priority); if (getState () == READY) { JOS.sch.unready ((Proc) getThread ()); JOS.sch.ready ((Proc) getThread ()); } } // get the currentProc from the Scheduler public static Proc currentProc () { return JOS.sch.currentProc (); } public Thread getThread () { // return super; // MODIFIED: 7/20/98 return (Thread) this; } // find out the name of the current running thread public static String who () { Proc currentProc = currentProc (); return currentProc.getName (); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Queue.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Queue.java 4. test : java DiningPhilosophersOK */ package JOS; import JOS.JOS; import JOS.Proc; import java.util.Vector; public class Queue { public static final int PRIO = 0; public static final int FIFO = 1; public static final int DISK = 2; private Vector queueVector; private String name; private int queueType; // create a Vector to store a queue of objects public Queue (String name, int queueType) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " Queue Constructor STUDENT VERSION"); queueVector = new Vector (); this.name = name; this.queueType = queueType; } // make a default FIFO queue public Queue () { this ("", FIFO); } // if the queueType is FIFO, just add the object // if the queueType is PRIO, insert it in order assuming object is a Proc // if the queueType is DISK, insert it to minimize arm movement // synchronize the access to the Vector public void enqueue (Object obj) { synchronized (queueVector) { if (queueType == FIFO || isEmpty ()) { queueVector.addElement (obj); } else if (queueType == PRIO) { Proc aProc = (Proc) obj; int flag = 0; int size = size (); for (int i = 0; i < size; i++) { Proc aProc2 = (Proc) queueVector.elementAt (i); if (aProc.getPrio () > aProc2.getPrio ()) { queueVector.insertElementAt (aProc, i); flag = 1; break; } } if (flag == 0) { queueVector.addElement (aProc); } } else if (queueType == DISK) { DiskReq diskReq = (DiskReq) obj; int flag = 0; int size = size (); for (int i = 0; i < size; i++) { DiskReq diskReq2 = (DiskReq) queueVector.elementAt (i); if (diskReq.getBlock () > diskReq2.getBlock ()) { queueVector.insertElementAt (diskReq, i); flag = 1; break; } } if (flag == 0) { queueVector.addElement (diskReq); } } } } // synchronize the access to the Vector public Object dequeue () { Object obj = new Object (); synchronized (queueVector) { obj = queueVector.firstElement (); queueVector.removeElementAt (0); } return obj; } public boolean isEmpty () { return queueVector.isEmpty (); } public void remove (Object obj) { queueVector.removeElement (obj); } // assuming the queue is sorted on Proc priorities, // return the priority of the head object // do not delete this object // if the queue is empty, return -1 public int maxKey () { if (queueType == PRIO && !isEmpty ()) { Proc proc = (Proc) queueVector.firstElement (); return proc.getPriority (); } else if (queueType == DISK && !isEmpty ()) { DiskReq diskReq = (DiskReq) queueVector.firstElement (); return diskReq.getBlock (); } else { return -1; } } public int size () { return queueVector.size (); } } /* Copyright (c) Edward A. Billard, 1997 STUB: Scheduler.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Scheduler.java 4. test : java DiningPhilosophersOK */ package JOS; import JOS.Proc; import JOS.Queue; import JOS.JOS; public class Scheduler { private static Proc currProc = new Null (); private static Queue readyQ = new Queue ("ReadyQ", Queue.PRIO); private static Dispatcher dispatcher = new Dispatcher (); // maintain static currProc, readyQ, and dispatcher // currProc is a new Null // set its state to CURRENT // start it using a thread start public Scheduler () { JOS.dbug.println (JOS.CONSTRUCTOR, "Scheduler Constructor STUDENT VERSION"); currProc.setState (Proc.CURRENT); currProc.start (); } protected Proc currentProc () { return currProc; } protected void setCurrentProc (Proc aProc) { currProc = aProc; } // save old current process // if its state is still CURRENT // if the maximum key in the ready queue is <= my priority // return // else // ready the old current process // set the current process to the head of the ready queue // context switch between the old and new process (see Dispatcher.java) protected void reSched () { // This is in case AWT threads, etc. get in here Thread me = Thread.currentThread (); Thread me1 = currProc.getThread (); if (!me.equals (me1)) { return; } Proc oldCurrent = currentProc (); if (oldCurrent.getState () == Proc.CURRENT) { if (readyQ.maxKey () <= oldCurrent.getPriority ()) { return; } else { this.ready (oldCurrent); } } Proc newCurrent = (Proc) readyQ.dequeue (); setCurrentProc (newCurrent); dispatcher.ctxsw (oldCurrent, newCurrent); } // a default method that assumes the new state will be READY protected void ready (Proc aProc) { this.ready (aProc, Proc.READY); } // set the state and add the proc to the ready queue // this method is used by new processes which are READY_BUT_NOT_STARTED protected void ready (Proc aProc, int state) { JOS.dbug.println (JOS.METHOD, "Ready Method " + aProc.getName ()); aProc.setState (state); readyQ.enqueue (aProc); } // set the state to SUSPENDED // remove the proc from the ready queue protected void unready (Proc aProc) { aProc.setState (Proc.SUSPENDED); readyQ.remove (currentProc ()); } public Queue getQueue () { return readyQ; } } /* Copyright (c) Edward A. Billard, 1997 STUB: Semaphore.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Semaphore.java 4. test : java DiningPhilosophersOK */ package JOS; import JOS.Queue; import JOS.Proc; public class Semaphore { private String name; private int count; private Queue queue; // maintain a count and a Queue public Semaphore (String name, int count) { JOS.dbug.println (JOS.CONSTRUCTOR, name + " Semaphore Constructor STUDENT VERSION"); this.name = name; this.count = count; this.queue = new Queue (name, Queue.FIFO); } // default constructor public Semaphore (int count) { this ("", count); } // if the count goes negative // a new waiter is the currentProc // set the state of the waiter to WAITING // add the waiter to the queue // reSched (see JOS.java for the handle) public void Wait () { Proc waiter; JOS.dbug.println(JOS.IPC, Proc.who () + " " + name + ".Wait "); if (--count < 0) { waiter = JOS.sch.currentProc (); waiter.setState (Proc.WAITING); queue.enqueue (waiter); JOS.sch.reSched (); } } // if the count stays negative (then there are waiting processes) // get the longest waiting process // ready this process // reSched (see JOS.java for the handle) public void Signal () { Proc proc; JOS.dbug.println (JOS.IPC, Proc.who () + " " + name + ".Signal"); if (count++ < 0) { proc = (Proc) queue.dequeue (); JOS.sch.ready (proc); JOS.sch.reSched (); } } } /* Copyright (c) Edward A. Billard, 1997 STUB: Transit.stub (move to .java) 1. declare : private variables 2. implement: public methods 3. compile : javac Transit.java 4. test : java OSmenu */ package JOS; import java.awt.*; public class Transit extends Frame { private CanvasGUI canvas; private CanvasProcs procs; private CheckPanel panel; private String title; // setLayout // maintain a CanvasGUI, CanvasProcs, and CheckPanel // add them to this Frame public Transit (String title) { this.title = title; this.resize (640, 480); this.setTitle (title); this.show (); setBackground (Color.lightGray); setForeground (Color.black); Font font = new Font ("Courier", Font.BOLD, 12); setFont (font); setLayout (new FlowLayout ()); procs = new CanvasProcs (); procs.resize (640, 160); add (procs); canvas = new CanvasGUI (); canvas.resize (640, 160); add (canvas); panel = new CheckPanel (); panel.resize (640, 160); add (panel); } public void transition (Color c, int s1, int s2) { canvas.transition(c, s1, s2); } public void registerProc (String name, Color c) { procs.registerProc (name, c); panel.resetBoxes (); } public boolean handleEvent (Event event) { if (event.id == Event.WINDOW_DESTROY) { System.exit (0); } else if (event.id == Event.WINDOW_ICONIFY) { System.exit (0); } return super.handleEvent (event); } } /* Filename: JOS.out */ /* DiningPhilosophers */ PhilosopherOK #1 Running STUDENT VERSION PhilosopherOK #2 Running STUDENT VERSION PhilosopherOK #1 eating at step 6 PhilosopherOK #2 eating at step 6 PhilosopherOK #1 eating at step 5 PhilosopherOK #2 eating at step 5 PhilosopherOK #1 eating at step 4 PhilosopherOK #2 eating at step 4 PhilosopherOK #1 eating at step 3 PhilosopherOK #2 eating at step 3 PhilosopherOK #1 eating at step 2 PhilosopherOK #2 eating at step 2 PhilosopherOK #1 eating at step 1 PhilosopherOK #2 eating at step 1 /* Producer/Consumer */ ProducerOK #1 Running ConsumerBAD #1 Running ProducerOK #1 puts 0 at step 6 ProducerOK #1 puts 1 at step 5 ConsumerBAD #1 gets 0 at step 6 ProducerOK #1 puts 2 at step 4 ConsumerBAD #1 gets 1 at step 5 ProducerOK #1 puts 3 at step 3 ConsumerBAD #1 gets 2 at step 4 ProducerOK #1 puts 4 at step 2 ConsumerBAD #1 gets 3 at step 3 ProducerOK #1 puts 5 at step 1 ConsumerBAD #1 gets 4 at step 2 ConsumerBAD #1 gets 5 at step 1 /* Reader/Writer */ ReaderOK #1 Running STUDENT VERSION ReaderOK #2 Running STUDENT VERSION WriterBAD #1 Running STUDENT VERSION ReaderOK #1 tests reading at step 6 ReaderOK #2 tests reading at step 6 WriterBAD #1 begins writing at step 6 WriterBAD #1 ends writing at step 6 ReaderOK #1 begins reading at step 6 ReaderOK #2 begins reading at step 6 ReaderOK #1 ends reading at step 6 ReaderOK #2 ends reading at step 6 ReaderOK #1 tests reading at step 5 ReaderOK #2 tests reading at step 5 WriterBAD #1 begins writing at step 5 WriterBAD #1 ends writing at step 5 ReaderOK #1 begins reading at step 5 ReaderOK #2 begins reading at step 5 ReaderOK #1 ends reading at step 5 ReaderOK #2 ends reading at step 5 ReaderOK #1 tests reading at step 4 ReaderOK #2 tests reading at step 4 WriterBAD #1 begins writing at step 4 WriterBAD #1 ends writing at step 4 ReaderOK #1 begins reading at step 4 ReaderOK #2 begins reading at step 4 ReaderOK #1 ends reading at step 4 ReaderOK #2 ends reading at step 4 ReaderOK #1 tests reading at step 3 ReaderOK #2 tests reading at step 3 WriterBAD #1 begins writing at step 3 WriterBAD #1 ends writing at step 3 ReaderOK #1 begins reading at step 3 ReaderOK #2 begins reading at step 3 ReaderOK #1 ends reading at step 3 ReaderOK #2 ends reading at step 3 ReaderOK #1 tests reading at step 2 ReaderOK #2 tests reading at step 2 WriterBAD #1 begins writing at step 2 WriterBAD #1 ends writing at step 2 ReaderOK #1 begins reading at step 2 ReaderOK #2 begins reading at step 2 ReaderOK #1 ends reading at step 2 ReaderOK #2 ends reading at step 2 ReaderOK #1 tests reading at step 1 ReaderOK #2 tests reading at step 1 WriterBAD #1 begins writing at step 1 WriterBAD #1 ends writing at step 1 ReaderOK #1 begins reading at step 1 ReaderOK #2 begins reading at step 1 ReaderOK #1 ends reading at step 1 ReaderOK #2 ends reading at step 1 /* Smoker */ SmokerBAD #1 Running STUDENT VERSION SmokerBAD #2 Running STUDENT VERSION SmokerBAD #3 Running STUDENT VERSION AgentOK #1 Running STUDENT VERSION SmokerBAD #1 smokes at step 6 SmokerBAD #2 smokes at step 6 SmokerBAD #3 smokes at step 6 SmokerBAD #1 smokes at step 5 SmokerBAD #2 smokes at step 5 SmokerBAD #3 smokes at step 5 SmokerBAD #1 smokes at step 4 SmokerBAD #2 smokes at step 4 SmokerBAD #3 smokes at step 4 SmokerBAD #1 smokes at step 3 SmokerBAD #2 smokes at step 3 SmokerBAD #3 smokes at step 3 SmokerBAD #1 smokes at step 2 SmokerBAD #2 smokes at step 2 SmokerBAD #3 smokes at step 2 SmokerBAD #1 smokes at step 1 SmokerBAD #2 smokes at step 1 SmokerBAD #3 smokes at step 1 /* Deadlock */ DeadlockOK #2 Running STUDENT VERSION DeadlockBAD #1 Running STUDENT VERSION DeadlockOK #2 sends [DeadlockOK #2 message] at step 6 DeadlockBAD #1 recvs [DeadlockOK #2 message] at step 6 DeadlockOK #2 sends [DeadlockOK #2 message] at step 5 DeadlockBAD #1 recvs [DeadlockOK #2 message] at step 5 DeadlockOK #2 sends [DeadlockOK #2 message] at step 4 DeadlockBAD #1 recvs [DeadlockOK #2 message] at step 4 DeadlockOK #2 sends [DeadlockOK #2 message] at step 3 DeadlockBAD #1 recvs [DeadlockOK #2 message] at step 3 DeadlockOK #2 sends [DeadlockOK #2 message] at step 2 DeadlockBAD #1 recvs [DeadlockOK #2 message] at step 2 DeadlockOK #2 sends [DeadlockOK #2 message] at step 1 DeadlockBAD #1 recvs [DeadlockOK #2 message] at step 1 /* Ring */ ServerBAD #1 Running STUDENT VERSION ClientOK #1 Running STUDENT VERSION ClientOK #2 Running STUDENT VERSION ClientOK #1 sends [ClientOK #1 requests block] at step 3 ClientOK #2 sends [ClientOK #2 requests block] at step 3 ServerBAD #1 recvs [ClientOK #1 requests block] at step 6 ServerBAD #1 sends [ServerBAD #1 returns block] at step 6 ServerBAD #1 recvs [ClientOK #2 requests block] at step 5 ClientOK #1 recvs [ServerBAD #1 returns block] at step 3 ServerBAD #1 sends [ServerBAD #1 returns block] at step 5 ClientOK #1 sends [ClientOK #1 requests block] at step 2 ClientOK #2 recvs [ServerBAD #1 returns block] at step 3 ServerBAD #1 recvs [ClientOK #1 requests block] at step 4 ClientOK #2 sends [ClientOK #2 requests block] at step 2 ServerBAD #1 sends [ServerBAD #1 returns block] at step 4 ServerBAD #1 recvs [ClientOK #2 requests block] at step 3 ClientOK #1 recvs [ServerBAD #1 returns block] at step 2 ServerBAD #1 sends [ServerBAD #1 returns block] at step 3 ClientOK #1 sends [ClientOK #1 requests block] at step 1 ClientOK #2 recvs [ServerBAD #1 returns block] at step 2 ServerBAD #1 recvs [ClientOK #1 requests block] at step 2 ClientOK #2 sends [ClientOK #2 requests block] at step 1 ServerBAD #1 sends [ServerBAD #1 returns block] at step 2 ServerBAD #1 recvs [ClientOK #2 requests block] at step 1 ClientOK #1 recvs [ServerBAD #1 returns block] at step 1 ServerBAD #1 sends [ServerBAD #1 returns block] at step 1 ClientOK #2 recvs [ServerBAD #1 returns block] at step 1 /* Star */ RingNodeBAD # 1 Running STUDENT VERSION RingNodeBAD # 2 Running STUDENT VERSION RingNodeBAD # 3 Running STUDENT VERSION RingNodeBAD # 4 Running STUDENT VERSION RingNodeBAD # 2 recvs 0 RingNodeBAD # 3 recvs 1 RingNodeBAD # 4 recvs 2 RingNodeBAD # 1 recvs 3 Message finished at RingNodeBAD # 1 CenterBAD #1 Running STUDENT VERSION RimOK #2 Running STUDENT VERSION RimOK #3 Running STUDENT VERSION RimOK #4 Running STUDENT VERSION RimOK #5 Running STUDENT VERSION RimOK #6 Running STUDENT VERSION RimOK #7 Running STUDENT VERSION RimOK #8 Running STUDENT VERSION RimOK #5 recvs Hello From The Outside World CenterBAD #1 recvs Hello From The Outside World RimOK #2 recvs Hello From the Center RimOK #3 recvs Hello From the Center RimOK #4 recvs Hello From the Center RimOK #6 recvs Hello From the Center RimOK #7 recvs Hello From the Center RimOK #8 recvs Hello From the Center /* BusyBees */ BeeOK #1 running , prio = 5 BeeOK #2 running , prio = 5 BeeOK #3 running , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 BeeOK #1 in critical section , prio = 5 BeeOK #2 in critical section , prio = 5 BeeOK #3 in critical section , prio = 5 /* Priority */ HighPrio/MedBurst Running HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding MedPrio/LongBurst Running MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding LowPrio/ShortBurst Running LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding /* FCFS */ MedPrio/LongBurst Running HighPrio/MedBurst Running LowPrio/ShortBurst Running MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding /* SJF */ MedPrio/LongBurst Running HighPrio/MedBurst Running LowPrio/ShortBurst Running MedPrio/LongBurst printing/yielding HighPrio/MedBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding LowPrio/ShortBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding HighPrio/MedBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding MedPrio/LongBurst printing/yielding /* DiskTest */ DiskTestOK #1 Running DiskTestOK #1 wasting some time DiskTestOK #1 wasting more time DiskTestOK #1 Suspend Method DiskTestOK #1 Suspend Method DiskTestOK #1 Suspend Method DiskTestOK #1 Suspend Method DiskTestOK #1 Suspend Method DiskTestOK #1 Suspend Method /* Filename: JOSDISK */ 0 8 25 26 38 39 46 69 76 83 95
BACK TO CS6560 PAGE.