Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopeixoto committed May 2, 2019
0 parents commit 9507274
Show file tree
Hide file tree
Showing 12 changed files with 561 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Burnout/Burnout.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
float speed = 1.0;
int seed = 0;
int minimumBurstTime = 5;
int maximumBurstTime = 15;
int quantum = 5;

Scheduler scheduler;
ShortestJobFirstScheduler shortestJobFirstScheduler;
RoundRobinScheduler roundRobinScheduler;

boolean pause = false;

void setup() {
size(800, 600);

ProcessBuilder processBuilder = new RandomProcessBuilder(seed, minimumBurstTime, maximumBurstTime);

ArrayList<Device> devices = new ArrayList();

devices.add(new CPUDevice());
devices.add(new CPUDevice());

shortestJobFirstScheduler = new ShortestJobFirstScheduler(processBuilder, devices);
roundRobinScheduler = new RoundRobinScheduler(processBuilder, devices, quantum);

scheduler = shortestJobFirstScheduler;
}

void draw() {
background(255);

delay(int(1000 / speed));

if (!pause)
scheduler.update();

scheduler.draw();

drawMainScreen();
}

void drawMainScreen() {
pushStyle();

fill(0);

textSize(32);
textAlign(CENTER, CENTER);
text("Burnout Scheduler", width / 2.0, 50.0);

textSize(12);
textAlign(CENTER, CENTER);
text("Copyright © 2019, Danilo Peixoto and Débora Bacelar. All rights reserved.", width / 2.0, height - 20.0);

textSize(12);
textAlign(CENTER, CENTER);
text("OPTIONS", width / 2.0, 440.0);

textSize(12);
textAlign(LEFT, CENTER);
text("1 - Add CPU Device\n" +
"2 - Remove CPU Device\n" +
"3 - Clear and Release Processes\n" +
"4 - Switch Scheduler Algorithm (Non-Preemptive Shortest Job First or Round Robin)\n" +
"5 - Pause/Resume", 10.0, 500.0);

popStyle();
}

void keyPressed() {
if (key == '1') {
ArrayList<Device> devices = scheduler.getDevices();
devices.add(new CPUDevice());
}
else if (key == '2') {
ArrayList<Device> devices = scheduler.getDevices();
int size = devices.size();

if (size > 0)
devices.remove(size - 1);
}
if (key == '3') {
AbstractCollection<Process> collection = scheduler.getCollection();
collection.clear();

for (Device device : scheduler.getDevices())
device.release();
}
else if (key == '4') {
scheduler = scheduler.getType() == SchedulerType.ShortestJobFirst ? roundRobinScheduler : shortestJobFirstScheduler;

AbstractCollection<Process> collection = scheduler.getCollection();
collection.clear();

for (Device device : scheduler.getDevices())
device.release();
}
else if (key == '5')
pause = !pause;
}
65 changes: 65 additions & 0 deletions Burnout/CPUDevice.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
final class CPUDevice extends Device {
public CPUDevice() {
super();
}

@Override
public void commit(Process process) {
this.process = process;
execute();
}
@Override
public void release() {
this.process = null;
}
@Override
public void execute() {
process.execute();
}

@Override
public boolean isIdle() {
if (process == null)
return true;

return process.getExecutionTime() >= process.getBurstTime();
}
@Override
public boolean isInterrupted(int quantum) {
if (process == null)
return true;

return process.getExecutionTime() % quantum == 0;
}

@Override
public void draw() {
float padding = 20;
float size = 60 + padding;
float textSize = 16;

pushStyle();

noFill();

stroke(0);
strokeWeight(2.0);

rect(anchorPoint.x, anchorPoint.y, size, size, 8.0);

fill(0);
textSize(textSize);
textAlign(CENTER, CENTER);
text("CPU" + String.valueOf(id), anchorPoint.x + size / 2.0, anchorPoint.y + size + textSize);

popStyle();

float halfPadding = padding / 2.0;
PVector position = new PVector(anchorPoint.x + halfPadding, anchorPoint.y + halfPadding);

if (process != null) {
process.setAnchorPoint(position);
process.draw();
}
}
}
24 changes: 24 additions & 0 deletions Burnout/Device.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
static int globalCPUCount = 0;

abstract class Device extends Shape {
protected int id;
protected Process process;

public Device() {
id = globalCPUCount++;
}

public int getID() {
return id;
}
public Process getProcess() {
return process;
}

public abstract void commit(Process process);
public abstract void release();
public abstract void execute();

public abstract boolean isIdle();
public abstract boolean isInterrupted(int quantum);
}
67 changes: 67 additions & 0 deletions Burnout/Process.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
final class Process extends Shape implements Comparable<Process> {
private int pid;
private int burstTime;
private int executionTime;

public Process() {
super();
}
public Process(int pid, int burstTime) {
super();
this.pid = pid;
this.burstTime = burstTime;
}

public void setPID(int pid) {
this.pid = pid;
}
public void setBurstTime(int burstTime) {
this.burstTime = burstTime;
}
public int getPID() {
return pid;
}
public int getBurstTime() {
return burstTime;
}
public int getExecutionTime() {
return executionTime;
}

public void execute() {
executionTime++;
}

@Override
public int compareTo(Process other) {
if (burstTime < other.burstTime)
return -1;
else if (burstTime > other.burstTime)
return 1;

return 0;
}

@Override
public void draw() {
float size = 60;
float textSize = 16;

pushStyle();

noStroke();

fill(220, 220, 220);
rect(anchorPoint.x, anchorPoint.y, size, size, 8.0);

fill(0, 255, 125);
rect(anchorPoint.x, anchorPoint.y, size * executionTime / burstTime, size, 8.0);

fill(0);
textSize(textSize);
textAlign(CENTER, CENTER);
text("PID" + String.valueOf(pid) + "\n" + "BT" + String.valueOf(burstTime), anchorPoint.x + size / 2.0, anchorPoint.y + size / 2.0);

popStyle();
}
}
7 changes: 7 additions & 0 deletions Burnout/ProcessBuilder.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
static int globalProcessCount = 0;

abstract class ProcessBuilder {
public ProcessBuilder() {}

public abstract Process next();
}
43 changes: 43 additions & 0 deletions Burnout/RandomProcessGenerator.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Random;

final class RandomProcessBuilder extends ProcessBuilder {
private int seed;
private int minimumBurstTime;
private int maximumBurstTime;
private Random random;

public RandomProcessBuilder() {
random = new Random();
}
public RandomProcessBuilder(int seed, int minimumBurstTime, int maximumBurstTime) {
this();
this.seed = seed;
this.minimumBurstTime = minimumBurstTime;
this.maximumBurstTime = maximumBurstTime;
}

public void setSeed(int seed) {
this.seed = seed;
random.setSeed(seed);
}
public void setMinimumBurstTime(int minimumBurstTime) {
this.minimumBurstTime = minimumBurstTime;
}
public void setMaximumBurstTime(int maximumBurstTime) {
this.maximumBurstTime = maximumBurstTime;
}
public int getSeed() {
return seed;
}
public int getMinimumBurstTime() {
return minimumBurstTime;
}
public int getMaximumBurstTime() {
return maximumBurstTime;
}

@Override
public Process next() {
return new Process(globalProcessCount++, int(minimumBurstTime + (maximumBurstTime - minimumBurstTime) * random.nextFloat()));
}
}
61 changes: 61 additions & 0 deletions Burnout/RoundRobinScheduler.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.LinkedList;

final class RoundRobinScheduler extends Scheduler {
private int quantum;

public RoundRobinScheduler() {
super(SchedulerType.RoundRobin);
collection = new LinkedList<Process>();
}
public RoundRobinScheduler(ProcessBuilder processBuilder, ArrayList<Device> devices, int quantum) {
super(processBuilder, devices, SchedulerType.RoundRobin);
this.quantum = quantum;
collection = new LinkedList<Process>();
}

public void setQuantum(int quantum) {
this.quantum = quantum;
}
public int getQuantum() {
return quantum;
}

@Override
protected void update() {
enqueue(processBuilder.next());

for (Device device : devices) {
if (device.isInterrupted(quantum)) {
if (!device.isIdle())
enqueue(device.getProcess());

device.release();

if (hasNext())
device.commit(dispatch());
}
else {
if (device.isIdle()) {
device.release();

if (hasNext())
device.commit(dispatch());
}

device.execute();
}
}
}
@Override
protected void enqueue(Process process) {
collection.add(process);
}
@Override
protected Process dispatch() {
return ((LinkedList<Process>)collection).poll();
}
@Override
protected boolean hasNext() {
return !collection.isEmpty();
}
}
Loading

0 comments on commit 9507274

Please sign in to comment.