Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- int cols = 9;
- int row = 5;
- int temp = 0;
- string header ="";
- int counter = 0;
- int arraycounter = 0;
- int response=0;
- int final_time = 0;
- int idle = 0;
- int idle_counter= 0 ;
- int average_wt = 0;
- int average_bt = 0;
- int average_tt = 0 ;
- int average_at = 0;
- cout << "Choose your CPU Scheduling Policy(1- FCFS\t2- SJF\t\t3- Priority)" << endl;
- cin >> response;
- while(cin.fail()|| response < 1 || response > 3)
- {
- cin.clear();
- cin.ignore(512,'\n');
- cout << "Invalid Input. Please try again." << endl;
- cout << "Choose your CPU Scheduling(1- FCFS\t2- SJF\t\t3- Priority )" << endl;
- cin>> response;
- }
- switch(response)
- {
- case 1:
- header = "First Come, First Serve Scheduling";
- break;
- case 2:
- header = "Shortest Job First Scheduling";
- break;
- case 3:
- header = "Priority Scheduling";
- break;
- }
- cout << header <<endl;
- cout << "--------------------------------------------------------------------------------" << endl;
- cout << "Input number of processes" << endl;
- cin >> row;
- while(cin.fail())
- {
- cin.clear();
- cin.ignore(512, '\n');
- cout << "Input error " << endl;
- cout << "Input number of processes" << endl;
- cin >> row;
- }
- while(row>10 || row <= 0)
- {
- cout << "Processes cannot be more than 10 or less than 1. Please input another value." << endl;
- cin >> row;
- while(cin.fail())
- {
- cin.clear();
- cin.ignore(512, '\n');
- cout << "Input error " << endl;
- cout << "Input number of processes" << endl;
- cin >> row;
- }
- }
- /* Indexes~
- * 0 - PID
- * 1 - Arv Time
- * 2 - Burst Time
- * 3 - Waiting Time
- * 4 - Turnaround Time
- * 5 - End Time
- * 6 - Start Time
- * 7 - Idle Time
- * 8 - Priority (conditional)
- *
- */
- int cpuTime[row][cols] = {{0,0,0,0,0,0,0,0,0}};
- cout << endl;
- cout << "------------------------------" << endl;
- cout << "Arrival Time" << endl;
- for(int input = 0 ; input < row ; input++)
- {
- cpuTime[input][0] = input+1;
- cout << "Input arrival time of process of " << " Process " << cpuTime[input][0] << endl;
- cin >> cpuTime[input][1];
- while(!cin)
- {
- cin.clear();
- cin.ignore(512, '\n');
- cout << "Input error" << endl;
- cout << "Input arrival time of process of " << " Process " << cpuTime[input][0] << endl;
- cin >> cpuTime[input][1];
- }
- cout << endl;
- }
- cout << endl;
- cout << "------------------------------" << endl;
- cout << "Burst Time" << endl;
- for(int secondinput = 0 ; secondinput < row; secondinput++)
- {
- cout << "Input the burst time of " << "Process " << cpuTime[secondinput][0]<< endl;
- cin >> cpuTime[secondinput][2];
- while(!cin)
- {
- cin.clear();
- cin.ignore(512, '\n');
- cout << "Input error" << endl;
- cout << "Input the burst time " << endl;
- cin >> cpuTime[secondinput][2];
- }
- cout << endl;
- }
- cout << "------------------------------" << endl;
- cout << "Priority" << endl;
- if(response == 3)
- {
- for(int prio = 0 ; prio < row ; prio++)
- {
- cout << "Input Priority level of Process " << cpuTime[prio][0] <<endl;
- cin >> cpuTime[prio][8];
- cout << endl;
- }
- }
- for(int sortvar = 0 ; sortvar < row - 1 ; sortvar++)
- {
- for(int sort2 = 0; sort2 < row-sortvar-1 ; sort2++)
- {
- if(cpuTime[sort2][1] > cpuTime[sort2+1][1]) // according to arrival time
- {
- temp = cpuTime[sort2][0];
- cpuTime[sort2][0] = cpuTime[sort2+1][0]; // Process Number Swap
- cpuTime[sort2+1][0] = temp;
- temp= cpuTime[sort2][1];
- cpuTime[sort2][1] = cpuTime[sort2+1][1]; // Arrival Time Swap
- cpuTime[sort2+1][1] = temp;
- temp = cpuTime[sort2][2];
- cpuTime[sort2][2] = cpuTime[sort2+1][2]; // Burst Time Swap
- cpuTime[sort2+1][2] = temp;
- temp = cpuTime[sort2][8];
- cpuTime[sort2][8]= cpuTime[sort2+1][8];
- cpuTime[sort2+1][8] = temp;
- }
- }
- }
- cout << "Sorted(Acc. Arrival Time)" << endl;
- cout << "------------------------------" << endl;
- cout << "Process\t" << "Arrival Time\t" << "Burst Time\t";
- cout << endl;
- for(int print = 0; print < row ; print++)
- {
- cout << cpuTime[print][0] << "\t" << cpuTime[print][1] << "\t\t\t" << cpuTime[print][2] << "\t";
- cout << endl;
- }
- if(response == 2) // if user chooses SJF hmmm~
- {
- 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)~
- {
- for(int sort2 = 1; sort2< row-sortvar-1; sort2++)
- {
- if(cpuTime[sort2][2]> cpuTime[sort2+1][2])
- {
- temp = cpuTime[sort2][0];
- cpuTime[sort2][0] = cpuTime[sort2+1][0]; // Process Number Swap
- cpuTime[sort2+1][0] = temp;
- temp = cpuTime[sort2][1];
- cpuTime[sort2][1] = cpuTime[sort2+1][1]; // Arrival Time Swap
- cpuTime[sort2+1][1] = temp;
- temp = cpuTime[sort2][2];
- cpuTime[sort2][2] = cpuTime[sort2+1][2]; // Burst Time Swap
- cpuTime[sort2+1][2] = temp;
- }
- }
- }
- cout << "Sorted(Acc. Burst Time)" << endl;
- cout << "------------------------------" << endl;
- cout << "Process\t" << "Arrival Time\t" << "Burst Time\t" << endl;
- for(int print = 0 ; print < row; print++)
- {
- cout << cpuTime[print][0] << "\t" << cpuTime[print][1] << "\t\t\t" << cpuTime[print][2] << endl;
- }
- }
- if(response == 3) // Fixed proper Prio scheduling within a given end time
- {
- if(cpuTime[0][1] > 0)
- {
- cpuTime[0][7] = cpuTime[0][1];
- }
- cpuTime[0][6] = cpuTime[0][1];
- cpuTime[0][5] = cpuTime[0][1] + cpuTime[0][2];
- for(int cal = 1 ; cal < row ; cal++)
- {
- if(cpuTime[cal-1][5] >= cpuTime[cal][1])
- {
- for(int mov = cal+1; mov < row ; mov++)
- {
- if(cpuTime[cal][8] > cpuTime[mov][8] && cpuTime[mov][1] < cpuTime[cal-1][5])
- {
- temp = cpuTime[cal][0];
- cpuTime[cal][0] = cpuTime[mov][0];
- cpuTime[mov][0] = temp;
- temp = cpuTime[cal][1];
- cpuTime[cal][1] = cpuTime[mov][1];
- cpuTime[mov][1] = temp;
- temp = cpuTime[cal][2];
- cpuTime[cal][2] = cpuTime[mov][2];
- cpuTime[mov][2] = temp;
- temp = cpuTime[cal][8];
- cpuTime[cal][8] = cpuTime[mov][8];
- cpuTime[mov][8] = temp;
- }
- }
- cpuTime[cal][6] = cpuTime[cal-1][5];
- }
- else{
- cpuTime[cal][6] = cpuTime[cal][1];
- cpuTime[cal][7] = cpuTime[cal][1]- cpuTime[cal-1][5];
- }
- cpuTime[cal][5] = cpuTime[cal][6] + cpuTime[cal][2];
- cpuTime[cal][3] = cpuTime[cal][6] - cpuTime[cal][1];
- cpuTime[cal][4] = cpuTime[cal][5] - cpuTime[cal][1];
- }
- cout << "Sorted (Acc. Priority Level)" << endl;
- cout << "------------------------------" << endl;
- cout << "Process\t" << "Arrival Time\t" << "Burst Time\t" << endl;
- for(int print =0 ; print < row; print++)
- {
- cout << cpuTime[print][0] << "\t\t" << cpuTime[print][1] << "\t\t\t" << cpuTime[print][2] << "\t\t" << cpuTime[print][8] << endl;
- }
- }
- /* Indexes~
- * 0 - PID
- * 1 - Arv Time
- * 2 - Burst Time
- * 3 - Waiting Time
- * 4 - Turnaround Time
- * 5 - End Time
- * 6 - Start Time
- * 7 - Idle Time
- * 8 - Priority (conditional)
- *
- */
- for(int cal = 0; cal < row; cal++) //Getting the start time, end time, waiting time, turnaround time of each process
- {
- if(cal == 0)
- {
- cpuTime[cal][6] = cpuTime[cal][1]; // start time would be the arrival time of first proc
- if(cpuTime[cal][1]>0)
- {
- cpuTime[cal][7] = cpuTime[cal][1];
- }
- cpuTime[cal][5] = cpuTime[cal][1] + cpuTime[cal][2]; // et = at + bt
- cpuTime[cal][3] = cpuTime[cal][6] - cpuTime[cal][1]; // wt = st - at
- cpuTime[cal][4] = cpuTime[cal][5] - cpuTime[cal][1]; // tt = et - at
- average_at+= cpuTime[cal][1];
- average_bt+= cpuTime[cal][2];
- average_wt+= cpuTime[cal][3];
- average_tt+= cpuTime[cal][4];
- continue;
- }
- if(cpuTime[cal][1] - cpuTime[cal-1][5] > 0) // if curr arv time - previous end time > 0 then implement idle time
- {
- cpuTime[cal][7] = cpuTime[cal][1] - cpuTime[cal-1][5];
- cpuTime[cal][6] = cpuTime[cal][1];
- }
- else{
- cpuTime[cal][6] = cpuTime[cal-1][5];
- }
- cpuTime[cal][5] = cpuTime[cal][6] + cpuTime[cal][2]; // et = at + bt
- cpuTime[cal][3] = cpuTime[cal][6] - cpuTime[cal][1]; // wt = st - at
- cpuTime[cal][4] = cpuTime[cal][5] - cpuTime[cal][1];
- average_at+= cpuTime[cal][1];
- average_bt+= cpuTime[cal][2];
- average_wt+= cpuTime[cal][3];
- average_tt+= cpuTime[cal][4];
- if(cal == row - 1)
- {
- final_time = cpuTime[cal][5];
- }
- }
- average_at/=row;
- average_bt/= row;
- average_wt/= row;
- average_tt/= row;
- cout << endl << endl << endl;
- cout << "With Calculated Waiting Time and Turn Around Time" << endl;
- cout << "+---------------------------------------------------------------------------+" << endl;
- cout << "PID\t" << "Arrival Time\t" << "Burst Time\t" << "Waiting Time\t" << "Turn Around Time\t";
- if(response == 3)
- {
- cout << "Priority";
- }
- cout << endl;
- for(int print = 0 ; print < row ; print++)
- {
- 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";
- if(response == 3)
- {
- cout << cpuTime[print][8];
- }
- cout << endl;
- }
- cout << "+---------------------------------------------------------------------------+" << endl;
- cout << "Type of Scheduling: " << header << endl;
- cout << "Total Number of Process/es: " << row << endl;
- cout << "Average Arrival Time: " << average_at << endl;
- cout << "Average Waiting Time: " << average_wt << endl;
- cout << "Average Burst Time: " << average_bt << endl;
- cout << "Average Turnaround Time: " << average_tt << endl;
- cout << "Process Endtime: " << final_time << endl;
- /* Indexes~
- * 0 - PID
- * 1 - Arv Time
- * 2 - Burst Time
- * 3 - Waiting Time
- * 4 - Turnaround Time
- * 5 - End Time
- * 6 - Start Time
- * 7 - Idle Time
- * 8 - Priority (conditional)
- *
- */
- cout << "-----------------------------------------------------------------------------" << endl;
- cout<< "\t\t\t\tGantt Chart" << endl;
- cout << " ";
- // Gantt Chart Start
- for(int a = 0 ; a < 68; a++)
- {
- cout << "-";
- }
- cout << endl;
- cout << " ";
- for(int b = 0; b < 204; b++)
- {
- switch (b)
- {
- case 0:
- case 2: // idle block first line
- case 5:
- case 9:
- case 12:
- case 16:
- case 19:
- case 23:
- case 26:
- case 30:
- case 32:
- case 36:
- case 39:
- case 43:
- case 46:
- case 50:
- case 53:
- case 57:
- case 60:
- case 64: // last idle block first line
- case 70: // first idle block second line
- case 72:
- case 76:
- case 78:
- case 82:
- case 84:
- case 88:
- case 90:
- case 93:
- case 95:
- case 99:
- case 101:
- case 104:
- case 107:
- case 110:
- case 113:
- case 116:
- case 119:
- case 122:
- case 128:
- case 131: /// 3rd idle
- case 135:
- case 138:
- case 142:
- case 145:
- case 149:
- case 152:
- case 156:
- case 158:
- case 162:
- case 165:
- case 169:
- case 172:
- case 176:
- case 179:
- case 183:
- case 186:
- case 190:
- case 7:
- case 14:
- case 21: // first line of barlines
- case 28:
- case 34:
- case 41:
- case 48:
- case 55:
- case 62:
- case 125:
- case 133:
- case 140:
- case 147:
- case 154:
- case 160:
- case 167:
- case 174:
- case 181:
- case 188:
- case 193:
- case 67:
- //135 middle barline right
- cout << "|";
- if(b == 67 || b == 125)
- {
- cout << endl;
- }
- break;
- case 68:
- case 126:
- //68 middle barline left
- if(b == 126)
- {
- cout << " |";
- break;
- }
- cout << " |";
- break;
- case 71: //1
- case 77:// 2
- case 83://3
- case 89: //4
- case 94: //5
- case 100: //6
- case 106: // 7
- case 112:
- case 118:
- case 123:
- if(counter < row)
- {
- cout << "P" << cpuTime[counter][0];
- counter++;
- }
- else{
- cout << " x";
- }
- break;
- case 69:
- case 74:
- case 80:
- case 86:
- case 92:
- case 98:
- case 103:
- case 109:
- case 114:
- case 120:
- if(idle_counter < row)
- {
- if(cpuTime[idle_counter][7] > 0)
- {
- cout << "I";
- idle_counter++;
- }
- else{
- cout << " ";
- idle_counter++;
- }
- }
- break;
- default:
- cout << " ";
- break;
- }
- }
- cout << endl;
- cout << " ";
- for(int a = 0 ; a < 68; a++)
- {
- cout << "-";
- }
- idle_counter= 0; // reset counter~~
- cout << endl;
- cout << " ";
- for(int label = 0 ; label < 68; label++)
- {
- switch(label)
- {
- case 0:
- case 7:
- case 14:
- case 21:
- case 28:
- case 35:
- case 42:
- case 49:
- case 56:
- case 63:
- case 70:
- if(arraycounter <= row)
- {
- if(arraycounter == row)
- {
- cout << final_time;
- arraycounter++;
- break;
- }
- if(cpuTime[arraycounter][7] > 0) // if next index of idle time > 0
- {
- if(arraycounter == 0)
- {
- cout << "|I|";
- }
- else{
- cout <<"\b\b\b\b" <<cpuTime[arraycounter-1][5] << "|I|";
- }
- }
- cout << cpuTime[arraycounter][6];
- arraycounter++;
- }
- else{
- cout << "-";
- }
- break;
- default:
- cout << " ";
- break;
- }
- }
- cout << endl;
- cout << endl;
- while(idle < row-1)
- {
- if(idle == 0)
- {
- cout << "Idle time before Process " << cpuTime[idle][0] << " starts " << cpuTime[idle_counter][7] << endl;
- idle_counter++;
- }
- cout << "Idle time between Process " << cpuTime[idle][0] << " and " << cpuTime[idle+1][0] << " is: " << cpuTime[idle_counter][7] << endl;
- idle++;
- idle_counter++;
- }
- for(int sortvar = 0 ; sortvar < row - 1 ; sortvar++)
- {
- for(int sort2 = 0; sort2 < row-sortvar-1 ; sort2++)
- {
- if(cpuTime[sort2][0] > cpuTime[sort2+1][0]) // according to arrival time
- {
- temp = cpuTime[sort2][0];
- cpuTime[sort2][0] = cpuTime[sort2+1][0]; // Process Number Swap
- cpuTime[sort2+1][0] = temp;
- temp= cpuTime[sort2][1];
- cpuTime[sort2][1] = cpuTime[sort2+1][1]; // Arrival Time Swap
- cpuTime[sort2+1][1] = temp;
- temp = cpuTime[sort2][2];
- cpuTime[sort2][2] = cpuTime[sort2+1][2]; // Burst Time Swap
- cpuTime[sort2+1][2] = temp;
- temp = cpuTime[sort2][3];
- cpuTime[sort2][3]= cpuTime[sort2+1][3];
- cpuTime[sort2+1][3] = temp;
- temp = cpuTime[sort2][4];
- cpuTime[sort2][4]= cpuTime[sort2+1][4];
- cpuTime[sort2+1][4] = temp;
- }
- }
- }
- cout << endl;
- cout << "+---------------------------------------------------------------------------+" << endl;
- cout << "Process ID Sorted With Calculated Waiting Time and Turn Around Time" << endl;
- cout << "+---------------------------------------------------------------------------+" << endl;
- cout << "PID\t" << "Arrival Time\t" << "Burst Time\t" << "Waiting Time\t" << "Turn Around Time\t";
- if(response == 3)
- {
- cout << "Priority";
- }
- cout << endl;
- for(int print = 0 ; print < row ; print++)
- {
- 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";
- if(response == 3)
- {
- cout << cpuTime[print][8];
- }
- cout << endl;
- }
- cout << "+---------------------------------------------------------------------------+" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement