a_pramanik

FCFS

Feb 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct gt
  5. {
  6.     int pro;
  7.     int time;
  8. };
  9.  
  10. vector<gt>v;
  11. int arr[1000], bur[1000], wt[1000];
  12.  
  13. int main()
  14. {
  15.     int n, i, j, k, x, y;
  16.  
  17.     printf("Enter number of processes : ");
  18.     cin>>n;
  19.     int arrTemp[1000];
  20.     int burTemp[1000];
  21.     int process[1000];
  22.     for(i=0; i<n; i++){
  23.         printf("Enter burst time and arrival time of p%d : ", i);
  24.         cin>>bur[i]>>arr[i];
  25.         process[i]=i;
  26.         arrTemp[i]=arr[i];
  27.         burTemp[i]=bur[i];
  28.     }
  29.  
  30.     for(i=0; i<n; i++){
  31.         for(j=i+1; j<n; j++){
  32.             if(arrTemp[i]>arrTemp[j]){
  33.                 swap(arrTemp[i], arrTemp[j]);
  34.                 swap(burTemp[i], burTemp[j]);
  35.                 swap(process[i], process[j]);
  36.             }
  37.         }
  38.     }
  39.  
  40.     wt[process[0]]=0;
  41.  
  42.     printf("\n\t\t****Gantt Chart****\n");
  43.     printf("%d__[p%d]__%d  ", arrTemp[0], process[0], arrTemp[0]+burTemp[0]);
  44.     int presentTime=arrTemp[0]+burTemp[0];
  45.  
  46.     double sum=0.0;
  47.  
  48.  
  49.  
  50.     for(i=1; i<n; i++){
  51.         int strt, endd;
  52.  
  53.         if(presentTime>arrTemp[i]){
  54.             wt[i]=presentTime-arrTemp[i];
  55.             strt=presentTime;
  56.             endd=presentTime+burTemp[i];
  57.             presentTime=endd;
  58.         }
  59.         else{
  60.             wt[i]=0;
  61.             strt=arrTemp[i];
  62.             endd=arrTemp[i]+burTemp[i];
  63.             presentTime=endd;
  64.         }
  65.  
  66.         printf("%d__[p%d]__%d   ", strt, process[i], endd);
  67.         sum+=(double)wt[i];
  68.     }
  69.     printf("\n\n");
  70.  
  71.     printf("\t****Individual Waiting time****");
  72.     printf("\n\n Process       Waiting time\n");
  73.  
  74.     for(i=0; i<n; i++){
  75.         printf("    p%d            %d\n", i, wt[i]);
  76.     }
  77.     printf("\n\nAverage of waiting time : %.2lf\n", sum/n);
  78.  
  79.  
  80.  
  81. }
Add Comment
Please, Sign In to add comment