我正在尝试使用javascript模拟os的最短作业优先技术,给定作业/进程,到达时间和突发时间:
查看此链接:Shorest Job First - 概念。现在我有一个数组:
var arr = [
{
"job": "j4",
"at": 0,
"bt": 8
},
{
"job": "j2",
"at": 2,
"bt": 4
},
{
"job": "j3",
"at": 2,
"bt": 5
},
{
"job": "j5",
"at": 6,
"bt": 4
},
{
"job": "j1",
"at": 8,
"bt": 3
}
];
我想创建一个包含 processes 对象的新数组。.
例如
[
{"job": "j4", "range" : "0-2"},
{"job": "j2", "range" : "2-6"},
{"job": "j5", "range" : "6-10"},
{"job": "j1", "range" : "10-13"},
{"job": "j3", "range" : "13-18"},
{"job": "j4", "range" : "18-24"}
]
所以我尝试这样做,但我超级卡住了,离我想要实现的目标还很远。
for(i = 0; i < arr.length; i++) {
var temp = [];
for(j = arr[i].at; j < arr[i].bt; j++) {
var clone = arr.slice(0);
var arrived = clone.splice(0, i).filter(function( obj ) {
return obj.at == j;
});
var shorter = arr[i];
for(k = 0; k < arrived.length; k++) {
if(arrived[k].bt < arr[i].bt) {
shorter = arrived[k];
arr[i].bt - (j - arr[i].at);
}
}
if(shorter != arr[i]) {
j = arr[i].bt;
}
}
}
如果解决,编辑 用实际值替换新数组值
Time Process
0 j4(8)
1
2 j4(6), j2`(4), j3(5)
...
6 j4(6), j3(5), j5(4) ::: j2 done
7
8 j5`(2), j4(6), j3(5), j1(3)
9
10 ::: j5 done
...
13 ::: j1 done
...
18 ::: j3 done
...
24 ::: j4 done
so the new array will be
[
{"job": "j4", "range" : "0-2"},
{"job": "j2", "range" : "2-6"},
{"job": "j5", "range" : "6-10"},
{"job": "j1", "range" : "10-13"},
{"job": "j3", "range" : "13-18"},
{"job": "j4", "range" : "18-24"}
]