JagadeeshUndavalli

Priority_non_preemtive

May 21st, 2022 (edited)
162
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.  
  3. #include <string.h>
  4.  
  5. struct process {
  6.     char name[5];
  7.     int burst;
  8.     int prior;
  9. };
  10.  
  11. int main() {
  12.     int size;
  13.     int warr[size + 1];
  14.     int carr[size + 1];
  15.     printf("\nEnter the number of processes  : ");
  16.     scanf("%d", & size);
  17.     struct process arr[size];
  18.     printf("\nEnter the process name, burst time, priority  : \n");
  19.     for (int i = 0; i < size; i++) {
  20.         printf("\t-->\t");
  21.         scanf("%s", arr[i].name);
  22.         scanf("%d", &arr[i].burst);
  23.         scanf("%d", &arr[i].prior);
  24.     }
  25.  
  26.     int pos;
  27.     struct process temp;
  28.     int posarr[size + 1];
  29.     for (int i = 0; i < size; i++) {
  30.         posarr[i] = i;
  31.     }
  32.     for (int i = 0; i < size; i++) {
  33.         pos = i;
  34.         for (int j = i + 1; j < size; j++) {
  35.             if (arr[j].prior < arr[pos].prior) {
  36.                 pos = j;
  37.             }
  38.         }
  39.  
  40.         temp = arr[i];
  41.         arr[i] = arr[pos];
  42.         arr[pos] = temp;
  43.     }
  44.     float waiting = 0, complete = 0;
  45.     warr[0] = 0;
  46.     for (int i = 0; i < size; i++) {
  47.         warr[i + 1] = warr[i] + arr[i].burst;
  48.         waiting += warr[i];
  49.     }
  50.  
  51.     for (int i = 0; i < size; i++) {
  52.         carr[i] = arr[i].burst + warr[i];
  53.         complete += carr[i];
  54.     }
  55.     printf("\n");
  56.     printf("------------------------------------------------------------------------------------------------------------------\n");
  57.     printf("Process\t\tBurst Time\t\tPriority\t\tWaiting Time\t\tCompletion Time\n");
  58.     for (int i = 0; i < size; i++) {
  59.         printf("%s\t\t%d ms\t\t\t%d\t\t\t%d ms\t\t\t%d ms\n", arr[i].name,arr[i].burst,arr[i].prior, warr[i], carr[i]);
  60.     }
  61.     printf("------------------------------------------------------------------------------------------------------------------");
  62.  
  63.     printf("\nAverage Waiting Time : %.2f ms\n", waiting / size);
  64.  
  65.     printf("\nAverage Completion Time : %.2f ms\n", complete / size);
  66. }
Add Comment
Please, Sign In to add comment