Advertisement
JagadeeshUndavalli

SJF NON_Preemtive

May 7th, 2022 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct process
  5. {
  6.   char name[5];
  7.   int burst;
  8. };
  9.  
  10. int
  11. main ()
  12. {
  13.   int size;
  14.   int warr[size + 1];
  15.   int carr[size+1];
  16.   printf ("\nEnter the number of processes  : ");
  17.   scanf ("%d", &size);
  18.   struct process arr[size];
  19.   struct process Process;
  20.   printf ("\nEnter the process name and burst time  : \n");
  21.   for (int i = 0; i < size; i++)
  22.     {
  23.       printf("\t-->\t");
  24.       scanf ("%s", Process.name);
  25.       scanf ("%d", &Process.burst);
  26.       arr[i] = Process;
  27.     }
  28.     int pos,temp;
  29.     int posarr[size+1];
  30.     for(int i=0;i<size;i++){
  31.         posarr[i]=i;
  32.     }
  33.   for(int i=0;i<size;i++)
  34.     {
  35.         pos=i;
  36.         for(int j=i+1;j<size;j++)
  37.         {
  38.             if(arr[j].burst<arr[pos].burst)
  39.             {
  40.                 pos=j;
  41.             }
  42.         }
  43.        
  44.         temp=arr[i].burst;
  45.         arr[i].burst=arr[pos].burst;
  46.         arr[pos].burst=temp;
  47.        
  48.         temp=posarr[i];
  49.         posarr[i]=posarr[pos];
  50.         posarr[pos]=temp;
  51.     }
  52.   float waiting = 0,complete=0;
  53.   warr[0] = 0;
  54.   for(int i=0;i<size;i++){
  55.       warr[i+1] = warr[i]+arr[i].burst;
  56.       waiting += warr[i];
  57.   }
  58.  
  59.   for(int i=0;i<size;i++){
  60.       carr[i] = arr[i].burst + warr[i];
  61.       complete+=carr[i];
  62.   }
  63.   printf("Process\tBurst Time\tWaiting Time\tCompletion Time\n");
  64.   for(int i=0;i<size;i++){
  65.       printf("%s\t%d ms\t\t%d ms\t\t%d ms\n",arr[posarr[i]].name,arr[i].burst,warr[i],carr[i]);
  66.   }
  67.  
  68.    
  69.   printf ("\nAverage Waiting Time : %.2f ms", waiting / size);
  70.  
  71.   printf ("\nAverage Completion Time : %.2f ms\n", complete / size);
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement