manthanthakker40

OS-non preemptive priority scheduling

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