gpt4 book ai didi

algorithm - KornShell (ksh) 调度算法 (SRT)

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:34:20 29 4
gpt4 key购买 nike

我接到了一项任务,要从如下所示的 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
=================================

最佳答案

我对堆栈溢出还比较陌生,对家庭作业的想法和意见也很幼稚。我正在考虑是否删除该问题,但在阅读这篇文章 (https://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions) 后,我认为我的问题符合指南,因此值得跟进。

我想出了最短剩余时间算法。我很庆幸没有人回答这个问题,我自己(在我的助教的帮助下)弄清楚算法是值得的。因此,我提供的答案只有基本的伪逻辑,没有实际代码。

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

注意:这与我从我的助教(编写作业的人)那里得到的帮助几乎相同,这就是为什么我觉得发布这个答案很舒服。

关于algorithm - KornShell (ksh) 调度算法 (SRT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671845/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com