Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,188 changes: 2,173 additions & 1,015 deletions package-lock.json

Large diffs are not rendered by default.

92 changes: 45 additions & 47 deletions src/Process.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
const { SchedulerInterrupt } = require('./constants/index');
const { SchedulerInterrupt } = require("./constants/index");

// A class representation of a process that may be blocking
// or non-blocking. We can specify how much CPU time a process
// needs in order to complete, or we can specify if the process
// is blocking; if so, the amount of blocking time needed is
// randomly determined.
class Process {
constructor(pid, cpuTimeNeeded=null, blocking=false) {
this._pid = pid;
this.queue = null;
this.cpuTimeNeeded = (cpuTimeNeeded !== null) ? cpuTimeNeeded : Math.round(Math.random() * 1000);
this.blockingTimeNeeded = blocking ? Math.round(Math.random() * 100) : 0;
// A bool representing whether this process was toggled from blocking to non-blocking or vice versa
this.stateChanged = false;
constructor(pid, cpuTimeNeeded = null, blocking = false) {
this._pid = pid;
this.queue = null;
this.cpuTimeNeeded =
cpuTimeNeeded !== null ? cpuTimeNeeded : Math.round(Math.random() * 1000);
this.blockingTimeNeeded = blocking ? Math.round(Math.random() * 100) : 0;

this.stateChanged = false;
}

setParentQueue(queue) {
this.queue = queue;
}

isFinished() {
return this.blockingTimeNeeded <= 0 && this.cpuTimeNeeded <= 0;
}

executeProcess(time) {
this.stateChanged = false;
if (!this.blockingTimeNeeded) {
this.cpuTimeNeeded -= time;
this.cpuTimeNeeded = this.cpuTimeNeeded > 0 ? this.cpuTimeNeeded : 0;
} else {
this.queue.emitInterrupt(this, "PROCESS_BLOCKED");
this.stateChanged = true;
}

setParentQueue(queue) {
}

}

isFinished() {

}

// If no blocking time is needed by this process, decrement the amount of
// CPU time it needs by the input time
// If blocking time is needed by this process, move it to the blocking queue
// by emitting the appropriate interrupt
// Make sure the `stateChanged` flag is toggled appropriately
executeProcess(time) {

}

// If this process requires blocking time, decrement the amount of blocking
// time it needs by the input time
// Once it no longer needs to perform any blocking execution, move it to the
// top running queue by emitting the appropriate interrupt
// Make sure the `stateChanged` flag is toggled appropriately
executeBlockingProcess(time) {

}

// Returns this process's stateChanged property
isStateChanged() {
executeBlockingProcess(time) {
this.blockingTimeNeeded -= time;
this.blockingTimeNeeded =
this.blockingTimeNeeded > 0 ? this.blockingTimeNeeded : 0;

if (!this.blockingTimeNeeded) {
this.queue.emitInterrupt(this, "PROCESS_READY");
this.stateChanged = true;
}
}

get pid() {
isStateChanged() {
return this.stateChanged;
}

}
get pid() {
return this._pid;
}

// Private function used for testing; DO NOT MODIFY
_getParentQueue() {
return this.queue;
}
_getParentQueue() {
return this.queue;
}
}

module.exports = Process;
48 changes: 48 additions & 0 deletions src/Process.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { SchedulerInterrupt: SchedulerInterrupt } = require("./constants/index");
class Process {
constructor(pid, cpuTimeNeeded = null, blocking = !1) {
(this._pid = pid),
(this.queue = null),
(this.cpuTimeNeeded =
null !== cpuTimeNeeded
? cpuTimeNeeded
: Math.round(1e3 * Math.random())),
(this.blockingTimeNeeded = blocking
? Math.round(100 * Math.random())
: 0),
(this.stateChanged = !1);
}
setParentQueue(queue) {
this.queue = queue;
}
isFinished() {
return this.blockingTimeNeeded <= 0 && this.cpuTimeNeeded <= 0;
}
executeProcess(time) {
(this.stateChanged = !1),
this.blockingTimeNeeded
? (this.queue.emitInterrupt(this, "PROCESS_BLOCKED"),
(this.stateChanged = !0))
: ((this.cpuTimeNeeded -= time),
(this.cpuTimeNeeded =
this.cpuTimeNeeded > 0 ? this.cpuTimeNeeded : 0));
}
executeBlockingProcess(time) {
(this.blockingTimeNeeded -= time),
(this.blockingTimeNeeded =
this.blockingTimeNeeded > 0 ? this.blockingTimeNeeded : 0),
this.blockingTimeNeeded ||
(this.queue.emitInterrupt(this, "PROCESS_READY"),
(this.stateChanged = !0));
}
isStateChanged() {
return this.stateChanged;
}
get pid() {
return this._pid;
}
_getParentQueue() {
return this.queue;
}
}
module.exports = Process;
130 changes: 73 additions & 57 deletions src/Queue.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,91 @@
const { SchedulerInterrupt } = require('./constants/index');
const { SchedulerInterrupt } = require("./constants/index");

// A class representation of a process queue that may hold either a
// blocking or non-blocking process
class Queue {
constructor(scheduler, quantum, priorityLevel, queueType) {
this.processes = [];
// The queue's priority level; the lower the number, the higher the priority
this.priorityLevel = priorityLevel;
// The queue's parent scheduler
this.scheduler = scheduler;
// The queue's allotted time slice; each process in this queue is executed for this amount of time in total
// This may be done over multiple scheduler iterations
this.quantum = quantum;
// A counter to keep track of how much time the queue has been executing so far
this.quantumClock = 0;
this.queueType = queueType;
}

// Enqueues the given process. Return the enqueue'd process
enqueue(process) {

}

// Dequeues the next process in the queue. Return the dequeue'd process
dequeue() {

}

// Return the least-recently added process without removing it from the list of processes
peek() {

}
constructor(scheduler, quantum, priorityLevel, queueType) {
this.processes = [];

isEmpty() {
this.priorityLevel = priorityLevel;

}
this.scheduler = scheduler;

getPriorityLevel() {
this.quantum = quantum;

}
this.quantumClock = 0;
this.queueType = queueType;
}

getQueueType() {
enqueue(process) {
process.setParentQueue(this);
this.processes.push(process);
return process;
}

}
dequeue() {
return this.processes.shift();
}

// Manages a process's execution for the given amount of time
// Processes that have had their states changed should not be affected
// Once a process has received the alloted time, it needs to be dequeue'd and
// then handled accordingly, depending on whether it has finished executing or not
manageTimeSlice(currentProcess, time) {
peek() {
return this.processes[0];
}

}
isEmpty() {
return this.processes.length === 0;
}

// Execute the next non-blocking process (assuming this is a CPU queue)
// This method should call `manageTimeSlice` as well as execute the next running process
doCPUWork(time) {
getPriorityLevel() {
return this.priorityLevel;
}

}

// Execute the next blocking process (assuming this is the blocking queue)
// This method should call `manageTimeSlice` as well as execute the next blocking process
doBlockingWork(time) {
getQueueType() {
return this.queueType;
}

manageTimeSlice(currentProcess, time) {
if (currentProcess.isStateChanged()) {
this.quantumClock = 0;
return;
}

// The queue's interrupt handler for notifying when a process needs to be moved to a different queue
// Should handle PROCESS_BLOCKED and PROCESS_READY interrupts
// The process also needs to be removed from the queue
emitInterrupt(source, interrupt) {

this.quantumClock += time;
if (this.quantumClock >= this.quantum) {
this.quantumClock = 0;
this.dequeue();
if (!currentProcess.isFinished())
this.scheduler.handleInterrupt(
this,
currentProcess,
SchedulerInterrupt.LOWER_PRIORITY
);
}
}

doCPUWork(time) {
const process = this.peek();
process.executeProcess(time);
this.manageTimeSlice(process, time);
}

doBlockingWork(time) {
const process = this.peek();
process.executeBlockingProcess(time);
this.manageTimeSlice(process, time);
}

emitInterrupt(source, interrupt) {
const index = this.processes.indexOf(source);
this.processes.splice(index, 1);
if (interrupt === SchedulerInterrupt.PROCESS_BLOCKED)
this.scheduler.handleInterrupt(
this,
source,
SchedulerInterrupt.PROCESS_BLOCKED
);
else if (interrupt === SchedulerInterrupt.PROCESS_READY)
this.scheduler.handleInterrupt(
this,
source,
SchedulerInterrupt.PROCESS_READY
);
}
}

module.exports = Queue;
68 changes: 68 additions & 0 deletions src/Queue.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { SchedulerInterrupt: SchedulerInterrupt } = require("./constants/index");
class Queue {
constructor(scheduler, quantum, priorityLevel, queueType) {
(this.processes = []),
(this.priorityLevel = priorityLevel),
(this.scheduler = scheduler),
(this.quantum = quantum),
(this.quantumClock = 0),
(this.queueType = queueType);
}
enqueue(process) {
return process.setParentQueue(this), this.processes.push(process), process;
}
dequeue() {
return this.processes.shift();
}
peek() {
return this.processes[0];
}
isEmpty() {
return 0 === this.processes.length;
}
getPriorityLevel() {
return this.priorityLevel;
}
getQueueType() {
return this.queueType;
}
manageTimeSlice(currentProcess, time) {
currentProcess.isStateChanged()
? (this.quantumClock = 0)
: ((this.quantumClock += time),
this.quantumClock >= this.quantum &&
((this.quantumClock = 0),
this.dequeue(),
currentProcess.isFinished() ||
this.scheduler.handleInterrupt(
this,
currentProcess,
SchedulerInterrupt.LOWER_PRIORITY
)));
}
doCPUWork(time) {
const process = this.peek();
process.executeProcess(time), this.manageTimeSlice(process, time);
}
doBlockingWork(time) {
const process = this.peek();
process.executeBlockingProcess(time), this.manageTimeSlice(process, time);
}
emitInterrupt(source, interrupt) {
const index = this.processes.indexOf(source);
this.processes.splice(index, 1),
interrupt === SchedulerInterrupt.PROCESS_BLOCKED
? this.scheduler.handleInterrupt(
this,
source,
SchedulerInterrupt.PROCESS_BLOCKED
)
: interrupt === SchedulerInterrupt.PROCESS_READY &&
this.scheduler.handleInterrupt(
this,
source,
SchedulerInterrupt.PROCESS_READY
);
}
}
module.exports = Queue;
Loading