id
stringlengths
36
36
text
stringlengths
1
1.25M
40147297-d6c0-4a48-b063-fc80c17411de
private void flushMemoryQueue() { Process p = memory.checkMemory(clock); // As long as there is enough memory, processes are moved from the memory queue to the cpu queue while(p != null) { // TODO: Add this process to the CPU queue! // Also add new events to the event queue if needed //p.leftMemoryQueue...
f5f829e5-39e7-4ae2-9352-5944b9a68d1a
private void switchProcess() { Process process = cpu.getActiveProcess(); if(process != null){ process.leftCpu(clock); cpu.addProcess(process); process.enterCpuQueue(clock); statistics.nofForcedProcessSwitch++; } process = cpu.startNextProcess(); if(process != null){ process.enterCpu(clock); ...
056a984c-bef5-4aac-b804-e98f96b47a29
private void endProcess() { Process process = cpu.getActiveProcess(); process.leftCpu(clock); process.setEndTime(clock); process.updateStatistics(statistics); memory.processCompleted(process); if(cpu.isIdle()){ switchProcess(); } }
d68f007a-5628-477a-9ece-16811d078e01
private void processIoRequest() { Process process = cpu.getActiveProcess(); process.leftCpu(clock); /** addProcess returns true if there is no active process in the IO*/ if(io.addProcess(process, clock)){ process.enterIo(clock); eventQueue.insertEvent(new Event(END_IO, clock+io.getRandomIoTime())); } ...
42c207f9-f3c9-48d2-addc-70091f6cf646
private void endIoOperation() { Process process = io.getActiveProcess(); process.leftIo(clock); statistics.nofIoOperations++; cpu.addProcess(process); process.enterCpuQueue(clock); if(cpu.isIdle()){ switchProcess(); } process = io.start(); if(process!=null){ process.enterIo(clock); eventQueue...
9ab53929-b84e-4e70-b800-b1e676fcb0ac
public static long readLong(BufferedReader reader) { try { return Long.parseLong(reader.readLine()); } catch (IOException ioe) { return 100; } catch (NumberFormatException nfe) { return 0; } }
486abdc7-212b-4d62-a57d-5e2d372720e6
public static void main(String args[]) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input system parameters: "); System.out.print("Memory size (KB): "); long memorySize = readLong(reader); while(memorySize < 400) { System.out.println("Memory si...
af6420df-190b-4fb5-a06c-c7e48c6f6a52
public SimulationGui(long memorySize, long maxCpuTime, long avgIoTime, long simulationLength, long avgArrivalInterval) { super("Process scheduling simulator"); memoryQueue = new Queue("memory queue", 10, EAST); cpuQueue = new Queue("CPU queue", 10, WEST); ioQueue = new Queue("I/O queue", 10, EAST); timeElapse...
87a12d97-6c7c-44c3-8c6b-dbea986475bb
private void placeComponents() { memory = new Resource("Memory"); cpu = new Resource("CPU"); io = new Resource("I/O"); loadImages(); backgroundPanel = new PicturePanel(background); getContentPane().setLayout(null); getContentPane().add(backgroundPanel); backgroundPanel.setBounds(0,0,494,374); backg...
801e920b-b597-4cde-88bd-8fc093cc978c
public void run() { running = true; simulator.simulate(); }
93a07ad4-4ffd-47f1-ab87-4cfdc49e6675
public void actionPerformed(ActionEvent ae) { if(!running) { startButton.setText("Complete simulation"); // The simulation must be run in a separate thread, // we can't "hijack" the GUI's event handling thread. Thread t = new Thread(this); t.start(); } else { sleep = false; } }
4a707ca3-c8a0-4420-90d3-85a3bf7e36d8
private void addSliderLabels(JPanel p, int x, int y, int w, int h, String leftText, String rightText, String text) { JLabel left, middle, right; left = new JLabel(leftText); left.setHorizontalAlignment(JLabel.LEFT); left.setOpaque(false); p.add(left); left.setBounds(x,y,w,h); middle = new JLabel(text); ...
a8684402-32fb-48cf-934b-2942e7e0b610
public void setCpuActive(Process p) { cpu.setActiveProcess(p); }
3e919957-17d1-4d85-b3dd-5d8695a9c21a
public void setIoActive(Process p) { io.setActiveProcess(p); }
9bc8e2db-5187-4aa4-ba5a-2a2bb18d9452
public void timePassed(long time) { timeElapsed += time; timeElapsedLabel.setText("Simulated time elapsed: "+timeElapsed+" ms."); timeElapsedLabel.repaint(); try { if(sleep && isShowing()) Thread.sleep((10000-simulationSpeedSlider.getValue())*time/3000); } catch (InterruptedException ie) {} }
bd466bfa-1991-4bcb-a24d-439185fe0d4a
private Image loadImage(Toolkit tk, String file, MediaTracker tracker) { Image result = tk.createImage(file); tracker.addImage(result, 0); return result; }
550e7bd0-8ff9-4d13-82a4-13c443feef32
private void loadImages() { MediaTracker tracker = new MediaTracker(this); Toolkit tk = Toolkit.getDefaultToolkit(); background = loadImage(tk, "images/background.gif", tracker); try { tracker.waitForID(0); } catch (InterruptedException ie) {} }
ccf9510f-bb9f-47f3-9c13-37434351d547
public void setCpuActive(Process p);
745e73a4-479c-4e65-965e-d509d4c528d4
public void setIoActive(Process p);
e8e2582b-87c6-429e-a51b-c95e94e0766c
public void timePassed(long time);
4d6c117d-10a9-4c9d-91ae-332a397370b9
public IO(Gui gui, Queue ioQueue, long ioTime, Statistics statistics){ this.gui = gui; this.ioQueue = ioQueue; this.ioTime = ioTime; this.statistics = statistics; this.activeProcess = null; }
496c0b63-8166-4f12-ab1a-9146ae6b5f87
public boolean addProcess(Process process, long clock){ statistics.nofTimesInIoQue++; if(activeProcess==null){ activeProcess=process; process.enterIo(clock); gui.setIoActive(process); return true; } process.enterIoQueue(clock); ioQueue.insert(process); if(ioQueue.getQueueLength()>statistics.IoQu...
b2191958-907f-4086-9725-2b6f07ae129b
public Process start(){ if (ioQueue.isEmpty()){ return null; } Process process = (Process) ioQueue.removeNext(); this.activeProcess = process; gui.setIoActive(process); return process; }
16d8f31d-3868-48c1-8aa5-96bc3b627e6c
public Process getActiveProcess(){ Process process = this.activeProcess; this.activeProcess = null; gui.setIoActive(this.activeProcess); return process; }
944a348c-8fd3-47e5-8add-339efd429182
public long getRandomIoTime(){ return (long) (Math.random()*(ioTime+0.5*ioTime)); }
06682818-a9ac-4a2c-a9ea-91436aae188d
public void timePassed(long timePassed) { statistics.ioQueueLengthTime += ioQueue.getQueueLength()*timePassed; if (ioQueue.getQueueLength() > statistics.IoQueueLargestLength) { statistics.IoQueueLargestLength = ioQueue.getQueueLength(); } }
f370030a-0b2d-458f-a147-1646d4c8c335
public Process(long memorySize, long creationTime) { // Memory need varies from 100 kB to 25% of memory size memoryNeeded = 100 + (long)(Math.random()*(memorySize/4-100)); // CPU time needed varies from 100 to 10000 milliseconds cpuTimeNeeded = 100 + (long)(Math.random()*9900); // Average interval between I/O...
9ac02403-2f4f-4338-814b-3f6c4d79ae9f
public void draw(Graphics g, int x, int y, int w, int h) { g.setColor(color); g.fillRect(x, y, w, h); g.setColor(Color.black); g.drawRect(x, y, w, h); g.setFont(font); FontMetrics fm = g.getFontMetrics(font); g.drawString(""+processId, x+w/2-fm.stringWidth(""+processId)/2, y+h/2+fm.getHeight()/2); }
b0707e10-3dc9-425d-b926-1a222dde5b4e
public void leftMemoryQueue(long clock) { timeSpentWaitingForMemory += clock - timeOfLastEvent; timeOfLastEvent = clock; }
33316da1-20cc-43ec-a457-b19089654969
public long getMemoryNeeded() { return memoryNeeded; }
88704974-ba7b-4d44-ae26-6db307355f7d
public void updateStatistics(Statistics statistics) { statistics.totalTimeSpentWaitingForMemory += timeSpentWaitingForMemory; statistics.timeInSystem += endTime-startTime; statistics.nofCompletedProcesses++; //statistics.totalCpuTime+=timeSpentInCpu; statistics.totalTimeInReadyQueue += timeSpentInReadyQueue; ...
d192b2f5-aa58-45b7-8170-825433640921
public void enterCpu(long clock) { timeSpentInReadyQueue += clock - timeOfLastEvent; timeOfLastEvent = clock; }
9e07f930-9b53-4908-9c06-26a3251ef3e9
public void leftCpu(long clock){ cpuTimeNeeded-= clock-timeOfLastEvent; timeSpentInCpu += clock-timeOfLastEvent; timeToNextIoOperation -= clock-timeOfLastEvent; timeOfLastEvent = clock; }
ed85de00-7774-4f8f-b43b-12b9a8a0bab6
public void enterCpuQueue(long clock) { nofTimesInReadyQueue++; timeOfLastEvent = clock; }
0e061075-e832-4ff5-b057-bf8ef301bad1
public void enterIo(long clock){ timeSpentWaitingForIo += clock-timeOfLastEvent; timeOfLastEvent = clock; }
2c836749-1b23-4610-b39d-eaadc06ac255
public void leftIo(long clock){ timeSpentInIo += clock-timeOfLastEvent; timeOfLastEvent = clock; timeToNextIoOperation = (long) (Math.random() * 2 * avgIoInterval); }
de343b1c-f0ec-4f98-a1fa-eb3387f5a322
public void enterIoQueue(long clock){ nofTimesInIoQueue++; timeOfLastEvent=clock; }
c05a44ed-9dc9-4c9f-8d06-5cdf54408969
public long getTimeToIO() { return timeToNextIoOperation; }
5e3d89fe-f073-462f-86c2-f29f4b478850
public long getRemainingCpuTime() { return cpuTimeNeeded; }
77c1ee0f-48a4-400c-b93f-ad36ef9ea6da
public void subtractRemainingCpuTime(long maxCpuTime) { this.cpuTimeNeeded-=maxCpuTime; if(this.cpuTimeNeeded<0)this.cpuTimeNeeded=0; //Should never happen }
0f301b51-326e-4fbf-a3e3-d97cd6df0d24
public void setEndTime(long clock) { this.endTime = clock; }
4b72dbbf-1088-4405-93e5-12d3b30e3007
public Customer(int id){ this.id = id; orders = rand.nextInt(SushiBar.maxOrder-1) + 1; eatenOrders = rand.nextInt(orders)+1; takeAwayOrders = orders - eatenOrders; }
64f43dc5-5263-4e68-998f-3f787cc09447
public int getOrders() { return orders; }
c2e558e4-66f4-4950-9e75-8e02552e7d60
public int getEatenOrders() { return eatenOrders; }
77c15a62-6593-4168-a2d6-fd0cd480d405
public int getTakeAwayOrders() { return takeAwayOrders; }
63a61efe-c2cf-4da9-8ac4-11a6fea2ad5f
public int getId(){ return id; }
53cad83d-bdd2-41c2-abc4-d9b1e8133fbb
public synchronized void waitMe(){ try { this.wait(); } catch (InterruptedException e) { } }
f3e16194-377e-4ae8-b99b-81fb26a3e5aa
public void run() { if(!ServingArea.enter(this)){ //If it is able to enter the serving area it will not wait but just continue to eat SushiBar.write(Thread.currentThread().getName()+ ": Customer "+id+" is waiting for a free seat"); waitMe(); //The customer waits to be notified } SushiBar.write(Thread.curren...
40cc6e04-7109-467c-9a01-3ffdd53a1cbd
public String toString(){ //only used when I want to print information about the program, not used in the exercise return (""+id); }
a5fafdd2-8878-4038-8ef1-57f404354c39
public static int getID(){ return id; }
a47b5a78-78c0-40c7-a1fa-ade8b2547f2f
public synchronized static void addOrders(int orders){ totalOrders+=orders; }
c0264207-6e32-4aec-8d04-599e8e3b52c0
public synchronized static void addEatenOrders(int orders){ totalEatenOrders+=orders; }
e1b99bb8-3c6d-45b9-897e-39af43cbbbaf
public synchronized static void addTakeAwayOrders(int orders){ totalTakeAwayOrders+=orders; }
56f5d31f-fb5a-4011-a3db-cbf45f00d7fc
public static int getTotalOrders(){ return totalOrders; }
037417c3-9077-4653-9151-a612236e7a60
public static int getTotalEatenOrders(){ return totalEatenOrders; }
f7afd505-b2e6-42c4-af2b-ad2d7119e6c9
public static int getTotalTakeAwayOrders(){ return totalTakeAwayOrders; }
a4d570e0-3aae-4c9a-917f-69bf950687d5
public static void printStatistics(){ if(customers.size()==0 && queCustomers.size()==0 && !SushiBar.isOpen){ //we shall only print statistics if all the customers have left and the door is closed SushiBar.write(Thread.currentThread().getName()+": Total number of orders are "+totalOrders); SushiBar.write(Thread....
f4dc46f4-630a-4978-90c9-0ecc42f884f0
public synchronized static Customer handleQueCustomer(Customer customer, Boolean bool){ if(bool){ queCustomers.add(customer); return null; } else{ if(queCustomers.size()>0){ return queCustomers.remove(0); } return null; } }
45d949bb-bc91-4027-9461-e0d9ae41bf72
public synchronized static Customer getQueCustomer(int index){ return queCustomers.get(index); }
8ebbaeb4-b961-4601-ad0a-4a3a2c42bcd0
public synchronized static Boolean enter(Customer customer){ //Only the customers that enters without beeing called by the leave function use this way to the serving area if (customers.size() < capacity && customer.getId()==id){ id++; customers.add(customer); SushiBar.write(Thread.currentThread().getName()+ ...
08e9fc18-7058-4aa9-8ce9-cd9dafab7977
public synchronized static void leave(Customer customer){ totalOrders+=customer.getOrders(); //adding statistics totalEatenOrders+=customer.getEatenOrders(); totalTakeAwayOrders+=customer.getTakeAwayOrders(); if(customers.indexOf(customer)>0){ Customer oldCustomer = customers.remove(0); synchronized (oldC...
6dec300f-45f3-460d-9a94-b50b59a99434
public void run(){ while(SushiBar.isOpen){ SushiBar.write(Thread.currentThread().getName()+ ": Customer "+id+" is now created"); Customer customer = new Customer(id); //creates customers ServingArea.handleQueCustomer(customer, true); new Thread(customer).start(); //start the customer in a new thread id...
3fd9ed0d-6506-49a2-a06a-496dfa94c7e1
public static void main(String[] args) { //It only opens the log file, creates the door and clock. log= new File(path + "log.txt"); door = new Door(); new Thread(door).start(); Clock clock = new Clock(SushiBar.duration); }
8cb9ce5e-ee6d-4ddc-bc7d-b4124aabf38b
public static void write(String str){ try { FileWriter fw = new FileWriter(log.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); bw.write(Clock.getTime() + ", " + str +"\n"); bw.close(); System.out.println(Clock.getTime() + ", " + str); } catch (IOException e) { e.printStackTra...
a74aab5d-498d-4703-9d2a-941316f0432b
public Clock(int seconds){ timer = new Timer(); //At this line a new Thread will be created timer.schedule(new RemindTask(), seconds*1000); //delay in milliseconds }
0fcf463b-ec7e-4254-bc2a-dc5c268b2839
public void run() { SushiBar.isOpen= false; //prevents creating new customers. timer.cancel(); }
56e2cd0e-9f1a-4171-8b65-e8b320a765af
public static String getTime(){ // get current time Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); return sdf.format(cal.getTime()) ; }
15aa2156-d6c3-4b09-9423-8ef377d212c9
public static void main(String args[]){ CashBox pCashBox = new CashBox(); Selector pSelector = new Selector(); int drink; int money; int sugar; String choice; Scanner in = new Scanner(System.in); System.out.println("Insert coins: "); money = in.nextInt(); if (money!=0){ pCashBox.deposit(...
5940e342-1fa1-40b4-8881-580fa0078f95
public final void make(String choice){ System.out.printf("\tMixer: Making %s..\n\n", choice.toString()); }
f0c8a4cc-0f45-4fc1-af51-0522ebc47919
public final int priceOf(String menu){ if(menu!="bouillon"){ return 35; } else{ return 25; } }
8cc735c0-20cf-4d95-9c2b-83d7facda90e
public CashBox(){ credit = 0; }
1fc7e506-09a3-44d6-b066-e1464f66d2b8
public final void deposit(int amount){ credit += amount; System.out.printf("\tCashBox: Depositing %d cents.",amount); System.out.printf("\tYou now have %d cents credit.\n\n",credit); }
9b131ac2-c715-4bc5-a9fd-e88be3fbee18
public final void returnCoins(){ System.out.printf("\tCashBox: Returning %d cents\n",credit); credit = 0; }
86855c33-85fd-4be5-9cbd-384afa8b9ab1
public final boolean haveYou(int amount){ return credit >= amount; }
c6e0c363-889d-47f8-96bb-ad520c72ef21
public final void deduct(int amount){ credit -= amount; returnCoins(); }
dc5224e3-c7ea-4795-a002-6dbbe3013347
public final void select(String selected, CashBox cash){ int amount = pSelection.priceOf(selected); if (cash.haveYou(amount)){ pMixer.make(selected); cash.deduct(amount); System.out.printf("\t%s Served.\n\n", selected.toString()); } else{ System.out.println("\tSelector: Insufficient funds"); c...
0eceff46-c0c5-4561-8892-7ae3404705c9
public Collection<Ball> rolls(Collection<Ball> balls, Collider<Ball>... colliders);
1dc10704-bb20-4ea7-bf5b-8c884882e528
public boolean areColliding(Ball b1, Ball b2);
449a9b56-b907-42fe-97cc-cc904880518a
public Ball collide(Ball b1, Ball b2);
d7790060-f19a-464e-91fc-74b00a2074ea
@Override public Collection<Ball> rolls(Collection<Ball> balls, Collider<Ball>... colliders) { LinkedList<Ball> remain = new LinkedList<Ball>(balls); boolean collisionOccurred; do { collisionOccurred = false; LinkedList<Ball> merged = new LinkedList<Ball>(); while (!remain.isEmpty()) { Ball rolli...
a4a6843d-b816-4aad-8a0c-a073f0447211
public ReuseDownhillCollider(boolean isConsumedKept) { this.isConsumedKept = isConsumedKept; }
1dc69b3b-8ee9-4c1e-8a1f-7073abe6e126
public ReuseDownhillCollider() { this(true); }
da73a8d3-607c-4111-86d6-37346d9e3c4a
@Override public Collection<Ball> rolls(Collection<Ball> balls, Collider<Ball>... colliders) { Collection<Ball> oldBalls = new HashSet<Ball>(balls); Collection<Ball> newBalls = oldBalls; Collection<Ball> consumed = new HashSet<Ball>(); do { Collection<Ball> merged = new LinkedList<Ball>(); for (Ball b...
9424e8b4-dd98-4275-9031-b5c39a895a69
public WaveDownhillCollider() { this(false); }
b377673f-3c94-4b6f-a0ea-8ab25bd55186
public WaveDownhillCollider(boolean isOrderIndependent) { this.isOrderIndependent = isOrderIndependent; }
5e3ba5e1-a435-4a4f-b83c-0e633bc084d9
@Override public Collection<Ball> rolls(Collection<Ball> balls, Collider<Ball>... colliders) { Collection<Ball> result; if (isOrderIndependent) { result = rollsIndependent(balls, colliders); } else { result = rollsDependent(balls, colliders); } return new ReductionDownhillCollider<Ball>().rolls(resu...
8fb5e1a7-cc1b-4081-9c58-5c82ba8e1b1f
public Collection<Ball> rollsDependent(Collection<Ball> balls, Collider<Ball>... colliders) { Collection<Ball> oldBalls = null; Collection<Ball> newBalls = new HashSet<Ball>(balls); do { oldBalls = newBalls; newBalls = new HashSet<Ball>(); for (Ball b1 : oldBalls) { for (Ball b2 : oldBalls) { ...
3ddfbb43-baea-49a1-8a72-e0630169c4c0
public Collection<Ball> rollsIndependent(Collection<Ball> balls, Collider<Ball>... colliders) { Collection<Ball> oldBalls = null; Collection<Ball> newBalls = new HashSet<Ball>(balls); do { oldBalls = newBalls; newBalls = new HashSet<Ball>(); LinkedList<Ball> remaining = new LinkedList<Ball>(oldBalls)...
c58024df-71dd-4cf4-a10f-81dbbed80880
protected abstract <Ball> DownhillCollider<Ball> buildDownhillCollider();
1a89d4f0-7542-4c9a-bbf3-9afe1834967c
public Range(int min, int max) { this.min = min; this.max = max; }
d68b360b-ff68-4eaa-9ce5-ba735cb4d7c6
@Override public boolean equals(Object obj) { if (obj == this) { return true; } else if (obj instanceof Range) { Range r = (Range) obj; return min == r.min && max == r.max; } else { return false; } }
2acce7f2-c146-463d-a356-200ca7823f5e
@Override public int hashCode() { return min + max; }
9db28889-8444-4c73-8ef9-307108a36db3
@Override public String toString() { return Arrays.asList(min, max).toString(); }
afe2fa66-7698-45b4-b24f-1486a2fbf6ec
@Test public void testFullMergingOf4BallsWhateverTheOrder() { Range b1 = new Range(0, 10); Range b2 = new Range(5, 20); Range b3 = new Range(15, 30); Range b4 = new Range(25, 40); Collider<Range> collider = new Collider<Range>() { @Override public boolean areColliding(Range b1, Range b2) { if (b1...
e1977c58-da10-4071-8ca5-45e2ab8fb022
@Override public boolean areColliding(Range b1, Range b2) { if (b1.max < b2.min) { return false; } else if (b1.min > b2.max) { return false; } else { return true; } }
54b6c325-5277-4fc2-bfd4-116c7415cadc
@Override public Range collide(Range b1, Range b2) { return new Range(Math.min(b1.min, b2.min), Math.max(b1.max, b2.max)); }
0c90b57f-dce9-4286-857b-7f89cfc54eca
@Override protected <Ball> DownhillCollider<Ball> buildDownhillCollider() { return new ReuseDownhillCollider<Ball>(false); }
f9d8ce47-eeac-4302-9ecf-c3f5d6d65a6f
@Override protected <Ball> DownhillCollider<Ball> buildDownhillCollider() { return new ReductionDownhillCollider<Ball>(); }
9e212cb5-bd1c-4241-9976-067cfcac87f0
@Override protected <Ball> DownhillCollider<Ball> buildDownhillCollider() { return new WaveDownhillCollider<Ball>(false); }
ecb49221-f324-4e9f-89f1-7bf4339b9b00
public DoubleCell(double input){ this.input=input; }