Skip to content

Commit 5464557

Browse files
Add Timelog for 8 algorithms
Round Robin is left.
1 parent b2fbc04 commit 5464557

File tree

1 file changed

+113
-43
lines changed

1 file changed

+113
-43
lines changed

script.js

+113-43
Original file line numberDiff line numberDiff line change
@@ -843,68 +843,115 @@ function ljf(input, utility, output) {
843843
}
844844
}
845845
function srjf(input, utility, output) {
846+
let currentTimeLog = new TimeLog();
847+
currentTimeLog.remain = input.processId;
848+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
849+
currentTimeLog.time++;
846850
while (utility.done.some((element) => element == false)) {
847-
let candidates = input.processId.filter(
848-
(element) => utility.done[element] == false && utility.returnTime[element] <= utility.currentTime
849-
);
850-
if (candidates.length == 0) {
851-
utility.currentTime++;
852-
output.schedule.push([-1, 1]);
853-
} else {
851+
let candidatesRemain = currentTimeLog.remain.filter((element) => input.arrivalTime[element] <= utility.currentTime);
852+
let candidatesBlock = currentTimeLog.block.filter((element) => utility.returnTime[element] <= utility.currentTime);
853+
let candidates = candidatesRemain.concat(candidatesBlock);
854+
candidates.forEach(element => {
855+
moveElement(element, currentTimeLog.remain, currentTimeLog.ready);
856+
moveElement(element, currentTimeLog.block, currentTimeLog.ready);
857+
});
858+
candidates = candidates.concat(currentTimeLog.ready);
859+
if (candidates.length > 0) {
860+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
861+
}
862+
let found = -1;
863+
if (candidates.length > 0) {
854864
candidates.sort((a, b) => utility.remainingBurstTime[a] - utility.remainingBurstTime[b]);
855-
let found = candidates[0];
865+
found = candidates[0];
866+
moveElement(found, currentTimeLog.ready, currentTimeLog.running);
867+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
856868
if (utility.start[found] == false) {
857869
utility.start[found] = true;
858870
output.responseTime[found] = utility.currentTime - input.arrivalTime[found];
859871
}
872+
}
873+
utility.currentTime++;
874+
currentTimeLog.time++;
875+
if (found != -1) {
860876
output.schedule.push([found + 1, 1]);
861-
utility.currentTime++;
862-
utility.remainingBurstTime[found]--;
863877
utility.remainingProcessTime[found][utility.currentProcessIndex[found]]--;
864-
if (utility.remainingProcessTime[found][utility.currentProcessIndex[found]] == 0) {
878+
utility.remainingBurstTime[found]--;
879+
if (utility.remainingProcessTime[found][utility.currentProcessIndex[found]] == 0) { //if current process completed
865880
utility.currentProcessIndex[found]++;
866-
if (utility.currentProcessIndex[found] == input.processTimeLength[found]) {
881+
if (utility.currentProcessIndex[found] == input.processTimeLength[found]) { //if last burst time
867882
utility.done[found] = true;
868883
output.completionTime[found] = utility.currentTime;
869-
} else {
884+
moveElement(found, currentTimeLog.running, currentTimeLog.terminate);
885+
} else { //if not last
870886
utility.returnTime[found] = utility.currentTime + input.processTime[found][utility.currentProcessIndex[found]];
871887
utility.currentProcessIndex[found]++;
888+
moveElement(found, currentTimeLog.running, currentTimeLog.block);
872889
}
873890
}
891+
else {
892+
moveElement(found, currentTimeLog.running, currentTimeLog.ready);
893+
}
874894
}
895+
else {
896+
output.schedule.push([-1, 1]);
897+
}
898+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
875899
}
876900
}
877901
function lrjf(input, utility, output) {
902+
let currentTimeLog = new TimeLog();
903+
currentTimeLog.remain = input.processId;
904+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
905+
currentTimeLog.time++;
878906
while (utility.done.some((element) => element == false)) {
879-
let candidates = input.processId.filter(
880-
(element) => utility.done[element] == false && utility.returnTime[element] <= utility.currentTime
881-
);
882-
if (candidates.length == 0) {
883-
utility.currentTime++;
884-
output.schedule.push([-1, 1]);
885-
} else {
907+
let candidatesRemain = currentTimeLog.remain.filter((element) => input.arrivalTime[element] <= utility.currentTime);
908+
let candidatesBlock = currentTimeLog.block.filter((element) => utility.returnTime[element] <= utility.currentTime);
909+
let candidates = candidatesRemain.concat(candidatesBlock);
910+
candidates.forEach(element => {
911+
moveElement(element, currentTimeLog.remain, currentTimeLog.ready);
912+
moveElement(element, currentTimeLog.block, currentTimeLog.ready);
913+
});
914+
candidates = candidates.concat(currentTimeLog.ready);
915+
if (candidates.length > 0) {
916+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
917+
}
918+
let found = -1;
919+
if (candidates.length > 0) {
886920
candidates.sort((a, b) => utility.remainingBurstTime[b] - utility.remainingBurstTime[a]);
887-
let found = candidates[0];
921+
found = candidates[0];
922+
moveElement(found, currentTimeLog.ready, currentTimeLog.running);
923+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
888924
if (utility.start[found] == false) {
889925
utility.start[found] = true;
890926
output.responseTime[found] = utility.currentTime - input.arrivalTime[found];
891927
}
928+
}
929+
utility.currentTime++;
930+
currentTimeLog.time++;
931+
if (found != -1) {
892932
output.schedule.push([found + 1, 1]);
893-
utility.currentTime++;
894-
utility.remainingBurstTime[found]--;
895933
utility.remainingProcessTime[found][utility.currentProcessIndex[found]]--;
896-
if (utility.remainingProcessTime[found][utility.currentProcessIndex[found]] == 0) {
934+
utility.remainingBurstTime[found]--;
935+
if (utility.remainingProcessTime[found][utility.currentProcessIndex[found]] == 0) { //if current process completed
897936
utility.currentProcessIndex[found]++;
898-
if (utility.currentProcessIndex[found] == input.processTimeLength[found]) {
937+
if (utility.currentProcessIndex[found] == input.processTimeLength[found]) { //if last burst time
899938
utility.done[found] = true;
900939
output.completionTime[found] = utility.currentTime;
901-
} else {
902-
utility.returnTime[found] =
903-
utility.currentTime + input.processTime[found][utility.currentProcessIndex[found]];
940+
moveElement(found, currentTimeLog.running, currentTimeLog.terminate);
941+
} else { //if not last
942+
utility.returnTime[found] = utility.currentTime + input.processTime[found][utility.currentProcessIndex[found]];
904943
utility.currentProcessIndex[found]++;
944+
moveElement(found, currentTimeLog.running, currentTimeLog.block);
905945
}
906946
}
947+
else {
948+
moveElement(found, currentTimeLog.running, currentTimeLog.ready);
949+
}
907950
}
951+
else {
952+
output.schedule.push([-1, 1]);
953+
}
954+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
908955
}
909956
}
910957
function rr(input, utility, output) {
@@ -1045,36 +1092,59 @@ function pnp(input, utility, output) {
10451092
}
10461093
}
10471094
function pp(input, utility, output) {
1095+
let currentTimeLog = new TimeLog();
1096+
currentTimeLog.remain = input.processId;
1097+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
1098+
currentTimeLog.time++;
10481099
while (utility.done.some((element) => element == false)) {
1049-
let candidates = input.processId.filter(
1050-
(element) => utility.done[element] == false && utility.returnTime[element] <= utility.currentTime
1051-
);
1052-
if (candidates.length == 0) {
1053-
utility.currentTime++;
1054-
output.schedule.push([-1, 1]);
1055-
} else {
1100+
let candidatesRemain = currentTimeLog.remain.filter((element) => input.arrivalTime[element] <= utility.currentTime);
1101+
let candidatesBlock = currentTimeLog.block.filter((element) => utility.returnTime[element] <= utility.currentTime);
1102+
let candidates = candidatesRemain.concat(candidatesBlock);
1103+
candidates.forEach(element => {
1104+
moveElement(element, currentTimeLog.remain, currentTimeLog.ready);
1105+
moveElement(element, currentTimeLog.block, currentTimeLog.ready);
1106+
});
1107+
candidates = candidates.concat(currentTimeLog.ready);
1108+
if (candidates.length > 0) {
1109+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
1110+
}
1111+
let found = -1;
1112+
if (candidates.length > 0) {
10561113
candidates.sort((a, b) => priorityPreference * (input.priority[a] - input.priority[b]));
1057-
let found = candidates[0];
1114+
found = candidates[0];
1115+
moveElement(found, currentTimeLog.ready, currentTimeLog.running);
1116+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
10581117
if (utility.start[found] == false) {
10591118
utility.start[found] = true;
10601119
output.responseTime[found] = utility.currentTime - input.arrivalTime[found];
10611120
}
1121+
}
1122+
utility.currentTime++;
1123+
currentTimeLog.time++;
1124+
if (found != -1) {
10621125
output.schedule.push([found + 1, 1]);
1063-
utility.currentTime++;
1064-
utility.remainingBurstTime[found]--;
10651126
utility.remainingProcessTime[found][utility.currentProcessIndex[found]]--;
1066-
if (utility.remainingProcessTime[found][utility.currentProcessIndex[found]] == 0) {
1127+
utility.remainingBurstTime[found]--;
1128+
if (utility.remainingProcessTime[found][utility.currentProcessIndex[found]] == 0) { //if current process completed
10671129
utility.currentProcessIndex[found]++;
1068-
if (utility.currentProcessIndex[found] == input.processTimeLength[found]) {
1130+
if (utility.currentProcessIndex[found] == input.processTimeLength[found]) { //if last burst time
10691131
utility.done[found] = true;
10701132
output.completionTime[found] = utility.currentTime;
1071-
} else {
1072-
utility.returnTime[found] =
1073-
utility.currentTime + input.processTime[found][utility.currentProcessIndex[found]];
1133+
moveElement(found, currentTimeLog.running, currentTimeLog.terminate);
1134+
} else { //if not last
1135+
utility.returnTime[found] = utility.currentTime + input.processTime[found][utility.currentProcessIndex[found]];
10741136
utility.currentProcessIndex[found]++;
1137+
moveElement(found, currentTimeLog.running, currentTimeLog.block);
10751138
}
10761139
}
1140+
else {
1141+
moveElement(found, currentTimeLog.running, currentTimeLog.ready);
1142+
}
10771143
}
1144+
else {
1145+
output.schedule.push([-1, 1]);
1146+
}
1147+
output.timeLog.push(JSON.parse(JSON.stringify(currentTimeLog)));
10781148
}
10791149
}
10801150
function hrrn(input, utility, output) {

0 commit comments

Comments
 (0)