Advertisement
Dark_Shard

Prio

Jul 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.02 KB | None | 0 0
  1. #include<iostream>
  2. // meep
  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 Policy(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.     /*  Indexes~
  385.     *   0 - PID
  386.     *   1 - Arv Time
  387.     *   2 - Burst Time
  388.     *   3 - Waiting Time
  389.     *   4 - Turnaround Time
  390.     *   5 - End Time
  391.     *   6 - Start Time
  392.     *   7 - Idle Time
  393.     *   8 - Priority (conditional)
  394.     *
  395.     */
  396.  
  397.    if(response == 3) // this is arranging it on how it will be presented in gantt chart for prio cpu scheduling
  398.     {
  399.         for(int a = 0 ; a < row-1; a++)
  400.         {
  401.             for(int ab = 1 ; ab < row-a-1; ab++)
  402.             {
  403.                 if(cpuTime[ab-1][5] > cpuTime[ab][1] && cpuTime[ab][8] > cpuTime[ab+1][8])
  404.                 {
  405.                 temp_var = cpuTime[ab][0];
  406.                 cpuTime[ab][0] = cpuTime[ab+1][0];            // Process Number Swap
  407.                 cpuTime[ab+1][0] = temp_var;
  408.  
  409.                 temp_var = cpuTime[ab][1];
  410.                 cpuTime[ab][1] = cpuTime[ab+1][1];            // Arrival Time Swap
  411.                 cpuTime[ab+1][1] =  temp_var;
  412.  
  413.                 temp_var = cpuTime[ab][2];
  414.                  cpuTime[ab][2] =  cpuTime[ab+1][2];          // Burst Time Swap
  415.                  cpuTime[ab+1][2] = temp_var;
  416.  
  417.                  temp_var = cpuTime[ab][8];                 //Priority Swap
  418.                  cpuTime[ab][8] = cpuTime[ab+1][8];
  419.                  cpuTime[ab+1][8] = temp_var;
  420.  
  421.                  temp_var = cpuTime[ab][3];                 //Priority Swap
  422.                  cpuTime[ab][3] = cpuTime[ab+1][3];
  423.                  cpuTime[ab+1][3] = temp_var;
  424.  
  425.                  temp_var = cpuTime[ab][4];                 //Priority Swap
  426.                  cpuTime[ab][4] = cpuTime[ab+1][4];
  427.                  cpuTime[ab+1][4] = temp_var;
  428.                 }
  429.             }
  430.         }
  431.     }
  432.  
  433.  
  434.  
  435.     cout << "-----------------------------------------------------------------------------" << endl;
  436.     cout<< "\t\t\t\tGantt Chart" << endl;
  437.     cout << "      ";
  438.  
  439.  
  440.     // Gantt Chart Starttooo
  441.      for(int a = 0 ; a < 68; a++)
  442.     {
  443.         cout << "-";
  444.     }
  445.  
  446.  
  447.     cout << endl;
  448.  
  449.  
  450.     cout << "      ";
  451.     for(int b = 0; b < 204; b++)
  452.     {
  453.         switch (b)
  454.         {
  455.             case 0:
  456.             case 7:
  457.             case 14:
  458.             case 21: // first line of barlines
  459.             case 28:
  460.  
  461.             case 34:
  462.  
  463.             case 41:
  464.             case 48:
  465.             case 55:
  466.             case 62:
  467.                  // 89 90 91 92 93 94 95 96 // 96 97 98 99 100 101 102 103
  468.                 //79 //  86 //  93 //
  469.  
  470.             case 125:
  471.  
  472.             case 133:
  473.             case 140:
  474.             case 147:
  475.             case 154:
  476.  
  477.             case 160:
  478.             case 167:
  479.             case 174:
  480.             case 181:
  481.             case 188:
  482.             case 193:
  483.  
  484.  
  485.             case 67:
  486.  
  487.  
  488.                 //135 middle barline right
  489.                 cout << "|";
  490.                 if(b == 67 || b == 125)
  491.                 {
  492.                     cout << endl;
  493.                 }
  494.                 break;
  495.  
  496.             case 68:
  497.             case 126:
  498.                 //68 middle barline left
  499.                 if(b == 126)
  500.                 {
  501.                     cout << "      |";
  502.                     break;
  503.                 }
  504.                 cout << "      |";
  505.  
  506.                 break;
  507.  
  508.             case 71: //1
  509.             case 77:// 2
  510.             case 83://3
  511.             case 89: //4
  512.             case 94: //5
  513.             case 100: //6
  514.             case 106: // 7
  515.             case 112:
  516.             case 118:
  517.             case 123:
  518.  
  519.  
  520.                 if(counter < row)
  521.                 {
  522.                     cout << "P"  << cpuTime[counter][0];
  523.                     counter++;
  524.                 }
  525.                 else{
  526.                     cout << "x ";
  527.                 }
  528.                 break;
  529.  
  530.             default:
  531.                 cout << " ";
  532.                 break;
  533.         }
  534.     }
  535.  
  536.     cout << "  ";
  537.     for(int a = 0 ; a < 68; a++)
  538.     {
  539.         cout << "-";
  540.     }
  541.  
  542.     cout << endl;
  543.     cout << "      ";
  544.     for(int label = 0 ; label < 68; label++)
  545.     {
  546.         switch(label)
  547.         {
  548.             case 0:
  549.             case 7:
  550.             case 14:
  551.             case 21:
  552.             case 28:
  553.             case 35:
  554.             case 42:
  555.             case 49:
  556.             case 56:
  557.             case 63:
  558.             case 70:
  559.                 if(arraycounter <= row)
  560.                 {
  561.                     if(arraycounter == row)
  562.                     {
  563.                         cout << final_time;
  564.                         arraycounter++;
  565.                         break;
  566.                     }
  567.  
  568.                     if(cpuTime[arraycounter][7] > 0) // if next index of idle time > 0
  569.                     {
  570.                         if(arraycounter == 0)
  571.                         {
  572.                             cout << "|I|";
  573.  
  574.                         }
  575.                         else{
  576.                             cout <<"\b\b\b\b" <<cpuTime[arraycounter-1][5] << "|I|";
  577.                         }
  578.  
  579.  
  580.                     }
  581.                     cout << cpuTime[arraycounter][6];
  582.  
  583.  
  584.                     arraycounter++;
  585.                 }
  586.                 else{
  587.                     cout << "-";
  588.                 }
  589.                 break;
  590.  
  591.             default:
  592.                 cout << " ";
  593.                 break;
  594.         }
  595.     }
  596.     cout << endl;
  597.     cout << endl;
  598.  
  599.     while(idle < row-1)
  600.     {
  601.         if(idle == 0)
  602.         {
  603.             cout << "Idle time before Process " << cpuTime[idle][0]  << " starts " << cpuTime[idle_counter][7] << endl;
  604.             idle_counter++;
  605.         }
  606.  
  607.         cout << "Idle time between Process " << cpuTime[idle][0] << " and " << cpuTime[idle+1][0] << " is: " << cpuTime[idle_counter][7] << endl;
  608.         idle++;
  609.         idle_counter++;
  610.     }
  611.  
  612. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement