我被分配了从一个看起来像这样的 txt 文件中读取模拟进程的任务。
ID: 35; Arrival_Time: 0; Total_Exec_Time: 4;
ID: 65; Arrival_Time: 2; Total_Exec_Time: 6;
ID: 10; Arrival_Time: 3; Total_Exec_Time: 3;
ID: 124; Arrival_Time: 5; Total_Exec_Time: 5;
ID: 182; Arrival_Time: 6; Total_Exec_Time: 2;
我必须从(先到先得,最短过程接下来,最短剩余时间,循环 q=2)的选择中完成两个算法。我需要根据我选择的两种算法打印出当前时间和当时正在运行的进程。我已经成功完成了 FCFS。我的下一个方法是在 SRT 上,除了算法背后的逻辑存在一些严重问题。
我目前正在尝试一种迭代方法(在下面发布),它在一定程度上有效(直到当前时间 9),但我觉得这可能只是一个幸运的巧合。
有没有人对此算法或其他两个算法之一有任何建议。我已经从事这项任务好几天了,并决定收起我的骄傲并在堆栈上发帖。
注意:这是我第一次使用 shell 脚本,所以我的代码可能有点乱。我仍在尝试了解 KornShell (ksh)。
file="/path/to/file.txt"
IFS=': \;'
i=0
while read -r f1 f2 f3 f4 f5 f6
do
integer id[i]="$f2" #id array
integer at[i]="$f4" #arrival time array
integer et[i]="$f6" #exec time array
integer rt[i]=0 #run time so far
integer current[i]=i
((i++))
done <"$file"
integer curr_index=0
integer currTime=0
let totalProcesses=${#at[@]}
let totalProcesses=totalProcesses-1
let totalRunTime=0
for x in ${et[@]}; do
let totalRunTime+=$x
done
scheduleTask () {
currTime=$1
for y in ${current[@]}; do
if (( rt[$y] < et[$y] )); then
#if the program is not finished, keep going
if (( at[$y] < $currTime )); then
#if the program is in que, keep going
let diff=et[$y]-rt[$y]#not currently using
let currDiff=et[$curr_index]-rt[$curr_index] #not currently using
if (( et[$y] <= et[$curr_index] )); then #is this broken?
curr_index=$y
fi
fi
else
echo "${id[$y]} RAN ${rt[$y]} out of ${et[$y]} seconds"
unset current[$y]
fi
done
}
for (( i = 0; i < $totalRunTime; i++ )); do
echo "================================="
scheduleTask $i
((rt[$curr_index]++))
print "\t\tcurrent time: $i"
print "\t\t\tcurrent process: ${id[$curr_index]}"
echo "================================="
done
SRT 的正确输出应该是这样的。
=================================
current time: 0
current process: 35
=================================
=================================
current time: 1
current process: 35
=================================
=================================
current time: 2
current process: 35
=================================
=================================
current time: 3
current process: 35
=================================
=================================
current time: 4
current process: 10
=================================
=================================
current time: 5
current process: 10
=================================
=================================
current time: 6
current process: 10
=================================
=================================
current time: 7
current process: 182
=================================
=================================
current time: 8
current process: 182
=================================
=================================
current time: 9
current process: 124
=================================
=================================
current time: 10
current process: 124
=================================
=================================
current time: 11
current process: 124
=================================
=================================
current time: 12
current process: 124
=================================
=================================
current time: 13
current process: 124
=================================
=================================
current time: 14
current process: 65
=================================
=================================
current time: 15
current process: 65
=================================
=================================
current time: 16
current process: 65
=================================
=================================
current time: 17
current process: 65
=================================
=================================
current time: 18
current process: 65
=================================
=================================
current time: 19
current process: 65
=================================