Advertisement
coffeebeforecode

priority_scheduling.c

Dec 6th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. //#define INT_MAX 2147483647
  6.  
  7. typedef struct job{
  8.     int id;
  9.     int at;
  10.     int bt;
  11.     int st;
  12.     int ct;
  13.     int wt;
  14.     int tat;
  15.     int pr;
  16. } job;
  17.  
  18. void sortJobsAT(job jobs[], int n){
  19.     int i, j;
  20.     job t;
  21.     for(i = 0; i < n-1; i++){
  22.         for(j = i+1; j < n; j++){
  23.             if (jobs[i].at > jobs[j].at){
  24.                 t = jobs[i];
  25.                 jobs[i] = jobs[j];
  26.                 jobs[j] = t;
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. void calc(job jobs[], int n){
  33.     int *rt = (int*)malloc(sizeof(int)*n);
  34.     int i, j;
  35.     for(i = 0; i < n; i++){
  36.         rt[i] = jobs[i].bt;
  37.     }
  38.    
  39.     int complete = 0, t = 0, minm = INT_MAX;
  40.     int mostImportant = 0, finish_time;
  41.     bool check = false;
  42.    
  43.     while (complete != n){
  44.         for(j = 0; j < n; j++){
  45.             if ((jobs[j].at > t) || ((rt[j] == 0))){
  46.                continue;
  47.             }
  48.             if (jobs[j].pr < minm){
  49.                 minm = jobs[j].pr;
  50.                 mostImportant = j;
  51.                 check = true;
  52.             }
  53.         }
  54.        
  55.         if (check == false){
  56.             t++;    
  57.             continue; //no process in ready queue
  58.         }
  59.        
  60.         //printf("\nt=%d, Process %d", t, mostImportant);
  61.  
  62.         if (rt[mostImportant] == jobs[mostImportant].bt){
  63.             jobs[mostImportant].st = t;
  64.         }
  65.  
  66.         rt[mostImportant]--;
  67.        
  68.         if (rt[mostImportant] == 0){
  69.             complete++;
  70.             check = false;
  71.             minm = INT_MAX;
  72.            
  73.             jobs[mostImportant].ct = t+1;
  74.             jobs[mostImportant].wt = jobs[mostImportant].ct - jobs[mostImportant].bt - jobs[mostImportant].at;
  75.            
  76.             jobs[mostImportant].wt = (jobs[mostImportant].wt < 0 ) ? 0 : jobs[mostImportant].wt;
  77.            
  78.             jobs[mostImportant].tat = jobs[mostImportant].ct - jobs[mostImportant].at;
  79.         }
  80.        
  81.         t++;
  82.     }
  83. }
  84.  
  85. void solve(){
  86.     int k;
  87.     printf("\n\tPriority Scheduling Algorithm\n");
  88.     printf("\n1. Same Arrival time (0)");
  89.     printf("\n2. Different Arrival Times\n>>");
  90.     scanf("%d", &k);
  91.     if (!(k == 1) && !(k == 2)){
  92.         printf("\nINVALID");
  93.         exit(0);
  94.     }
  95.     int n;
  96.     printf("Enter number of processes: ");
  97.     scanf("%d", &n);
  98.     job *jobs;
  99.     jobs = (job*)malloc(sizeof(job)*n);
  100.     int i;
  101.     for(i=0; i < n; i++){
  102.         jobs[i].id = i;
  103.         if (k != 1){
  104.             printf("Enter Arrival Time of Process %d: ", i);
  105.             scanf("%d", &jobs[i].at);
  106.         }
  107.         else{
  108.             jobs[i].at = 0;
  109.         }
  110.         printf("Enter Burst Time of Process %d: ", i);
  111.         scanf("%d", &jobs[i].bt);
  112.         printf("Enter Priority of Process %d: ", i);
  113.         scanf("%d", &jobs[i].pr);
  114.         printf("\n");
  115.     }
  116.    
  117.     sortJobsAT(jobs, n);
  118.     calc(jobs, n);
  119.    
  120.     printf("\nID\tAT\tBT\tPR\tST\tCT\tWT\tTAT");
  121.     for (i = 0; i < n; i++){
  122.  
  123.         printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", jobs[i].id, jobs[i].at, jobs[i].bt, jobs[i].pr, jobs[i].st, jobs[i].ct, jobs[i].wt, jobs[i].tat);
  124.     }
  125.    
  126.     float avgWT = 0;
  127.     float avgTAT = 0;
  128.  
  129.     for(i = 0; i < n; i++){
  130.         avgWT += jobs[i].wt;
  131.         avgTAT += jobs[i].tat;
  132.     }
  133.     avgWT /= n;
  134.     avgTAT /= n;
  135.     printf("\nAverage Waiting Time = %f\nAverage Turnaround Time = %f", avgWT, avgTAT);
  136. }
  137.  
  138.  
  139. int main(){
  140.     solve();
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement