Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KornShell (ksh) Scheduling Algorithms (SRT)

I have been given an assignment to read mock processes from a txt file that looks like this.

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). 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. My next approach is on SRT, except I am having some serious issues with the logic behind the algorithm.

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.

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. I am still trying to understand 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..

=================================
        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
=================================
like image 344
Alex Naspo Avatar asked Dec 02 '12 17:12

Alex Naspo


1 Answers

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.

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.

like image 139
Alex Naspo Avatar answered Nov 15 '22 04:11

Alex Naspo