daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Apr 23rd, 2018 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<queue>
  3. using namespace std;
  4. typedef struct process
  5. {
  6.    int pid,arr_time,burst_time,finishing_time,priority;
  7.    float waiting_time,turn_arnd_time;
  8. }process;
  9.  
  10. process Process[10],p1[10],temp;
  11. queue<int> queue1;
  12. int accept(int ch);
  13. void turn_wait(int n);
  14. void display(int n);
  15. void ganttrr(int n);
  16. int main()
  17. {
  18.    int i,n,ts,ch,j,x;
  19.    Process[0].turn_arnd_time=0;
  20.    Process[0].waiting_time=0;
  21.    n=accept(ch);
  22.    ganttrr(n);
  23.    turn_wait(n);
  24.    display(n);
  25.    return 0;
  26. }
  27.  
  28. int accept(int ch) //functions for accepting inputs from the user
  29. {
  30.    int i,n;
  31.    cout<<"\nEnter the no. of process : ";
  32.    cin>>n;
  33.    if(n==0)
  34.    {
  35.       cout<<endl<<"wrong no. of processes entered";
  36.       exit(1);
  37.    }
  38.    cout<<"Provide inputs"<<endl;
  39.    for(i=1;i<=n;i++)
  40.    {
  41.       cout<<endl<<"Arrival time for process P"<<i<<" : ";
  42.       cin>>Process[i].arr_time;
  43.       Process[i].pid=i;
  44.       cout<<endl<<"Burst time for process P"<<i<<" : ";
  45.       cin>>Process[i].burst_time;
  46.       cout<<endl<<endl;
  47.    }
  48.    cout<<endl<<"Table of inputs provided by you : "<<endl<<endl;
  49.    
  50.    cout<<"==============================================================="<<endl;
  51.    cout<<endl<<"Process\tAT\tBT";
  52.    for(i=1;i<=n;i++)
  53.       {
  54.      
  55.       printf("\nP%d\t%d\t%d",Process[i].pid,Process[i].arr_time,Process[i].burst_time);
  56.      
  57.       }
  58.    printf("\n============================================================");
  59.    
  60.    for(i=1;i<=n;i++) // creating dummy array
  61.     {
  62.         p1[i]=Process[i];
  63.     }
  64.    return n;
  65. }
  66.  
  67. void ganttrr(int n)     // Displaying ghant chart
  68. {
  69.    int i,ts,m,nextval,nextarr;
  70.    nextval=Process[1].arr_time;
  71.    cout<<"\nEnter time slice : ";
  72.    cin>>ts;
  73.    
  74.    nextval=p1[1].arr_time;
  75.    cout<<endl<<"\nGantt chart :\n"<<endl;
  76.    cout<<p1[1].arr_time;
  77.    
  78.    for(i=1;i<=n,p1[i].arr_time<=nextval;i++)
  79.    queue1.push(p1[i].pid);
  80.    nextarr=p1[i].arr_time;
  81.    while(!queue1.empty())
  82.    {
  83.       m=queue1.front();
  84.       queue1.pop();
  85.       if(p1[m].burst_time>=ts)
  86.          nextval=nextval+ts;
  87.       else
  88.          nextval=nextval+p1[m].burst_time;
  89.       cout<<"->P"<<p1[m].pid<<"->"<<nextval;
  90.       p1[m].burst_time=p1[m].burst_time-ts;
  91.       if(nextval<nextarr)
  92.       {
  93.          if(p1[m].burst_time>0)
  94.          queue1.push(m);
  95.          if(p1[m].burst_time<=0)
  96.          Process[m].finishing_time=nextval;
  97.       }
  98.       else
  99.       {
  100.          while(i<=n&&p1[i].arr_time<=nextval)
  101.          {
  102.             queue1.push(p1[i].pid);
  103.             i++;
  104.          }
  105.       if(i<=n)
  106.          nextarr=p1[i].arr_time;
  107.       if(p1[m].burst_time>0)
  108.          queue1.push(m);
  109.       if(p1[m].burst_time<=0)
  110.          Process[m].finishing_time=nextval;
  111.       }
  112.    }
  113. }
  114.  
  115. void turn_wait(int n)   //  Calculating turn around time and waiting time
  116. {
  117.    int i;
  118.    for(i=1;i<=n;i++)
  119.    {
  120.       Process[i].turn_arnd_time=Process[i].finishing_time-Process[i].arr_time;
  121.       Process[i].waiting_time=Process[i].turn_arnd_time-Process[i].burst_time;
  122.       Process[0].turn_arnd_time=Process[0].turn_arnd_time+Process[i].turn_arnd_time;
  123.       Process[0].waiting_time=Process[0].waiting_time+Process[i].waiting_time;
  124.    }
  125.    Process[0].turn_arnd_time=Process[0].turn_arnd_time/n;
  126.    Process[0].waiting_time=Process[0].waiting_time/n;
  127. }
  128.  
  129. void display(int n)    // Display result
  130. {
  131.    int i;
  132.    cout<<"\n\n-------------------RESULT----------------------------------\n";
  133.    cout<<"\nProcess\tAT\tBT\tFT\tTAT\tWT";
  134.    
  135.    for(i=1;i<=n;i++)
  136.    {
  137.    cout<<"\nP"<<Process[i].pid<<"\t"<<Process[i].arr_time<<"\t"<<Process[i].burst_time<<"\t"<<Process[i].finishing_time<<"\t"<<Process[i].turn_arnd_time<<"\t"<<Process[i].waiting_time;
  138.    }
  139.    
  140.    cout<<"\n\n-----------------------------------------------------------";
  141.    
  142.    cout<<"\nAverage Turn Around Time: "<<Process[0].turn_arnd_time;
  143.    cout<<"\nAverage Waiting Time: "<<Process[0].waiting_time<<endl;
  144. }
RAW Paste Data
Top