简体   繁体   English

KornShell(ksh)调度算法(SRT)

[英]KornShell (ksh) Scheduling Algorithms (SRT)

I have been given an assignment to read mock processes from a txt file that looks like this. 我被分配去从看起来像这样的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;

I have to complete two algorithms from the choices of (First come first serve, Shortest Proccess Next, Shortest Remaining Time, Round Robin q=2). 我必须从以下两种选择中完成两种算法(先来先服务,最短过程下一步,最短剩余时间,循环q = 2)。 I need to print out the current time and the process that is running at that time based on whichever two algos I choose. 我需要根据我选择的两个算法,打印出当前时间和当时正在运行的流程。 I have successfully completed the FCFS. 我已经成功完成了FCFS。 My next approach is on SRT, except I am having some serious issues with the logic behind the algorithm. 我的下一个方法是使用SRT,除了算法背后的逻辑存在一些严重问题。

I am currently attempting an iterative approach (posted below) which works to a certain extent (up until current time 9), however I feel it may just be a lucky coincidence. 我目前正在尝试一种迭代方法(在下面发布),该方法在一定程度上有效(直到当前时间9),但是我觉得这可能只是一个幸运的巧合。

Does anyone have any suggestions for this algorithm, or one of the other two. 有没有人对此算法有任何建议,还是其他两个建议之一? I have been on this task for several days now and decided to suck up my pride and post on stack. 我从事这项工作已经有好几天了,并决定吸取我的自尊心,然后发表在堆栈上。

Note: This is my first experience with shell scripting, so my code may be a little messy. 注意:这是我第一次使用Shell脚本,因此我的代码可能有点混乱。 I am still trying to understand KornShell (ksh). 我仍在尝试了解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

The proper output for SRT should read like this.. 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
=================================

I'm still relatively new to stack overflow and was naive to the thoughts and opinions about homework assignments. 我对堆栈溢出还比较陌生,并且对有关家庭作业的想法和观点很幼稚。 I was debating on removing the question, but after reading this post( https://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions ), I decided my question fits the guidelines and therefore is worthy of keeping up. 我正在就删除问题进行辩论,但是在阅读了这篇文章之后( https://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions ),我认为我的问题符合准则因此值得跟上。

I figured out the Shortest remaining time algorithm. 我想出了最短剩余时间算法。 I am thankful that no one answered this question, figuring out the algorithm on my own (with some help from my TA) was worth it. 我很感激没有人回答这个问题,我自己(在我的助教的帮助下)弄清楚算法是值得的。 Therefore, my provided answer will have the basic pseudo logic and no actual code. 因此,我提供的答案将具有基本的伪逻辑,而没有实际的代码。

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

NOTE: This is just about the same help I received from my TA (who wrote the assignment) which is why I feel comfortable posting this answer. 注意:这几乎与我从助教(撰写作业)获得的帮助相同,这就是为什么我愿意发布此答案的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM