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
30 changes: 23 additions & 7 deletions src/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Process {
}

setParentQueue(queue) {

this.queue = queue;
}

isFinished() {

return this.cpuTimeNeeded === 0 && this.blockingTimeNeeded === 0;
}

// If no blocking time is needed by this process, decrement the amount of
Expand All @@ -29,7 +29,17 @@ class Process {
// by emitting the appropriate interrupt
// Make sure the `stateChanged` flag is toggled appropriately
executeProcess(time) {

this.stateChanged = false; // I was getting wonky errors before I added this line
if (this.blockingTimeNeeded === 0) {
this.cpuTimeNeeded -=time;
if (this.cpuTimeNeeded < 0) {
this.cpuTimeNeeded = 0;
}
}
else {
this.queue.emitInterrupt(this, SchedulerInterrupt.PROCESS_BLOCKED);
this.stateChanged = true;
}
}

// If this process requires blocking time, decrement the amount of blocking
Expand All @@ -38,16 +48,22 @@ class Process {
// top running queue by emitting the appropriate interrupt
// Make sure the `stateChanged` flag is toggled appropriately
executeBlockingProcess(time) {

this.blockingTimeNeeded -= time;
if (this.blockingTimeNeeded < 0 ) {
this.blockingTimeNeeded = 0;
}
if (this.blockingTimeNeeded === 0) {
this.queue.emitInterrupt(this, SchedulerInterrupt.PROCESS_READY);
this.stateChanged = true;
}
}

// Returns this process's stateChanged property
isStateChanged() {

return this.stateChanged;
}

get pid() {

return this._pid;
}

// Private function used for testing; DO NOT MODIFY
Expand Down
45 changes: 33 additions & 12 deletions src/Queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,77 @@ class Queue {

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

this.processes.push(process);
process.setParentQueue(this);
return process;
}

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

return this.processes.shift();
}

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

peek() {
return this.processes[0];
}

isEmpty() {

return this.processes.length === 0;
}

getPriorityLevel() {

return this.priorityLevel;
}

getQueueType() {

return this.queueType;
}

// 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) {

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

// 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) {

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

// 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) {

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

// 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) {

const sourceIndex = this.processes.indexOf(source);
this.processes.splice(sourceIndex, 1); // remove 1 element at sourceIndex
this.scheduler.handleInterrupt(this, source, interrupt); // found the format for passing arguments in the scheduler file
}
}

Expand Down
55 changes: 52 additions & 3 deletions src/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const {
// A class representing the scheduler
// It holds a single blocking queue for blocking processes and three running queues
// for non-blocking processes

class Scheduler {
constructor() {
this.clock = Date.now();
this.blockingQueue = new Queue(this, 50, 0, QueueType.BLOCKING_QUEUE);
this.runningQueues = [];
// Initialize all the CPU running queues

for (let i = 0; i < PRIORITY_LEVELS; i++) {
this.runningQueues[i] = new Queue(this, 10 + i * 20, i, QueueType.CPU_QUEUE);
}
Expand All @@ -24,21 +25,69 @@ class Scheduler {
// On every iteration of the scheduler, if the blocking queue is not empty, blocking work
// should be done. Once the blocking work has been done, perform some CPU work in the same iteration.
run() {
while (!this.allQueuesEmpty()) {
const current = Date.now();
const worktime = current - this.clock;

if(!this.blockingQueue.isEmpty()) {
this.blockingQueue.doBlockingWork(worktime);
}
else {
for (let i = 0; i < PRIORITY_LEVELS; i++) {
if (!this.runningQueues[i].isEmpty()) {
this.runningQueues[i].doCPUWork(worktime);
}
}
}
if (this.allQueuesEmpty()) {
break;
}
this.clock = Date.now();
}
}

allQueuesEmpty() {

allQueuesEmpty() {
return (
this.runningQueues.every(queue => queue.isEmpty()) &&
this.blockingQueue.isEmpty()
)
}

addNewProcess(process) {

addNewProcess(process) {
this.runningQueues[0].enqueue(process);
}


// The scheduler's interrupt handler that receives a queue, a process, and an interrupt string constant
// Should handle PROCESS_BLOCKED, PROCESS_READY, and LOWER_PRIORITY interrupts.
handleInterrupt(queue, process, interrupt) {
switch(interrupt) {
case "PROCESS_BLOCKED":
this.blockingQueue.enqueue(process);
break;

case "PROCESS_READY":
this.addNewProcess(process);
break;

case "LOWER_PRIORITY":
if(queue.getQueueType() === "BLOCKING_QUEUE") {
queue.enqueue(process);
break;
}
const priority = queue.getPriorityLevel();
if (priority === PRIORITY_LEVELS - 1) {
queue.enqueue(process);
break;
}
// otherwise, add to back of next lowest priority queue
this.runningQueues[priority + 1].enqueue(process);
break;
default:
break;
}
}

// Private function used for testing; DO NOT MODIFY
Expand Down
Loading