From 7537288dfd1ff08585b916ea57e647489703a68b Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 18:26:01 -0400 Subject: [PATCH 01/14] set parent and is finished --- src/Process.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Process.js b/src/Process.js index 159a307..38aa022 100644 --- a/src/Process.js +++ b/src/Process.js @@ -16,11 +16,14 @@ class Process { } setParentQueue(queue) { - + this.queue = queue } isFinished() { - + if (this.cpuTimeNeeded <= 0) { + return true + } + return false; } // If no blocking time is needed by this process, decrement the amount of From 27bbaf5a7b110ac41277e4fcab98ab2a11d16c92 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 18:38:07 -0400 Subject: [PATCH 02/14] execute process --- src/Process.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Process.js b/src/Process.js index 38aa022..66db963 100644 --- a/src/Process.js +++ b/src/Process.js @@ -32,7 +32,12 @@ class Process { // by emitting the appropriate interrupt // Make sure the `stateChanged` flag is toggled appropriately executeProcess(time) { - + if (this.blockingTimeNeeded == 0) { + this.cpuTimeNeeded -= time; + } else { + this.queue = QueueType.BLOCKING_QUEUE; + this.stateChanged = !this.stateChanged; + } } // If this process requires blocking time, decrement the amount of blocking From d4d2dc67323262a63aa55ed578108fda7336cd55 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 18:43:09 -0400 Subject: [PATCH 03/14] finish process --- src/Process.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Process.js b/src/Process.js index 66db963..f42034f 100644 --- a/src/Process.js +++ b/src/Process.js @@ -46,16 +46,21 @@ class Process { // top running queue by emitting the appropriate interrupt // Make sure the `stateChanged` flag is toggled appropriately executeBlockingProcess(time) { - + if (this.blockingTimeNeeded > 0) { + this.blockingTimeNeeded -= timr + } else { + this.queue = QueueType.CPU_QUEUE; + this.stateChanged = !this.stateChanged; + } } // Returns this process's stateChanged property isStateChanged() { - + return this.stateChanged; } get pid() { - + return this._pid; } // Private function used for testing; DO NOT MODIFY From baa9619615d61ee6a3d48d3eb9b5081f1bca94b4 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 19:02:07 -0400 Subject: [PATCH 04/14] process fixes --- src/Process.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Process.js b/src/Process.js index f42034f..0c6e21f 100644 --- a/src/Process.js +++ b/src/Process.js @@ -20,7 +20,7 @@ class Process { } isFinished() { - if (this.cpuTimeNeeded <= 0) { + if (this.cpuTimeNeeded <= 0 && this.blockingTimeNeeded <= 0) { return true } return false; @@ -33,9 +33,12 @@ class Process { // Make sure the `stateChanged` flag is toggled appropriately executeProcess(time) { if (this.blockingTimeNeeded == 0) { + if (time > this.cpuTimeNeeded) { + time = this.cpuTimeNeeded; + } this.cpuTimeNeeded -= time; } else { - this.queue = QueueType.BLOCKING_QUEUE; + this.queue.emitInterrupt(this, SchedulerInterrupt.PROCESS_BLOCKED); this.stateChanged = !this.stateChanged; } } @@ -47,7 +50,7 @@ class Process { // Make sure the `stateChanged` flag is toggled appropriately executeBlockingProcess(time) { if (this.blockingTimeNeeded > 0) { - this.blockingTimeNeeded -= timr + this.blockingTimeNeeded -= time; } else { this.queue = QueueType.CPU_QUEUE; this.stateChanged = !this.stateChanged; From 033c29096e261d07fcfdf5d8b37838e2d6f3dc41 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 19:27:55 -0400 Subject: [PATCH 05/14] some queue methods --- src/Process.js | 5 ++++- src/Queue.js | 16 +++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Process.js b/src/Process.js index 0c6e21f..afb51f7 100644 --- a/src/Process.js +++ b/src/Process.js @@ -49,10 +49,13 @@ class Process { // top running queue by emitting the appropriate interrupt // Make sure the `stateChanged` flag is toggled appropriately executeBlockingProcess(time) { + if (time > this.blockingTimeNeeded) { + time = this.blockingTimeNeeded; + } if (this.blockingTimeNeeded > 0) { this.blockingTimeNeeded -= time; } else { - this.queue = QueueType.CPU_QUEUE; + this.queue.emitInterrupt(this, SchedulerInterrupt.PROCESS_READY); this.stateChanged = !this.stateChanged; } } diff --git a/src/Queue.js b/src/Queue.js index 7b45fdf..6d1c446 100644 --- a/src/Queue.js +++ b/src/Queue.js @@ -19,12 +19,14 @@ 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.pop(0); } // Return the least-recently added process without removing it from the list of processes @@ -33,15 +35,19 @@ class Queue { } isEmpty() { - + if (this.processes.length <= 0) { + return true; + } else { + return false; + } } getPriorityLevel() { - + return this.priorityLevel; } getQueueType() { - + return this.queueType; } // Manages a process's execution for the given amount of time From d2a33f265a02ead2668b72c377cd4157baccc041 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 19:36:48 -0400 Subject: [PATCH 06/14] peek queue method --- src/Queue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Queue.js b/src/Queue.js index 6d1c446..3feb4a1 100644 --- a/src/Queue.js +++ b/src/Queue.js @@ -31,7 +31,7 @@ class Queue { // Return the least-recently added process without removing it from the list of processes peek() { - + return this.processes[0]; } isEmpty() { From 3ae89c6a37055f01015070464edaef2a2073dfe9 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 15 Aug 2018 19:43:58 -0400 Subject: [PATCH 07/14] fix dequeue to remove first element --- src/Queue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Queue.js b/src/Queue.js index 3feb4a1..f9681a4 100644 --- a/src/Queue.js +++ b/src/Queue.js @@ -26,7 +26,7 @@ class Queue { // Dequeues the next process in the queue. Return the dequeue'd process dequeue() { - return this.processes.pop(0); + return this.processes.shift(); } // Return the least-recently added process without removing it from the list of processes From 10e07d76b0b1e78765225d4d8546c996d2ab621f Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 14:21:56 -0400 Subject: [PATCH 08/14] queue complete --- src/Queue.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Queue.js b/src/Queue.js index f9681a4..43848b2 100644 --- a/src/Queue.js +++ b/src/Queue.js @@ -55,26 +55,44 @@ class Queue { // 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.stateChanged == true) { + 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); + this.scheduler.handleInterrupt(this, source, interrupt); } } From 3786e7e1d9555ea20440feb07d6e216bc8bb0671 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 14:35:31 -0400 Subject: [PATCH 09/14] add new process and all queues empty queue methods --- src/Scheduler.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Scheduler.js b/src/Scheduler.js index 3bca870..1d38c52 100644 --- a/src/Scheduler.js +++ b/src/Scheduler.js @@ -28,11 +28,20 @@ class Scheduler { } allQueuesEmpty() { - + let empty = true; + for (let i = 0; i < PRIORITY_LEVELS; i++) { + if (this.runningQueues[i].isEmpty() === false) { + empty = false; + } + } + if (this.blockingQueue.isEmpty() === false) { + empty = false; + } + return empty; } addNewProcess(process) { - + this.runningQueues[0].enqueue(process); } // The scheduler's interrupt handler that receives a queue, a process, and an interrupt string constant From e9b4186f4f76c9dc8fb44f147e7e3a4ac7aba283 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 14:48:07 -0400 Subject: [PATCH 10/14] handle interrupt for process blocked --- src/Scheduler.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Scheduler.js b/src/Scheduler.js index 1d38c52..b5b2c72 100644 --- a/src/Scheduler.js +++ b/src/Scheduler.js @@ -47,7 +47,15 @@ class Scheduler { // 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": + queue.dequeue(process); + process.setParentQueue(this.blockingQueue); + this.blockingQueue.enqueue(process); + break; + case "PROCESS_READY": + break; + } } // Private function used for testing; DO NOT MODIFY From af19e907766c741cd585586f65347a36b467609c Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 14:57:08 -0400 Subject: [PATCH 11/14] handle interrupt process ready --- src/Scheduler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scheduler.js b/src/Scheduler.js index b5b2c72..7c8557d 100644 --- a/src/Scheduler.js +++ b/src/Scheduler.js @@ -50,10 +50,10 @@ class Scheduler { switch(interrupt) { case "PROCESS_BLOCKED": queue.dequeue(process); - process.setParentQueue(this.blockingQueue); this.blockingQueue.enqueue(process); break; case "PROCESS_READY": + this.addNewProcess(process); break; } } From 017b6d2e718726ae0cdbe11fe3bb91557c7b9daa Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 15:16:25 -0400 Subject: [PATCH 12/14] complete handle interrupt method --- src/Scheduler.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Scheduler.js b/src/Scheduler.js index 7c8557d..eac8ff5 100644 --- a/src/Scheduler.js +++ b/src/Scheduler.js @@ -55,6 +55,16 @@ class Scheduler { case "PROCESS_READY": this.addNewProcess(process); break; + case "LOWER_PRIORITY": + const currentQueue = this.runningQueues.indexOf(queue); + queue.dequeue(process); + if (this.runningQueues[currentQueue + 1] && queue.getQueueType() === 'CPU_QUEUE') { + this.runningQueues[currentQueue + 1].enqueue(process); + } else { + queue.dequeue(process); + queue.enqueue(process); + } + break; } } From 95a822e65506d372bcd6de143a093b79110d13dc Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 17:29:43 -0400 Subject: [PATCH 13/14] bug fixes --- src/Process.js | 20 ++++++++++++++------ src/Scheduler.js | 26 ++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Process.js b/src/Process.js index afb51f7..41e9f1d 100644 --- a/src/Process.js +++ b/src/Process.js @@ -49,15 +49,23 @@ class Process { // top running queue by emitting the appropriate interrupt // Make sure the `stateChanged` flag is toggled appropriately executeBlockingProcess(time) { - if (time > this.blockingTimeNeeded) { - time = this.blockingTimeNeeded; + this.blockingTimeNeeded -= time; + if (this.blockingTimeNeeded < 0) { + this.blockingTimeNeeded = 0; } - if (this.blockingTimeNeeded > 0) { - this.blockingTimeNeeded -= time; - } else { + if (this.blockingTimeNeeded === 0) { this.queue.emitInterrupt(this, SchedulerInterrupt.PROCESS_READY); - this.stateChanged = !this.stateChanged; + this.stateChanged = !this.stateChanged } + // if (time > this.blockingTimeNeeded) { + // time = this.blockingTimeNeeded; + // } + // if (this.blockingTimeNeeded > 0) { + // this.blockingTimeNeeded -= time; + // } else { + // this.queue.emitInterrupt(this, SchedulerInterrupt.PROCESS_READY); + // this.stateChanged = !this.stateChanged; + // } } // Returns this process's stateChanged property diff --git a/src/Scheduler.js b/src/Scheduler.js index eac8ff5..cfbac66 100644 --- a/src/Scheduler.js +++ b/src/Scheduler.js @@ -24,7 +24,30 @@ 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() === false) { + const time = Date.now() + const forTimeSlice = time - this.clock; + if (this.blockingQueue.isEmpty() === false) { + this.blockingQueue.doBlockingWork(forTimeSlice); + } else { + this.runningQueues.forEach(cpuQ =>{ + if (!cpuQ.isEmpty()) { + cpuQ.doCPUWork(forTimeSlice); + } + }) + } + this.clock = time; + } + // while (this.blockingQueue) { + // this.blockingQueue.doBlockingWork(this.clock - Date.now()); + // this.clock = Date.now(); + // } + // while (this.runningQueues) { + // for (let i = 0; i < PRIORITY_LEVELS; i++) { + // this.runningQueues[i].doCPUWork(this.clock - Date.now()); + // this.clock = Date.now(); + // } + // } } allQueuesEmpty() { @@ -49,7 +72,6 @@ class Scheduler { handleInterrupt(queue, process, interrupt) { switch(interrupt) { case "PROCESS_BLOCKED": - queue.dequeue(process); this.blockingQueue.enqueue(process); break; case "PROCESS_READY": From 09a0b34f9ff972fb52e3fc5bb04b2005027669aa Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 16 Aug 2018 17:36:30 -0400 Subject: [PATCH 14/14] all tests passing --- src/Scheduler.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Scheduler.js b/src/Scheduler.js index cfbac66..ebfa1bc 100644 --- a/src/Scheduler.js +++ b/src/Scheduler.js @@ -79,11 +79,9 @@ class Scheduler { break; case "LOWER_PRIORITY": const currentQueue = this.runningQueues.indexOf(queue); - queue.dequeue(process); if (this.runningQueues[currentQueue + 1] && queue.getQueueType() === 'CPU_QUEUE') { this.runningQueues[currentQueue + 1].enqueue(process); } else { - queue.dequeue(process); queue.enqueue(process); } break;