Dark_Shard

CPU_Sched

Jul 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.38 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int cols = 9;
  8.     int row = 5;
  9.  
  10.     string header ="";
  11.  
  12.  
  13.     int counter = 0;
  14.     int arraycounter = 0;
  15.     int response=0;
  16.  
  17.     int final_time = 0;
  18.  
  19.     int idle = 0;
  20.     int idle_counter= 0  ;
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     int average_wt = 0;
  27.     int average_bt = 0;
  28.     int average_tt = 0 ;
  29.     int average_at = 0;
  30.  
  31.  
  32.     int temp_var = 0;
  33.     cout << "Choose your CPU Scheduling(1- FCFS\t2- SJF\t\t3- Priority)" << endl;
  34.     cin >> response;
  35.     while(cin.fail()|| response < 1 || response > 3)
  36.     {
  37.         cin.clear();
  38.         cin.ignore(512,'\n');
  39.         cout << "Invalid Input. Please try again." << endl;
  40.          cout << "Choose your CPU Scheduling(1- FCFS\t2- SJF\t\t3- Priority )" << endl;
  41.          cin>> response;
  42.     }
  43.     switch(response)
  44.     {
  45.         case 1:
  46.             header = "First Come, First Serve Scheduling";
  47.             break;
  48.  
  49.         case 2:
  50.             header = "Shortest Job First Scheduling";
  51.             break;
  52.  
  53.         case 3:
  54.             header = "Priority Scheduling";
  55.             break;
  56.     }
  57.  
  58.  
  59.     cout  << header   <<endl;
  60.     cout << "--------------------------------------------------------------------------------";
  61.     cout << "Input number of processes" << endl;
  62.     cin >> row;
  63.  
  64.     while(cin.fail())
  65.             {
  66.                 cin.clear();
  67.                 cin.ignore(512, '\n');
  68.                 cout << "Input error " << endl;
  69.                 cout << "Input number of processes" << endl;
  70.                 cin >> row;
  71.             }
  72.  
  73.     while(row>10 || row <= 0)
  74.     {
  75.         cout << "Processes cannot be more than 10 or less than 1. Please input another value." << endl;
  76.         cin >> row;
  77.         while(cin.fail())
  78.             {
  79.                 cin.clear();
  80.                 cin.ignore(512, '\n');
  81.                 cout << "Input error " << endl;
  82.                 cout << "Input number of processes" << endl;
  83.                 cin >> row;
  84.             }
  85.     }
  86.     /*  Indexes~
  87.     *   0 - PID
  88.     *   1 - Arv Time
  89.     *   2 - Burst Time
  90.     *   3 - Waiting Time
  91.     *   4 - Turnaround Time
  92.     *   5 - End Time
  93.     *   6 - Start Time
  94.     *   7 - Idle Time
  95.     *   8 - Priority (conditional)
  96.     *
  97.     */
  98.  
  99.     int cpuTime[row][cols] = {{0,0,0,0,0,0,0,0,0}};
  100.  
  101.     cout << endl;
  102.     cout << "------------------------------" << endl;
  103.     cout << "Arrival Time" << endl;
  104.  
  105.     for(int input = 0 ; input < row ; input++)
  106.     {
  107.  
  108.  
  109.             cpuTime[input][0] = input+1;
  110.             cout << "Input arrival time of process of " << " Process "  << cpuTime[input][0]  << endl;
  111.             cin >> cpuTime[input][1];
  112.  
  113.             while(!cin)
  114.             {
  115.                 cin.clear();
  116.                 cin.ignore(512, '\n');
  117.                 cout << "Input error" << endl;
  118.                 cout << "Input arrival time of process of " << " Process "  << cpuTime[input][0]  << endl;
  119.                 cin >> cpuTime[input][1];
  120.             }
  121.             cout << endl;
  122.     }
  123.  
  124.     cout << endl;
  125.     cout << "------------------------------" << endl;
  126.     cout << "Burst Time" << endl;
  127.  
  128.     for(int secondinput = 0 ; secondinput < row; secondinput++)
  129.     {
  130.  
  131.             cout << "Input the burst time of " << "Process " << cpuTime[secondinput][0]<< endl;
  132.             cin >> cpuTime[secondinput][2];
  133.  
  134.             while(!cin)
  135.             {
  136.                 cin.clear();
  137.                 cin.ignore(512, '\n');
  138.                 cout << "Input error" << endl;
  139.                 cout << "Input the burst time  " << endl;
  140.                 cin >> cpuTime[secondinput][2];
  141.             }
  142.             cout << endl;
  143.  
  144.     }
  145.      cout << "------------------------------" << endl;
  146.     cout << "Priority" << endl;
  147.     if(response == 3)
  148.     {
  149.         for(int prio = 0 ; prio < row ; prio++)
  150.         {
  151.             cout << "Input Priority level of Process " << cpuTime[prio][0] <<endl;
  152.             cin >> cpuTime[prio][8];
  153.             cout << endl;
  154.         }
  155.     }
  156.  
  157.     for(int sortvar = 0 ; sortvar < row - 1 ; sortvar++)
  158.     {
  159.         for(int sort2 = 0; sort2 < row-sortvar-1 ; sort2++)
  160.         {
  161.             if(cpuTime[sort2][1] > cpuTime[sort2+1][1])        // according to arrival time
  162.             {
  163.                 temp_var = cpuTime[sort2][0];
  164.                 cpuTime[sort2][0] = cpuTime[sort2+1][0];            // Process Number Swap
  165.                 cpuTime[sort2+1][0] = temp_var;
  166.  
  167.                 temp_var = cpuTime[sort2][1];
  168.                 cpuTime[sort2][1] = cpuTime[sort2+1][1];            // Arrival Time Swap
  169.                 cpuTime[sort2+1][1] =  temp_var;
  170.  
  171.                 temp_var = cpuTime[sort2][2];
  172.                  cpuTime[sort2][2] =  cpuTime[sort2+1][2];          // Burst Time Swap
  173.                  cpuTime[sort2+1][2] = temp_var;
  174.  
  175.             }
  176.         }
  177.     }
  178.     cout << "Sorted(Acc. Arrival Time)" << endl;
  179.     cout << "------------------------------" << endl;
  180.     cout << "Process\t" << "Arrival Time\t"  << "Burst Time\t";
  181.     if(response == 3)
  182.     {
  183.         cout << "Priority";
  184.     }
  185.     cout << endl;
  186.     for(int print = 0; print < row ; print++)
  187.     {
  188.         cout << cpuTime[print][0] << "\t" << cpuTime[print][1] << "\t\t\t" << cpuTime[print][2] << "\t";
  189.         if(response == 3)
  190.         {
  191.             cout << cpuTime[print][8];
  192.         }
  193.         cout << endl;
  194.     }
  195.  
  196.     if(response == 2)                               // if user chooses SJF hmmm~
  197.     {
  198.  
  199.         for(int sortvar = 0; sortvar < row - 1; sortvar++)  // starts with the first index cause the first process would already be the lowest arrival time (not needed to be sorted no moreee)~
  200.         {
  201.             for(int sort2 = 1; sort2< row-sortvar-1; sort2++)
  202.             {
  203.                 if(cpuTime[sort2][2]> cpuTime[sort2+1][2])
  204.                 {
  205.                      temp_var = cpuTime[sort2][0];
  206.                 cpuTime[sort2][0] = cpuTime[sort2+1][0];            // Process Number Swap
  207.                 cpuTime[sort2+1][0] = temp_var;
  208.  
  209.                 temp_var = cpuTime[sort2][1];
  210.                 cpuTime[sort2][1] = cpuTime[sort2+1][1];            // Arrival Time Swap
  211.                 cpuTime[sort2+1][1] =  temp_var;
  212.  
  213.                 temp_var = cpuTime[sort2][2];
  214.                  cpuTime[sort2][2] =  cpuTime[sort2+1][2];          // Burst Time Swap
  215.                  cpuTime[sort2+1][2] = temp_var;
  216.  
  217.  
  218.                 }
  219.             }
  220.         }
  221.  
  222.  
  223.  
  224.     cout << "Sorted(Acc. Burst Time)" << endl;
  225.     cout << "------------------------------" << endl;
  226.     cout << "Process\t" << "Arrival Time\t"  << "Burst Time\t" << endl;
  227.  
  228.         for(int print = 0 ; print < row; print++)
  229.         {
  230.             cout << cpuTime[print][0] << "\t" << cpuTime[print][1] << "\t\t\t" << cpuTime[print][2] << endl;
  231.         }
  232.     }
  233.  
  234.  
  235.  
  236.     else if(response == 3)  // if user chooses Priority(woaaah priorities)
  237.     {
  238.         for(int sort = 1; sort < row - 1; sort++)   // starts with the first index cause the first process would already be the lowest arrival time (not needed to be sorted no moreee)~
  239.         {
  240.             for(int sort2 = 1; sort2< row-sort-1; sort2++)
  241.             {
  242.                 if(cpuTime[sort2][8]> cpuTime[sort2+1][8])
  243.                 {
  244.                 temp_var = cpuTime[sort2][0];
  245.                 cpuTime[sort2][0] = cpuTime[sort2+1][0];            // Process Number Swap
  246.                 cpuTime[sort2+1][0] = temp_var;
  247.  
  248.                 temp_var = cpuTime[sort2][1];
  249.                 cpuTime[sort2][1] = cpuTime[sort2+1][1];            // Arrival Time Swap
  250.                 cpuTime[sort2+1][1] =  temp_var;
  251.  
  252.                 temp_var = cpuTime[sort2][2];
  253.                  cpuTime[sort2][2] =  cpuTime[sort2+1][2];          // Burst Time Swap
  254.                  cpuTime[sort2+1][2] = temp_var;
  255.  
  256.                  temp_var = cpuTime[sort2][8];                  //Priority Swap
  257.                  cpuTime[sort2][8] = cpuTime[sort2+1][8];
  258.                  cpuTime[sort2+1][8] = temp_var;
  259.  
  260.                 }
  261.             }
  262.         }
  263.     }
  264.  
  265.  
  266.     /*  Indexes~
  267.     *   0 - PID
  268.     *   1 - Arv Time
  269.     *   2 - Burst Time
  270.     *   3 - Waiting Time
  271.     *   4 - Turnaround Time
  272.     *   5 - End Time
  273.     *   6 - Start Time
  274.     *   7 - Idle Time
  275.     *   8 - Priority (conditional)
  276.     *
  277.     */
  278.     for(int cal = 0; cal < row; cal++)
  279.     {
  280.         if(cal == 0)
  281.         {
  282.                 cpuTime[cal][6] = cpuTime[cal][1]; // start time would be the arrival time of first proc
  283.                 if(cpuTime[cal][1]>0)
  284.                 {
  285.                     cpuTime[cal][7] = cpuTime[cal][1];
  286.  
  287.                 }
  288.                 cpuTime[cal][5] = cpuTime[cal][1] + cpuTime[cal][2]; // et = at + bt
  289.                 cpuTime[cal][3] = cpuTime[cal][6] - cpuTime[cal][1]; // wt = st - at
  290.                 cpuTime[cal][4] = cpuTime[cal][5] - cpuTime[cal][1];
  291.                                                             // tt = et - at
  292.                 continue;
  293.         }
  294.  
  295.         if(cpuTime[cal][1] - cpuTime[cal-1][5] > 0) // if curr arv time - previous end time > 0 then implement idle time
  296.         {
  297.  
  298.             cpuTime[cal][7] = cpuTime[cal][1] - cpuTime[cal-1][5];
  299.             cpuTime[cal][6] = cpuTime[cal][1];
  300.         }
  301.  
  302.         else{
  303.             cpuTime[cal][6] = cpuTime[cal-1][5];
  304.         }
  305.         cpuTime[cal][5] = cpuTime[cal][6] + cpuTime[cal][2]; // et = at + bt
  306.         cpuTime[cal][3] = cpuTime[cal][6] - cpuTime[cal][1]; // wt = st - at
  307.         cpuTime[cal][4] = cpuTime[cal][5] - cpuTime[cal][1];
  308.  
  309.  
  310.     }
  311.     /*for(int c = 0; c < row; c++)          //Getting the start time, end time, waiting time, turnaround time of each process
  312.     {
  313.         if(c == 0)
  314.         {
  315.  
  316.         start_time = cpuTime[c][1];
  317.         cpuTime[c][6] = start_time;
  318.         end_time = start_time + cpuTime[c][2];
  319.         cpuTime[c][5] = end_time;
  320.             if(cpuTime[c][1] - end_time > 0 )
  321.             {
  322.                 cpuTime[c][7] = cpuTime[c][1] - end_time;
  323.             }
  324.  
  325.         cpuTime[c][4] = end_time - cpuTime[c][1];
  326.         cpuTime[c][3] = start_time - cpuTime[c][1];
  327.         average_at+= cpuTime[c][1];
  328.         average_bt+= cpuTime[c][2];
  329.         average_wt+= cpuTime[c][3];
  330.         average_tt+= cpuTime[c][4];
  331.             continue;
  332.         }
  333.         start_time = end_time;
  334.         if(cpuTime[c][1] - end_time > 0 )
  335.         {
  336.             cpuTime[c][7] = cpuTime[c][1] - end_time;
  337.             start_time = end_time + cpuTime[c][7];
  338.         }
  339.  
  340.  
  341.         cpuTime[c][6] = start_time;
  342.         end_time = start_time + cpuTime[c][2];
  343.         cpuTime[c][5] = end_time;
  344.         final_time = end_time;
  345.         cpuTime[c][4] = end_time - cpuTime[c][1];
  346.         cpuTime[c][3] = start_time - cpuTime[c][1];
  347.         average_at+= cpuTime[c][1];
  348.         average_bt+= cpuTime[c][2];
  349.         average_wt+= cpuTime[c][3];
  350.         average_tt+= cpuTime[c][4];
  351.     }*/
  352.     average_at/=row;
  353.     average_bt/= row;
  354.     average_wt/= row;
  355.     average_tt/= row;
  356.     cout << endl << endl << endl;
  357.  
  358.     cout << "With Calculated Waiting Time and Turn Around Time" << endl;
  359.     cout << "+---------------------------------------------------------------------------+" << endl;
  360.     cout << "PID\t" << "Arrival Time\t" << "Burst Time\t" << "Waiting Time\t" << "Turn Around Time\t";
  361.     if(response == 3)
  362.     {
  363.         cout << "Priority";
  364.     }
  365.     cout << endl;
  366.     for(int print = 0 ; print < row ; print++)
  367.     {
  368.         cout << cpuTime[print][0] << "\t     " << cpuTime[print][1] << "\t\t " << cpuTime[print][2] << "\t\t\t" << cpuTime[print][3] << "\t\t"<< cpuTime[print][4] << "\t\t\t";
  369.         if(response == 3)
  370.         {
  371.             cout << cpuTime[print][8];
  372.         }
  373.         cout << endl;
  374.     }
  375.     cout << "+---------------------------------------------------------------------------+" << endl;
  376.     cout << "Type of Scheduling" << header << endl;
  377.     cout << "Total Number of Process/es: " << row << endl;
  378.     cout << "Average Arrival Time: " << average_at << endl;
  379.     cout << "Average Waiting Time: " << average_wt << endl;
  380.     cout << "Average Burst Time: " << average_bt << endl;
  381.     cout << "Average Turnaround Time: " << average_tt << endl;
  382.     cout << "Process Endtime: " << final_time << endl;
  383.  
  384.  
  385.  
  386.     cout << "-----------------------------------------------------------------------------" << endl;
  387.     cout<< "\t\t\t\tGantt Chart" << endl;
  388.     cout << "      ";
  389.  
  390.  
  391.     // Gantt Chart Starttooo
  392.      for(int a = 0 ; a < 68; a++)
  393.     {
  394.         cout << "-";
  395.     }
  396.  
  397.  
  398.     cout << endl;
  399.  
  400.  
  401.     cout << "      ";
  402.     for(int b = 0; b < 204; b++)
  403.     {
  404.         switch (b)
  405.         {
  406.             case 0:
  407.             case 7:
  408.             case 14:
  409.             case 21: // first line of barlines
  410.             case 28:
  411.  
  412.             case 34:
  413.  
  414.             case 41:
  415.             case 48:
  416.             case 55:
  417.             case 62:
  418.                  // 89 90 91 92 93 94 95 96 // 96 97 98 99 100 101 102 103
  419.                 //79 //  86 //  93 //
  420.  
  421.             case 125:
  422.  
  423.             case 133:
  424.             case 140:
  425.             case 147:
  426.             case 154:
  427.  
  428.             case 160:
  429.             case 167:
  430.             case 174:
  431.             case 181:
  432.             case 188:
  433.             case 193:
  434.  
  435.  
  436.             case 67:
  437.  
  438.  
  439.                 //135 middle barline right
  440.                 cout << "|";
  441.                 if(b == 67 || b == 125)
  442.                 {
  443.                     cout << endl;
  444.                 }
  445.                 break;
  446.  
  447.             case 68:
  448.             case 126:
  449.                 //68 middle barline left
  450.                 if(b == 126)
  451.                 {
  452.                     cout << "      |";
  453.                     break;
  454.                 }
  455.                 cout << "      |";
  456.  
  457.                 break;
  458.  
  459.             case 71: //1
  460.             case 77:// 2
  461.             case 83://3
  462.             case 89: //4
  463.             case 94: //5
  464.             case 100: //6
  465.             case 106: // 7
  466.             case 112:
  467.             case 118:
  468.             case 123:
  469.  
  470.  
  471.                 if(counter < row)
  472.                 {
  473.                     cout << "P"  << cpuTime[counter][0];
  474.                     counter++;
  475.                 }
  476.                 else{
  477.                     cout << "x ";
  478.                 }
  479.                 break;
  480.  
  481.             default:
  482.                 cout << " ";
  483.                 break;
  484.         }
  485.     }
  486.  
  487.     cout << "  ";
  488.     for(int a = 0 ; a < 68; a++)
  489.     {
  490.         cout << "-";
  491.     }
  492.  
  493.     cout << endl;
  494.     cout << "      ";
  495.     for(int label = 0 ; label < 68; label++)
  496.     {
  497.         switch(label)
  498.         {
  499.             case 0:
  500.             case 7:
  501.             case 14:
  502.             case 21:
  503.             case 28:
  504.             case 35:
  505.             case 42:
  506.             case 49:
  507.             case 56:
  508.             case 63:
  509.             case 70:
  510.                 if(arraycounter <= row)
  511.                 {
  512.                     if(arraycounter == row)
  513.                     {
  514.                         cout << final_time;
  515.                         arraycounter++;
  516.                         break;
  517.                     }
  518.  
  519.                     if(cpuTime[arraycounter][7] > 0) // if next index of idle time > 0
  520.                     {
  521.                         if(arraycounter == 0)
  522.                         {
  523.                             cout << "|I|";
  524.  
  525.                         }
  526.                         else{
  527.                             cout <<"\b\b\b\b" <<cpuTime[arraycounter-1][5] << "|I|";
  528.                         }
  529.  
  530.  
  531.                     }
  532.                     cout << cpuTime[arraycounter][6];
  533.  
  534.  
  535.                     arraycounter++;
  536.                 }
  537.                 else{
  538.                     cout << "-";
  539.                 }
  540.                 break;
  541.  
  542.             default:
  543.                 cout << " ";
  544.                 break;
  545.         }
  546.     }
  547.     cout << endl;
  548.     cout << endl;
  549.  
  550.     while(idle < row-1)
  551.     {
  552.         if(idle == 0)
  553.         {
  554.             cout << "Idle time before Process " << cpuTime[idle][0]  << " starts " << cpuTime[idle_counter][7] << endl;
  555.             idle_counter++;
  556.         }
  557.  
  558.         cout << "Idle time between Process " << cpuTime[idle][0] << " and " << cpuTime[idle+1][0] << " is: " << cpuTime[idle_counter][7] << endl;
  559.         idle++;
  560.         idle_counter++;
  561.     }
  562.  
  563. }
Add Comment
Please, Sign In to add comment