manthanthakker40

OS-HRRN

Sep 24th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 8
  3. struct process{
  4.     char pid;
  5.     int at;
  6.     int et;
  7.     float pri;
  8.     int rem;
  9.     int done;
  10.     int ct;
  11.     int wt;
  12.     int tt;
  13. }pro[N] =   { //{'pid', at, et},
  14.         {'A', 0, 10},
  15.         {'B', 0, 5},
  16.         {'C', 0, 2},
  17.         {'D', 0, 4},
  18.         {'E', 10, 8},
  19.         {'F', 10, 16},
  20.         {'G', 10, 6},
  21.         {'H', 20, 3},
  22.  
  23.         };
  24. void init(){ //initially, all processes are unfinished and their remaining and execution times are same.
  25.     int i;
  26.     for(i=0; i< N; i++){
  27.     pro[i].done=0;
  28.     pro[i].rem=pro[i].et;
  29.     }
  30. }
  31. int nextProc(int time){
  32.     int i, send=0;
  33.     for(i = 0; i < N; i++)
  34.     if(pro[i].done==0) break; //find first process that's not completed.
  35.     if(i==N) return -1;
  36.     send=i; //send will be returned at the end of function
  37.     for(; i < N; i++){
  38.     if(pro[i].done || pro[i].at > time) continue; //if the process is completed, skip it
  39.     if(time==0||time==10||time==20)
  40.         pro[i].pri=((float)(time-pro[i].at)/(float)pro[i].rem)+1; //calculated at t=0,10,20...
  41.     if (pro[i].pri > pro[send].pri) send = i; //process with highest response time will be returned
  42.     else if(pro[i].pri == pro[send].pri && pro[i].et < pro[send].et) send = i; //compare execution time
  43.     else if(pro[i].et == pro[send].et && pro[i].at < pro[send].at) send = i; //compare arrival times
  44.     else if(pro[i].at == pro[send].at && pro[i].pid < pro[send].pid) send = i; //compare pids
  45.     }
  46.     if(time==0||time==10||time==20){
  47.     printf("\n\nAt t=%d, the processes already arrived but not completed are:-\n", time);
  48.         for(i=0; i < N; i++)
  49.         if(pro[i].done || pro[i].at > time) continue;
  50.         else printf("%c(Pri: %.3f)",pro[i].pid, pro[i].pri);
  51.     printf("\n\n");
  52.     }
  53.     return send;
  54. }
  55. void printResult(int sumWT, int sumTT){
  56.     int i;
  57.     printf("\tpid\tAT\tET\tCT\tWT\tTT\n");
  58.     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);
  59.     printf("\nThe average waiting time is %.2f\nThe average turn around time is %.2f\n", sumWT/(float)N, sumTT/(float)N);
  60. }
  61. int main(){
  62.     int i, time = 0, sumWT = 0, sumTT = 0,  curr;
  63.     init();
  64.     while(1){
  65.     curr = nextProc(time); //process to be executed
  66.     if(curr == -1) break; //if all processes have completed, print the result
  67.     printf("At time=%d, %c gets CPU.(RR: %.3f, remaining: %d)\n", time, pro[curr].pid, pro[curr].pri, pro[curr].rem);
  68.     pro[curr].rem--; //subtract one time unit from the process's remaining time
  69.     time++;
  70.     if(pro[curr].rem == 0){
  71.         pro[curr].done=1; //if remaining time is 0, the process is completed.
  72.         pro[curr].ct = time; //completed time is the time when process execution is completed
  73.         pro[curr].tt = pro[curr].ct - pro[curr].at;
  74.         pro[curr].wt = pro[curr].tt - pro[curr].et;
  75.         printf("\n%c has finished executing at %d\n\n", pro[curr].pid, time);
  76.         sumWT += pro[curr].wt;
  77.         sumTT += pro[curr].tt;
  78.     }
  79.     }
  80.     printResult(sumWT, sumTT);
  81.     getch();
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment