我已经获得了一个从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
=================================
答案 0 :(得分:3)
我仍然相对较新的堆栈溢出,并且对于家庭作业的想法和意见天真。我正在讨论删除这个问题,但在阅读这篇文章后(https://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions),我认为我的问题符合指南,因此值得跟上。
我找出了最短剩余时间算法。我很感激没有人回答这个问题,我自己搞定算法(在TA的帮助下)是值得的。因此,我提供的答案将具有基本的伪逻辑,而没有实际的代码。
shortest = the first process read from the input(assuming it has already arrived)
while there are still processes to be run
process = next process (out of processes that have not completed yet)
if (process arrival time <= currentTime) #process arrived
if (process execution time < shortest execution time)
shortest = process
注意:这与我从TA(编写作业)收到的帮助大致相同,这就是为什么我觉得这个答案很舒服。