8

我被分配了从一个看起来像这样的 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
=================================
4

1 回答 1

3

我对堆栈溢出还比较陌生,对家庭作业的想法和意见还很幼稚。我正在讨论删除这个问题,但在阅读了这篇文章(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

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

于 2012-12-06T22:40:36.460 回答