a_pramanik

Priority Scheduling

Feb 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 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. int arr[1000], bur[1000], pr[1000], wt[1000], tmp[1000];
  11. vector<gt>v;
  12.  
  13. int main()
  14. {
  15.     int n, i, j,k, x, y;
  16.  
  17.     int min1, min2, mx, st, flg;
  18.  
  19.     struct gt temp;
  20.  
  21.     cout<<"Enter number of processes : ";
  22.     cin>>n;
  23.  
  24.     for(i=1; i<=n; i++)
  25.     {
  26.         printf("Enter burst time, arrival time and priority of process %d : ", i);
  27.         scanf("%d%d%d", &bur[i], &arr[i], &pr[i]);
  28.         tmp[i]=bur[i];
  29.     }
  30.  
  31.     mx = 0;
  32.  
  33.     for(i=1; i<=n; i++)
  34.     {
  35.         if(arr[i]==0)
  36.         {
  37.             if(pr[i]>mx)
  38.             {
  39.                 mx = pr[i];
  40.                 st=i;
  41.             }
  42.         }
  43.     }
  44.  
  45.     memset(wt, 0, sizeof wt);
  46.  
  47.     temp.pro=st;
  48.     temp.time=0;
  49.  
  50.     v.push_back(temp);
  51.  
  52.     int t=1;
  53.     int now = st;
  54.     tmp[now]--;
  55.  
  56.     while(1)
  57.     {
  58.  
  59.         min1=t;
  60.         mx = pr[now];
  61.         for(i=1; i<=n; i++)
  62.         {
  63.             if(i!=now && arr[i]<min1 && tmp[i]>0 && arr[i]<=t && pr[i]>mx)
  64.             {
  65.                 min1=arr[i];
  66.                 mx = pr[i];
  67.                 now=i;
  68.             }
  69.         }
  70.  
  71.         for(i=1; i<=n; i++)
  72.         {
  73.             if(i!=now && tmp[i]>0 && arr[i]<=t)
  74.             {
  75.                 wt[i]++;
  76.             }
  77.         }
  78.  
  79.         temp.pro=now;
  80.         temp.time=t;
  81.         v.push_back(temp);
  82.  
  83.         tmp[now]--;
  84.         t++;
  85.  
  86.         if(tmp[now]==0)
  87.         {
  88.  
  89.             mx = 0;
  90.             min1=t;
  91.             flg=0;
  92.             for(i=1; i<=n; i++)
  93.             {
  94.                 if(arr[i]<=t && tmp[i]>0 && pr[i]>mx && arr[i]<=min1)
  95.                 {
  96.                     flg=1;
  97.                     min1 = arr[i];
  98.                     mx = pr[i];
  99.                     now=i;
  100.                 }
  101.             }
  102.  
  103.             if(flg==0)
  104.             {
  105.                 min1=99999999;
  106.                 mx=0;
  107.                 for(i=1; i<=n; i++)
  108.                 {
  109.                     if(tmp[i]>0 && arr[i]<min1 && pr[i]>mx)
  110.                     {
  111.                         min1=arr[i];
  112.                         mx=pr[i];
  113.                         t=arr[i];
  114.                         now=i;
  115.                     }
  116.                 }
  117.  
  118.             }
  119.  
  120.         }
  121.  
  122.         int koyta=0;
  123.  
  124.         for(i=1; i<=n; i++)
  125.         {
  126.             if(tmp[i]==0)koyta++;
  127.         }
  128.         if(koyta==n)break;
  129.     }
  130.  
  131.  
  132.     printf("\n\t****Individual Waiting time****\n");
  133.     printf("\t  Process            Waiting Time\n");
  134.  
  135.     for(i=1; i<=n; i++)
  136.     {
  137.         printf("\t      %d                 %d\n", i, wt[i]);
  138.     }
  139.     printf("\n");
  140.  
  141.  
  142.  
  143.     double sum = 0.0;
  144.  
  145.     for(i=1; i<=n; i++)
  146.     {
  147.         sum+=(double)wt[i];
  148.     }
  149.     printf("Average waiting time : %.3lf\n", sum/n);
  150.  
  151.  
  152.  
  153.     printf("\n\t\t*****Gantt Chart*****\n\n");
  154.  
  155. //    for(i=0; i<v.size(); i++)
  156. //    {
  157. //        printf("p%d ", v[i].pro);
  158. //    }
  159. //    printf("\n");
  160. //
  161. //    for(i=0; i<v.size(); i++)
  162. //    {
  163. //        if(v[i].time<=9)printf("%d  ", v[i].time);
  164. //        else printf("%d ", v[i].time);
  165. //    }
  166. //    printf("\n");
  167.  
  168. int process, startTime, endTime;
  169.     int cnt=0;
  170.     for(i=0; i<v.size();){
  171.             cnt++;
  172.         process = v[i].pro;
  173.         startTime = v[i].time;
  174.         endTime;
  175.         for(j=i+1; j<v.size(); j++){
  176.             if(v[j].pro!=process){
  177.                 i=j;
  178.                 endTime=v[j-1].time+1;
  179.                 break;
  180.             }
  181.         }
  182.         if(j>=(v.size()-1)){
  183.             endTime=v[v.size()-1].time+1;
  184.             i=v.size();
  185.         }
  186.         printf("%d __[p%d]__ %d  ", startTime, process, endTime);
  187.     }
  188.  
  189.  
  190.      printf("\n");
  191.  
  192. }
  193.  
  194. /*4
  195. 4 0 5
  196. 10 1 1
  197. 2 1 2
  198. 5 2 3*/
Add Comment
Please, Sign In to add comment