Advertisement
Shailrshah

Shortest Job First

Aug 2nd, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 12
  3.  
  4. struct process{
  5.     char pid;
  6.     int at;
  7.     int et;
  8.     int ct;
  9.     int wt;
  10.     int tt;
  11. }pro[N] =   { //{'pid', at, et},
  12.                 {'A', 0, 10},
  13.                 {'B', 0, 3},
  14.                 {'C', 2, 2},
  15.                 {'D', 2, 1},
  16.                 {'E', 2, 4},
  17.                 {'F', 10, 5},
  18.                 {'G', 10, 7},
  19.                 {'H', 15, 8},
  20.                 {'I', 20, 10},
  21.                 {'J', 30, 9},
  22.                 {'K', 40, 3},
  23.                 {'L', 40, 2}
  24.             };
  25.  
  26. int main(){
  27.     int i, time = 0, sumWT = 0, sumTT = 0, sj = 0, done[N] = {0};
  28.  
  29.     while(1){
  30.         for(i = 0; i < N; i++) if(!done[i] && time >= pro[i].at){sj = i; break;} //select first unexecuted process
  31.         if(i == N) break; //Exit loop if all processes are done
  32.         for(++i;i < N; i++){
  33.             if(done[i] || time < pro[i].at) continue; //ignore unarrived & completed processes
  34.             if(pro[i].et < pro[sj].et) sj = i; //if the execution time is better, make it sj
  35.             else if((pro[i].et == pro[sj].et) && (pro[i].at < pro[sj].at)) sj = i; //if et is same, see at
  36.             else if((pro[i].at == pro[sj].at) && (pro[i].pid < pro[sj].pid)) sj = i; //if at is same, see pid
  37.         }
  38.         if(time < pro[sj].at) {time++; continue;}//idle time
  39.         done[sj] = 1;//sj has been executed
  40.         pro[sj].ct = time + pro[sj].et;
  41.         time = pro[sj].ct; //update time
  42.         pro[sj].wt = pro[sj].ct - (pro[sj].at + pro[sj].et);
  43.         pro[sj].tt = pro[sj].ct - pro[sj].at;
  44.         sumWT += pro[sj].wt;
  45.         sumTT += pro[sj].tt;
  46.     }
  47.    
  48.     printf("\tpid\tAT\tET\tCT\tWT\tTT\n");
  49.     for(i = 0; i < N; i++) printf("\t%c\t%d\t%d\t%d\t%d\t%d\n", pro[i].pid, pro[i].at, pro[i].et, pro[i].ct, pro[i].wt, pro[i].tt);
  50.     printf("\nThe average waiting time is %.2f\nThe average turn around time is %.2f\n", sumWT/(float)N, sumTT/(float)N);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement