Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Process
- {
- int number;
- int arrivalTime;
- int burstTime;
- int startTime;
- int endTime;
- int turnTime;
- int waitTime;
- int priority;
- };
- int main()
- {
- bool exit = false;
- while(!exit)
- {
- int option = 0;
- bool running = true;
- bool valid = false;
- char quit = ' ';
- int processes = 0;
- int tempPN = 0;
- int tempAT = 0;
- int tempBT = 0;
- int tempST = 0;
- int tempET = 0;
- int tempTT = 0;
- int tempWT = 0;
- int tempPR = 0;
- float turnTimeAve = 0;
- float waitTimeAve = 0;
- float burstTime = 0;
- cout << "==============================\n";
- cout << "| SCHEDULING ALGORITHM |\n";
- cout << "==============================\n";
- cout << "| OPTION |\n";
- cout << "| |\n";
- cout << "| [1] First-Come-First-Serve |\n";
- cout << "| [2] Shortest-Job-First |\n";
- cout << "| [3] Priority |\n";
- cout << "| [4] Quit |\n";
- cout << "| |\n";
- cout << "==============================\n";
- valid = false;
- while(!valid)
- {
- cout << "Enter No.: ";
- cin >> option;
- system("cls");
- if(cin.good() && option > 0 && option <= 4) valid = true;
- else
- {
- cout << "==============================\n";
- cout << "| SCHEDULING ALGORITHM |\n";
- cout << "==============================\n";
- cout << "| OPTION |\n";
- cout << "| |\n";
- cout << "| [1] First-Come-First-Serve |\n";
- cout << "| [2] Shortest-Job-First |\n";
- cout << "| [3] Priority |\n";
- cout << "| [4] Quit |\n";
- cout << "| |\n";
- cout << "==============================\n";
- cout << "Invalid option!\n";
- cin.clear();
- cin.ignore();
- }
- }
- switch(option)
- {
- case 1:
- system("cls");
- running = true;
- while(running)
- {
- cout << "First-Come-First-Serve (FCFS) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- valid = false;
- while(!valid)
- {
- cout << "Enter number of processes: ";
- cin >> processes;
- if(cin.good() && processes > 1) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- system("cls");
- cout << "First-Come-First-Serve (FCFS) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- Process prcs[processes];
- for(int i = 0; i < processes; i++)
- {
- cout << "Process No. " << i + 1 << endl;
- prcs[i].number = i + 1;
- valid = false;
- while(!valid)
- {
- cout << "Arrival Time: ";
- cin >> prcs[i].arrivalTime;
- if(cin.good() && prcs[i].arrivalTime >= 0) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- valid = false;
- while(!valid)
- {
- cout << "Burst Time: ";
- cin >> prcs[i].burstTime;
- if(cin.good() && prcs[i].burstTime > 0)
- {
- valid = true;
- cout << "\n";
- }
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- }
- system("cls");
- cout << "First-Come-First-Serve (FCFS) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- cout << "Unsorted Data" << endl;
- cout << "Process\t\tArrival Time\tBurst Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- cout << prcs[i].number << "\t\t" << prcs[i].arrivalTime << "\t\t" << prcs[i].burstTime << endl;
- }
- //Sorting to arrival time
- for(int i = 0; i < processes; i++)
- {
- for(int j = 0; j < processes - i - 1; j++)
- {
- if(prcs[j].arrivalTime > prcs[j + 1].arrivalTime)
- {
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- }
- }
- }
- cout << "\nSorted Data" << endl;
- cout << "Process\t\tArrival Time\tBurst Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- if(i == 0)
- {
- prcs[0].startTime = prcs[0].arrivalTime;
- prcs[0].endTime = prcs[0].arrivalTime + prcs[0].burstTime;
- }
- else
- {
- prcs[i].startTime = prcs[i - 1].endTime;
- if(prcs[i].arrivalTime > prcs[i].startTime) prcs[i].startTime = prcs[i].arrivalTime;
- prcs[i].endTime = prcs[i].startTime + prcs[i].burstTime;
- }
- cout << prcs[i].number << "\t\t" << prcs[i].arrivalTime << "\t\t" << prcs[i].burstTime << endl;
- }
- cout << "\nGantt Chart" << endl;
- //Top line
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "========";
- else if (i == 0 && prcs[0].startTime == 0) cout << "========";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << "========";
- if (i == processes - 1) cout << "========\n";
- else if (prcs[i].startTime != 0) cout << "========";
- }
- //Idle/Process No. Bar
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "[ IDLE ]";
- else if(i == 0 && prcs[0].startTime == 0) cout << "[ P" << prcs[0].number << " ]";
- else if(prcs[i].startTime != prcs[i - 1].endTime) cout << "[ IDLE ]";
- if (i == processes - 1) cout << "[ P" << prcs[i].number << " ]\n";
- else if (prcs[i].startTime != 0) cout << "[ P" << prcs[i].number << " ]";
- }
- //Bottom line
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "========";
- else if (i == 0 && prcs[0].startTime == 0) cout << "========";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << "========";
- if (i == processes - 1) cout << "========\n";
- else if (prcs[i].startTime != 0) cout << "========";
- }
- //Number label
- for(int i = 0; i < processes; i++)
- {
- if (i == 0 && prcs[0].startTime != 0) cout << "0 " << prcs[0].startTime << " " << prcs[0].endTime << " ";
- else if (i == 0 && prcs[0].startTime == 0) cout << prcs[0].startTime << " " << prcs[0].endTime << " ";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << prcs[i].startTime << " " << prcs[i].endTime << " ";
- else cout << prcs[i].endTime << " ";
- }
- //Sorting to process no.
- for(int i = 0; i < processes; i++)
- {
- for(int j = 0; j < processes - i - 1; j++)
- {
- if(prcs[j].number > prcs[j + 1].number)
- {
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempST = prcs[j + 1].startTime;
- prcs[j + 1].startTime = prcs[j].startTime;
- prcs[j].startTime = tempST;
- tempET = prcs[j + 1].endTime;
- prcs[j + 1].endTime = prcs[j].endTime;
- prcs[j].endTime = tempET;
- tempTT = prcs[j + 1].turnTime;
- prcs[j + 1].turnTime = prcs[j].turnTime;
- prcs[j].turnTime = tempTT;
- tempWT = prcs[j + 1].waitTime;
- prcs[j + 1].waitTime = prcs[j].waitTime;
- prcs[j].waitTime = tempWT;
- }
- }
- }
- cout << "\n\nTurnaround Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- prcs[i].turnTime = prcs[i].endTime - prcs[i].arrivalTime;
- cout << "TT[" << i + 1 << "] = " << prcs[i].endTime << " - " << prcs[i].arrivalTime << " = " << prcs[i].turnTime << endl;
- turnTimeAve += prcs[i].turnTime;
- }
- cout << "----------------------" << endl;
- printf("TT[A] = %.2f / %i = %.2f\n", turnTimeAve, processes, turnTimeAve / processes);
- turnTimeAve /= processes;
- cout << "\nWaiting Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- prcs[i].waitTime = prcs[i].startTime - prcs[i].arrivalTime;
- cout << "WT[" << i + 1 << "] = " << prcs[i].startTime << " - " << prcs[i].arrivalTime << " = " << prcs[i].waitTime << endl;
- waitTimeAve += prcs[i].waitTime;
- }
- cout << "----------------------" << endl;
- printf("WT[A] = %.2f / %i = %.2f\n", waitTimeAve, processes, waitTimeAve / processes);
- waitTimeAve /= processes;
- cout << "\n======================" << endl;
- cout << "TT - WT = BT" << endl;
- burstTime = turnTimeAve - waitTimeAve;
- printf("%.2f - %.2f = %.2f\n", turnTimeAve, waitTimeAve, burstTime);
- cout << "======================" << endl;
- valid = false;
- while(!valid)
- {
- cout << "Would you like to quit?\n";
- cout << "[Y] - Main Menu\n";
- cout << "[N] - Run again\n";
- cout << "[Y/N]: ";
- cin >> quit;
- if(cin.good() && quit == 'Y' || quit == 'y')
- {
- system("cls");
- valid = true;
- running = false;
- }
- else if (cin.good() && quit == 'N' || quit == 'n')
- {
- system("cls");
- valid = true;
- turnTimeAve = 0;
- waitTimeAve = 0;
- burstTime = 0;
- }
- else
- {
- cout << "Invalid input!\n";
- cin.clear();
- cin.ignore();
- }
- }
- }
- break;
- case 2:
- system("cls");
- running = true;
- while(running)
- {
- cout << "Shortest-Job-First (SJF) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- valid = false;
- while(!valid)
- {
- cout << "Enter number of processes: ";
- cin >> processes;
- if(cin.good() && processes > 1) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- system("cls");
- cout << "Shortest-Job-First (SJF) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- Process prcs[processes];
- for(int i = 0; i < processes; i++)
- {
- cout << "Process No. " << i + 1 << endl;
- prcs[i].number = i + 1;
- valid = false;
- while(!valid)
- {
- cout << "Arrival Time: ";
- cin >> prcs[i].arrivalTime;
- if(cin.good() && prcs[i].arrivalTime >= 0) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- valid = false;
- while(!valid)
- {
- cout << "Burst Time: ";
- cin >> prcs[i].burstTime;
- if(cin.good() && prcs[i].burstTime > 0)
- {
- valid = true;
- cout << "\n";
- }
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- }
- system("cls");
- cout << "Shortest-Job-First (SJF) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- cout << "Unsorted Data" << endl;
- cout << "Process\t\tArrival Time\tBurst Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- cout << prcs[i].number << "\t\t" << prcs[i].arrivalTime << "\t\t" << prcs[i].burstTime << endl;
- }
- //Sorting to arrival time
- for(int i = 0; i < processes; i++)
- {
- for(int j = 0; j < processes - i - 1; j++)
- {
- if(prcs[j].arrivalTime >= prcs[j + 1].arrivalTime)
- {
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- }
- }
- }
- for(int i = 0; i < processes; i++)
- {
- if(i == 0)
- {
- prcs[0].startTime = prcs[0].arrivalTime;
- prcs[0].endTime = prcs[0].arrivalTime + prcs[0].burstTime;
- }
- else
- {
- prcs[i].startTime = prcs[i - 1].endTime;
- if(prcs[i].arrivalTime > prcs[i].startTime) prcs[i].startTime = prcs[i].arrivalTime;
- prcs[i].endTime = prcs[i].startTime + prcs[i].burstTime;
- }
- }
- //Sorting to arrival time & burst time
- for (int i = 0; i < processes; i++)
- {
- for (int j = 1; j < processes - i - 1; j++)
- {
- //if (j != 0 && prcs[j].endTime >= prcs[j + 1].arrivalTime && prcs[j].burstTime > prcs[j + 1].burstTime)
- if(j != 0 && prcs[j].arrivalTime <= prcs[j - 1].endTime && prcs[j].burstTime > prcs[j + 1].burstTime)
- {
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- }
- }
- }
- cout << "\nSorted Data" << endl;
- cout << "Process\t\tArrival Time\tBurst Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- if(i == 0)
- {
- prcs[0].startTime = prcs[0].arrivalTime;
- prcs[0].endTime = prcs[0].arrivalTime + prcs[0].burstTime;
- }
- else
- {
- prcs[i].startTime = prcs[i - 1].endTime;
- if(prcs[i].arrivalTime > prcs[i].startTime) prcs[i].startTime = prcs[i].arrivalTime;
- prcs[i].endTime = prcs[i].startTime + prcs[i].burstTime;
- }
- cout << prcs[i].number << "\t\t" << prcs[i].arrivalTime << "\t\t" << prcs[i].burstTime << endl;
- }
- cout << "\nGantt Chart" << endl;
- //Top line
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "========";
- else if (i == 0 && prcs[0].startTime == 0) cout << "========";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << "========";
- if (i == processes - 1) cout << "========\n";
- else if (prcs[i].startTime != 0) cout << "========";
- }
- //Idle/Process No. Bar
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "[ IDLE ]";
- else if(i == 0 && prcs[0].startTime == 0) cout << "[ P" << prcs[0].number << " ]";
- else if(prcs[i].startTime != prcs[i - 1].endTime) cout << "[ IDLE ]";
- if (i == processes - 1) cout << "[ P" << prcs[i].number << " ]\n";
- else if (prcs[i].startTime != 0) cout << "[ P" << prcs[i].number << " ]";
- }
- //Bottom line
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "========";
- else if (i == 0 && prcs[0].startTime == 0) cout << "========";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << "========";
- if (i == processes - 1) cout << "========\n";
- else if (prcs[i].startTime != 0) cout << "========";
- }
- //Number label
- for(int i = 0; i < processes; i++)
- {
- if (i == 0 && prcs[0].startTime != 0) cout << "0 " << prcs[0].startTime << " " << prcs[0].endTime << " ";
- else if (i == 0 && prcs[0].startTime == 0) cout << prcs[0].startTime << " " << prcs[0].endTime << " ";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << prcs[i].startTime << " " << prcs[i].endTime << " ";
- else cout << prcs[i].endTime << " ";
- }
- //Sorting to process no.
- for(int i = 0; i < processes; i++)
- {
- for(int j = 0; j < processes - i - 1; j++)
- {
- if(prcs[j].number > prcs[j + 1].number)
- {
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempST = prcs[j + 1].startTime;
- prcs[j + 1].startTime = prcs[j].startTime;
- prcs[j].startTime = tempST;
- tempET = prcs[j + 1].endTime;
- prcs[j + 1].endTime = prcs[j].endTime;
- prcs[j].endTime = tempET;
- tempTT = prcs[j + 1].turnTime;
- prcs[j + 1].turnTime = prcs[j].turnTime;
- prcs[j].turnTime = tempTT;
- tempWT = prcs[j + 1].waitTime;
- prcs[j + 1].waitTime = prcs[j].waitTime;
- prcs[j].waitTime = tempWT;
- }
- }
- }
- cout << "\n\nTurnaround Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- prcs[i].turnTime = prcs[i].endTime - prcs[i].arrivalTime;
- cout << "TT[" << i + 1 << "] = " << prcs[i].endTime << " - " << prcs[i].arrivalTime << " = " << prcs[i].turnTime << endl;
- turnTimeAve += prcs[i].turnTime;
- }
- cout << "----------------------" << endl;
- printf("TT[A] = %.2f / %i = %.2f\n", turnTimeAve, processes, turnTimeAve / processes);
- turnTimeAve /= processes;
- cout << "\nWaiting Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- prcs[i].waitTime = prcs[i].startTime - prcs[i].arrivalTime;
- cout << "WT[" << i + 1 << "] = " << prcs[i].startTime << " - " << prcs[i].arrivalTime << " = " << prcs[i].waitTime << endl;
- waitTimeAve += prcs[i].waitTime;
- }
- cout << "----------------------" << endl;
- printf("WT[A] = %.2f / %i = %.2f\n", waitTimeAve, processes, waitTimeAve / processes);
- waitTimeAve /= processes;
- cout << "\n======================" << endl;
- cout << "TT - WT = BT" << endl;
- burstTime = turnTimeAve - waitTimeAve;
- printf("%.2f - %.2f = %.2f\n", turnTimeAve, waitTimeAve, burstTime);
- cout << "======================" << endl;
- valid = false;
- while(!valid)
- {
- cout << "Would you like to quit?\n";
- cout << "[Y] - Main Menu\n";
- cout << "[N] - Run again\n";
- cout << "[Y/N]: ";
- cin >> quit;
- if(cin.good() && quit == 'Y' || quit == 'y')
- {
- system("cls");
- valid = true;
- running = false;
- }
- else if (cin.good() && quit == 'N' || quit == 'n')
- {
- system("cls");
- valid = true;
- turnTimeAve = 0;
- waitTimeAve = 0;
- burstTime = 0;
- }
- else
- {
- cout << "Invalid input!\n";
- cin.clear();
- cin.ignore();
- }
- }
- }
- break;
- case 3:
- system("cls");
- running = true;
- while(running)
- {
- cout << "Priority (Prio) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- valid = false;
- while(!valid)
- {
- cout << "Enter number of processes: ";
- cin >> processes;
- if(cin.good() && processes > 1) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- system("cls");
- cout << "Priority (Prio) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- Process prcs[processes];
- for(int i = 0; i < processes; i++)
- {
- cout << "Process No. " << i + 1 << endl;
- prcs[i].number = i + 1;
- valid = false;
- while(!valid)
- {
- cout << "Arrival Time: ";
- cin >> prcs[i].arrivalTime;
- if(cin.good() && prcs[i].arrivalTime >= 0) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- valid = false;
- while(!valid)
- {
- cout << "Burst Time: ";
- cin >> prcs[i].burstTime;
- if(cin.good() && prcs[i].burstTime > 0) valid = true;
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- valid = false;
- while(!valid)
- {
- cout << "Priority: ";
- cin >> prcs[i].priority;
- if(cin.good() && prcs[i].priority >= 0)
- {
- valid = true;
- cout << "\n";
- }
- else
- {
- cin.clear();
- cin.ignore();
- }
- }
- }
- system("cls");
- cout << "Priority (Prio) Scheduling Algorithm" << endl;
- cout << "==================================================" << endl;
- cout << "Unsorted Data" << endl;
- cout << "Process\t\tArrival Time\tBurst Time\tPriority" << endl;
- for(int i = 0; i < processes; i++)
- {
- cout << prcs[i].number << "\t\t" << prcs[i].arrivalTime << "\t\t" << prcs[i].burstTime << "\t\t" << prcs[i].priority << endl;
- }
- //Sorting to arrival time
- for(int i = 0; i < processes; i++)
- {
- for(int j = 0; j < processes - i - 1; j++)
- {
- if(prcs[j].arrivalTime >= prcs[j + 1].arrivalTime)
- {
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- tempPR = prcs[j + 1].priority;
- prcs[j + 1].priority = prcs[j].priority;
- prcs[j].priority = tempPR;
- }
- }
- }
- for(int i = 0; i < processes; i++)
- {
- if(i == 0)
- {
- prcs[0].startTime = prcs[0].arrivalTime;
- prcs[0].endTime = prcs[0].arrivalTime + prcs[0].burstTime;
- }
- else
- {
- prcs[i].startTime = prcs[i - 1].endTime;
- if(prcs[i].arrivalTime > prcs[i].startTime) prcs[i].startTime = prcs[i].arrivalTime;
- prcs[i].endTime = prcs[i].startTime + prcs[i].burstTime;
- }
- }
- //Sorting to arrival time & priority
- for (int i = 0; i < processes; i++)
- {
- for (int j = 0; j < processes - i - 1; j++)
- {
- //if (j != 0 && prcs[j].endTime >= prcs[j + 1].arrivalTime && prcs[j].priority > prcs[j + 1].priority)
- if(j != 0 && prcs[j].arrivalTime <= prcs[j - 1].endTime && prcs[j].priority > prcs[j + 1].priority)
- {
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- tempPR = prcs[j + 1].priority;
- prcs[j + 1].priority = prcs[j].priority;
- prcs[j].priority = tempPR;
- }
- }
- }
- cout << "\nSorted Data" << endl;
- cout << "Process\t\tArrival Time\tBurst Time\tPriority" << endl;
- for(int i = 0; i < processes; i++)
- {
- if(i == 0)
- {
- prcs[0].startTime = prcs[0].arrivalTime;
- prcs[0].endTime = prcs[0].arrivalTime + prcs[0].burstTime;
- }
- else
- {
- prcs[i].startTime = prcs[i - 1].endTime;
- if(prcs[i].arrivalTime > prcs[i].startTime) prcs[i].startTime = prcs[i].arrivalTime;
- prcs[i].endTime = prcs[i].startTime + prcs[i].burstTime;
- }
- cout << prcs[i].number << "\t\t" << prcs[i].arrivalTime << "\t\t" << prcs[i].burstTime << "\t\t" << prcs[i].priority << endl;
- }
- cout << "\nGantt Chart" << endl;
- //Top line
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "========";
- else if (i == 0 && prcs[0].startTime == 0) cout << "========";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << "========";
- if (i == processes - 1) cout << "========\n";
- else if (prcs[i].startTime != 0) cout << "========";
- }
- //Idle/Process No. Bar
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "[ IDLE ]";
- else if(i == 0 && prcs[0].startTime == 0) cout << "[ P" << prcs[0].number << " ]";
- else if(prcs[i].startTime != prcs[i - 1].endTime) cout << "[ IDLE ]";
- if (i == processes - 1) cout << "[ P" << prcs[i].number << " ]\n";
- else if (prcs[i].startTime != 0) cout << "[ P" << prcs[i].number << " ]";
- }
- //Bottom line
- for(int i = 0; i < processes; i++)
- {
- if(i == 0 && prcs[0].startTime != 0) cout << "========";
- else if (i == 0 && prcs[0].startTime == 0) cout << "========";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << "========";
- if (i == processes - 1) cout << "========\n";
- else if (prcs[i].startTime != 0) cout << "========";
- }
- //Number label
- for(int i = 0; i < processes; i++)
- {
- if (i == 0 && prcs[0].startTime != 0) cout << "0 " << prcs[0].startTime << " " << prcs[0].endTime << " ";
- else if (i == 0 && prcs[0].startTime == 0) cout << prcs[0].startTime << " " << prcs[0].endTime << " ";
- else if (prcs[i].startTime != prcs[i - 1].endTime) cout << prcs[i].startTime << " " << prcs[i].endTime << " ";
- else cout << prcs[i].endTime << " ";
- }
- //Sorting to process no.
- for(int i = 0; i < processes; i++)
- {
- for(int j = 0; j < processes - i - 1; j++)
- {
- if(prcs[j].number > prcs[j + 1].number)
- {
- tempPN = prcs[j + 1].number;
- prcs[j + 1].number = prcs[j].number;
- prcs[j].number = tempPN;
- tempAT = prcs[j + 1].arrivalTime;
- prcs[j + 1].arrivalTime = prcs[j].arrivalTime;
- prcs[j].arrivalTime = tempAT;
- tempBT = prcs[j + 1].burstTime;
- prcs[j + 1].burstTime = prcs[j].burstTime;
- prcs[j].burstTime = tempBT;
- tempST = prcs[j + 1].startTime;
- prcs[j + 1].startTime = prcs[j].startTime;
- prcs[j].startTime = tempST;
- tempET = prcs[j + 1].endTime;
- prcs[j + 1].endTime = prcs[j].endTime;
- prcs[j].endTime = tempET;
- tempTT = prcs[j + 1].turnTime;
- prcs[j + 1].turnTime = prcs[j].turnTime;
- prcs[j].turnTime = tempTT;
- tempWT = prcs[j + 1].waitTime;
- prcs[j + 1].waitTime = prcs[j].waitTime;
- prcs[j].waitTime = tempWT;
- tempPR = prcs[j + 1].priority;
- prcs[j + 1].priority = prcs[j].priority;
- prcs[j].priority = tempPR;
- }
- }
- }
- cout << "\n\nTurnaround Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- prcs[i].turnTime = prcs[i].endTime - prcs[i].arrivalTime;
- cout << "TT[" << i + 1 << "] = " << prcs[i].endTime << " - " << prcs[i].arrivalTime << " = " << prcs[i].turnTime << endl;
- turnTimeAve += prcs[i].turnTime;
- }
- cout << "----------------------" << endl;
- printf("TT[A] = %.2f / %i = %.2f\n", turnTimeAve, processes, turnTimeAve / processes);
- turnTimeAve /= processes;
- cout << "\nWaiting Time" << endl;
- for(int i = 0; i < processes; i++)
- {
- prcs[i].waitTime = prcs[i].startTime - prcs[i].arrivalTime;
- cout << "WT[" << i + 1 << "] = " << prcs[i].startTime << " - " << prcs[i].arrivalTime << " = " << prcs[i].waitTime << endl;
- waitTimeAve += prcs[i].waitTime;
- }
- cout << "----------------------" << endl;
- printf("WT[A] = %.2f / %i = %.2f\n", waitTimeAve, processes, waitTimeAve / processes);
- waitTimeAve /= processes;
- cout << "\n======================" << endl;
- cout << "TT - WT = BT" << endl;
- burstTime = turnTimeAve - waitTimeAve;
- printf("%.2f - %.2f = %.2f\n", turnTimeAve, waitTimeAve, burstTime);
- cout << "======================" << endl;
- valid = false;
- while(!valid)
- {
- cout << "Would you like to quit?\n";
- cout << "[Y] - Main Menu\n";
- cout << "[N] - Run again\n";
- cout << "[Y/N]: ";
- cin >> quit;
- if(cin.good() && quit == 'Y' || quit == 'y')
- {
- system("cls");
- valid = true;
- running = false;
- }
- else if (cin.good() && quit == 'N' || quit == 'n')
- {
- system("cls");
- valid = true;
- turnTimeAve = 0;
- waitTimeAve = 0;
- burstTime = 0;
- }
- else
- {
- cout << "Invalid input!\n";
- cin.clear();
- cin.ignore();
- }
- }
- }
- break;
- case 4:
- exit = true;
- break;
- default:
- cout << "Invalid algorithm" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement