Advertisement
BSIT_21

Computer Scheduling [Gelacio]/[Pulan]

Jul 31st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6.     struct procs
  7.     {
  8.         int pName =0;
  9.         int arrTime = 0;
  10.         int bTime = 0;
  11.         int wTime=0;
  12.         int tTime=0;
  13.         int eTime=0;
  14.         int sTime=0;
  15.         int iTime=0;
  16.         int prio=0;
  17.     };
  18.  
  19. void print_gantt_chart(procs P[], int numProc);
  20.  
  21. int main(procs P[])
  22. {
  23.  
  24.     int numProc = 0;
  25.     int choice = 0;
  26.  
  27. top:
  28.    
  29.     cout<< "Welcome to CPU Scheduling Algorithm, please pick an option.\n[1]First Come First Serve\n[2]Shortest Job First\n[3]Priority"<<endl;
  30.     cout<<"\nOption: ";
  31.     cin>>choice;
  32.     cin.ignore();
  33.     while(!cin)
  34.     {
  35.         cout<<"Invalid Input, Please try again\n";
  36.         cin.clear();
  37.         cin.ignore();
  38.         system("pause");
  39.         system("cls");
  40.         goto top;
  41.     }
  42.    
  43.     if(choice==1)
  44.     {
  45.         cout<<"\nRedirecting to FCFS"<<endl;
  46.         system("pause");
  47.         system("cls");
  48.         cout<<" ======================"<<endl;
  49.         cout<<"|First Come First Serve|"<<endl;
  50.         cout<<" ======================"<<endl;
  51.     }
  52.    
  53.     else if(choice==2)
  54.     {
  55.         cout<<"\nRedirecting to SJF"<<endl;
  56.         system("pause");
  57.         system("cls");
  58.         cout<<" =================="<<endl;
  59.         cout<<"|Shortest Job First|"<<endl;
  60.         cout<<" =================="<<endl;
  61.     }
  62.    
  63.     else if(choice==3)
  64.     {
  65.         cout<<"\nRedirecting to Prio"<<endl;
  66.         system("pause");
  67.         system("cls");
  68.         cout<<" ========"<<endl;
  69.         cout<<"|Priority|"<<endl;
  70.         cout<<" ========"<<endl;
  71.     }
  72.    
  73.     else
  74.     {
  75.         cout<<"\nInvalid Input, Please try again"<<endl;
  76.         system("pause");
  77.         system("cls");
  78.         goto top;
  79.     }
  80.    
  81. proc:
  82.    
  83.     cout << "\nEnter number of processes: ";
  84.     cin>>numProc;
  85.     while(!cin)
  86.     {
  87.         cout<<"Invalid\nEnter number of processes: ";
  88.         cin.clear();
  89.         cin.ignore();
  90.         cin>>numProc;
  91.         cin.ignore();
  92.     }
  93.     if (numProc<=10)
  94.     {
  95.        
  96.         procs P[numProc];
  97.        
  98.         for (int i=0; i<numProc;i++)
  99.         {
  100.             P[i].pName = i+1;
  101.             cout << "Process #: "<<P[i].pName<<endl;
  102.             cout << "Arrival Time: ";
  103.             cin >> P[i].arrTime;
  104.             while(!cin)
  105.             {
  106.                 cout<<"Invalid\nArrival Time: ";
  107.                 cin.clear();
  108.                 cin.ignore();
  109.                 cin>>P[i].arrTime;
  110.                 cin.ignore();
  111.             }
  112.            
  113.             cout << "Burst Time: ";
  114.             cin >> P[i].bTime;  
  115.             while(!cin)
  116.             {
  117.                 cout<<"Invalid\nBurst Time: ";
  118.                 cin.clear();
  119.                 cin.ignore();
  120.                 cin>>P[i].bTime;
  121.                 cin.ignore();
  122.             }
  123.             if(choice==3)
  124.             {
  125.                 cout<<"Priority: ";
  126.                 cin>>P[i].prio;
  127.                 while(!cin)
  128.                 {
  129.                     cout<<"Invalid\nPriority: ";
  130.                     cin.clear();
  131.                     cin.ignore();
  132.                     cin>>P[i].prio;
  133.                     cin.ignore();
  134.                 }
  135.             }
  136.             cout<<endl;
  137.         }          
  138.    
  139.     //FCFS Sorting
  140.     for (int j=0; j<numProc-1;j++)
  141.     {
  142.         for (int k=0;k<numProc-j-1;k++)
  143.         {
  144.             if (P[k].arrTime>P[k+1].arrTime)
  145.             {
  146.                 swap(P[k].arrTime,P[k+1].arrTime);
  147.                 swap(P[k].pName,P[k+1].pName);
  148.                 swap(P[k].bTime,P[k+1].bTime);
  149.                 swap(P[k].prio,P[k+1].prio);
  150.             }                                
  151.         }
  152.     }
  153.    
  154.     //SJF Sorting
  155.     if(choice==2)
  156.     {
  157.         for (int i=0; i<numProc-1;i++)
  158.         {
  159.             for (int j=1;i<numProc-j-1;j++)
  160.             {
  161.                 if(P[j].bTime>=P[j+1].bTime)
  162.                 {
  163.                     swap(P[j].arrTime,P[j+1].arrTime);
  164.                     swap(P[j].pName,P[j+1].pName);
  165.                     swap(P[j].bTime,P[j+1].bTime);
  166.                    
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172.     else if(choice==3)
  173.     {                                  
  174.         //Prio Sorting
  175.        
  176.         int p;
  177.            
  178.         //Same Arrival Time Condition          
  179.         for (int i=0; i<numProc-1;i++)
  180.         {          
  181.             for (int j=1;j<numProc;j++)
  182.             {
  183.                 if(j!=i)
  184.                 {
  185.                     if(P[i].arrTime == P[j].arrTime)
  186.                     {
  187.                         p= true;
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.            
  193.         if(p==true)
  194.         {                  
  195.             for (int i=0; i<numProc-1;i++)
  196.             {          
  197.                 for (int j=1;j<numProc-i-1;j++)
  198.                 {
  199.                     if(P[j].arrTime<=P[j-1].eTime&&P[j].prio>=P[j+1].prio)
  200.                     {
  201.                         swap(P[j].prio,P[j+1].prio);
  202.                         swap(P[j].arrTime,P[j+1].arrTime);
  203.                         swap(P[j].pName,P[j+1].pName);
  204.                         swap(P[j].bTime,P[j+1].bTime);                 
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.        
  210.         //No same Arrival Time
  211.         else
  212.         {
  213.         for (int i=0; i<numProc-1;i++)
  214.         {
  215.             for (int j=1;i<numProc-j-1;j++)
  216.             {
  217.                 if(P[j].bTime>=P[j+1].bTime)
  218.                 {
  219.                     swap(P[j].arrTime,P[j+1].arrTime);
  220.                     swap(P[j].pName,P[j+1].pName);
  221.                     swap(P[j].bTime,P[j+1].bTime);
  222.                     swap(P[j].bTime,P[j+1].bTime);  
  223.                 }
  224.             }
  225.         }
  226.         }
  227.        
  228.     }  
  229.    
  230.     //Idle Condition
  231.     for (int i =0;i<numProc;i++)
  232.     {
  233.         if(i==0)
  234.         {
  235.             P[0].sTime = P[0].arrTime;
  236.             P[0].eTime = P[0].arrTime + P[0].bTime;
  237.         }
  238.         else
  239.         {
  240.             P[i].sTime = P[i-1].eTime;         
  241.             if(P[i].arrTime>P[i].sTime)
  242.             P[i].sTime = P[i].arrTime;
  243.             P[i].eTime = P[i].sTime + P[i].bTime;                      
  244.         }      
  245.     }                          
  246.    
  247.     if(choice==1||choice==2)
  248.     {
  249.         cout << "P#\t\tAT\t\tBT\t\tWT\t\tTT\t\t" <<endl;       
  250.         for (int l=0; l<numProc;l++)
  251.         {
  252.             cout << P[l].pName<<"\t\t"<<P[l].arrTime<<"\t\t"<<P[l].bTime<<"\t\t"<<P[l].sTime<<"\t\t"<<P[l].eTime<<endl;
  253.         }
  254.     }
  255.    
  256.     else if(choice==3)
  257.     {
  258.         cout << "P#\t\tAT\t\tBT\t\tWT\t\tTT\t\tP" <<endl;  
  259.         for (int j=0; j<numProc;j++)
  260.         {
  261.             cout << P[j].pName<<"\t\t"<<P[j].arrTime<<"\t\t"<<P[j].bTime<<"\t\t"<<P[j].sTime<<"\t\t"<<P[j].eTime<<"\t\t"<<P[j].prio<<endl;
  262.         }
  263.     }
  264.    
  265.     cout<< "\nGantt Chart" <<endl;
  266.     print_gantt_chart(P,numProc);          
  267.  
  268.     }
  269.     else
  270.     {
  271.         cout <<"Invalid Input! must not be greater than 10"<<endl;
  272.         goto proc;
  273.     }
  274.  
  275.     system("pause");
  276.     return 0;
  277. }
  278.  
  279. void print_gantt_chart(procs P[], int numProc)
  280. {
  281.     int i, j;
  282.  
  283.     cout <<" ";
  284.     for(i=0; i<numProc; i++)
  285.     {
  286.         for(j=0; j<P[i].bTime; j++) cout <<"--";
  287.         cout <<" ";
  288.     }
  289.  
  290.     cout <<"\n|";
  291.  
  292.     for(i=0; i<numProc; i++) {
  293.         for(j=0; j<P[i].bTime - 1; j++) cout <<" ";
  294.         cout<<P[i].pName;
  295.         for(j=0; j<P[i].bTime - 1; j++) cout<< " ";
  296.         cout<<" |";
  297.     }
  298.     cout<<"\n ";
  299.  
  300.     for(i=0; i<numProc; i++) {
  301.         for(j=0; j<P[i].bTime; j++) cout<<"--";
  302.         cout<<" ";
  303.     }
  304.     cout<<"\n";
  305.     int diffindex = P[0].sTime - 0;
  306.     int diff = 0;
  307.     int hold = 0;
  308.  
  309.     if (diffindex==0)
  310.     cout<<"0";
  311.     else
  312.     cout<<"0"<<"[I]"<< 0+diffindex;
  313.     for(i=0; i<numProc; i++)
  314.     {   hold=0;
  315.         if (i<3)
  316.         diff = P[i+1].sTime - P[i].eTime;
  317.         if (diff!=0)
  318.         {
  319.             hold = 1;
  320.         }
  321.    
  322.        
  323.         if (hold==1)
  324.         {
  325.             for(j=0; j<P[i].bTime-2; j++) cout<<"  ";
  326.             cout<<P[i].eTime<< "[I]"<<P[i].eTime+diff; 
  327.         }
  328.         else
  329.         {
  330.             for(j=0; j<P[i].bTime-1; j++) cout<<"  ";
  331.          cout<<P[i].eTime<<"";
  332.        
  333.         }
  334.          
  335.     }
  336.     cout<<"\n";
  337.  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement