Advertisement
Shailrshah

Priority Scheduling with and without Aging

Aug 4th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 12
  3. #define AF 25
  4.  
  5. struct process{
  6.     char pid;
  7.     int at;
  8.     int et;
  9.     int pri;
  10.     int ct;
  11.     int wt;
  12.     int cwt;
  13.     int tt;
  14. }pro[N] =   {
  15.                 {'A', 0, 10, 5},
  16.                 {'B', 0, 3, 1},
  17.                 {'C', 2, 2, 3},
  18.                 {'D', 2, 1, 6},
  19.                 {'E', 2, 4, 3},
  20.                 {'F', 10, 5, 2},
  21.                 {'G', 10, 7, 3},
  22.                 {'H', 15, 8, 1},
  23.                 {'I', 20, 10, 2},
  24.                 {'J', 30, 9, 4},
  25.                 {'K', 40, 3, 5},
  26.                 {'L', 40, 2, 4}
  27.             };
  28. int done[N] = {0}, time = 0, sumWT = 0, sumTT = 0;
  29. int calcNow(){
  30.     int i, now;
  31.     for(i = 0; i < N; i++) if(!done[i] && time >= pro[i].at){now = i; break;}
  32.     for(; i < N; i++){
  33.         if(done[i] || time < pro[i].at) continue;
  34.         if(pro[i].pri < pro[now].pri) now = i;
  35.         else if(pro[i].et == pro[now].et && pro[i].et < pro[now].et) now = i;
  36.         else if((pro[i].et == pro[now].et) && (i < now)) now = i;
  37.     }
  38.     if(time < pro[now].at) return -1;
  39.     return now;
  40. }
  41. void execute(int now){
  42.     done[now] = 1;
  43.     printf("%c executes from %d to %d\n", pro[now].pid, time, time+pro[now].et);
  44.     pro[now].ct = time + pro[now].et;
  45.     time = pro[now].ct;
  46.     pro[now].wt = pro[now].ct - (pro[now].at + pro[now].et);
  47.     pro[now].tt = pro[now].ct - pro[now].at;
  48.     sumWT += pro[now].wt;
  49.     sumTT += pro[now].tt;
  50. }
  51. void updatePriorities(){
  52.     int i;
  53.     for(i = 0; i < N; i++){
  54.         if(!done[i] && time >= pro[i].at) pro[i].cwt = time - pro[i].at;
  55.         if(pro[i].cwt >= 25 && pro[i].pri){
  56.             pro[i].cwt = 0;
  57.             pro[i].pri -= 1;
  58.             printf("Priority of %c is now %d\n", pro[i].pid, pro[i].pri);
  59.         }
  60.     }
  61. }
  62. void printTable(){
  63.     int i;
  64.     printf("\tpid\tAT\tET\tPRI\tCT\tWT\tTT\n");
  65.     for(i = 0; i < N; i++) printf("\t%c\t%d\t%d\t%d\t%d\t%d\t%d\n", pro[i].pid, pro[i].at, pro[i].et, pro[i].pri, pro[i].ct, pro[i].wt, pro[i].tt);
  66.     printf("\nThe average waiting time is %.2f\nThe average turn around time is %.2f\n", sumWT/(float)N, sumTT/(float)N);
  67. }
  68. int main(){
  69.     int i, now = 0;
  70.     while(1){
  71.         now = calcNow();
  72.         if(now < 0){time++; continue;}
  73.         execute(now);
  74.         updatePriorities(); //commenting out this line gives Priority Scheduling without Aging
  75.         for(i = 0; i < N; i++) if(!done[i]) break;
  76.         if(i == N) break;
  77.     }
  78.     printTable();
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement