Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <numeric>
- #include <cstdlib>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <climits>
- using namespace std;
- void executeFCFS() {
- // Implementation of First Come First Serve (FCFS) scheduling algorithm
- cout << "First Come First Serve (FCFS) Scheduling Algorithm" << endl;
- int n;
- cout << "Enter the number of processes: ";
- cin >> n;
- vector<int> arrivalTime(n);
- vector<int> burstTime(n);
- vector<int> completionTime(n);
- vector<int> turnaroundTime(n);
- vector<int> waitingTime(n);
- cout << "Enter arrival time and burst time for each process:" << endl;
- for (int i = 0; i < n; ++i) {
- cout << "Arrival time for process " << i + 1 << ": ";
- cin >> arrivalTime[i];
- cout << "Burst time for process " << i + 1 << ": ";
- cin >> burstTime[i];
- }
- // Calculate completion time, turnaround time, and waiting time
- completionTime[0] = burstTime[0] + arrivalTime[0];
- turnaroundTime[0] = completionTime[0] - arrivalTime[0];
- waitingTime[0] = turnaroundTime[0] - burstTime[0];
- for (int i = 1; i < n; ++i) {
- completionTime[i] = completionTime[i - 1] + burstTime[i];
- turnaroundTime[i] = completionTime[i] - arrivalTime[i];
- waitingTime[i] = turnaroundTime[i] - burstTime[i];
- }
- // Display results
- cout << "Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
- << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
- }
- // Calculate and display average turnaround time and average waiting time
- float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
- float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
- cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
- cout << "Average Waiting Time: " << avgWaitingTime << endl;
- }
- void executeSJFNonPreemptive() {
- // Implementation of Shortest Job First Non-Preemptive (SJF) scheduling algorithm
- cout << "SJF Non-Preemptive Scheduling Algorithm" << endl;
- int n;
- cout << "Enter the number of processes: ";
- cin >> n;
- vector<int> arrivalTime(n);
- vector<int> burstTime(n);
- vector<int> completionTime(n);
- vector<int> turnaroundTime(n);
- vector<int> waitingTime(n);
- vector<bool> visited(n, false);
- cout << "Enter arrival time and burst time for each process:" << endl;
- for (int i = 0; i < n; ++i) {
- cout << "Arrival time for process " << i + 1 << ": ";
- cin >> arrivalTime[i];
- cout << "Burst time for process " << i + 1 << ": ";
- cin >> burstTime[i];
- }
- int currentTime = 0;
- int completedProcesses = 0;
- while (completedProcesses < n) {
- int shortestJob = -1;
- int shortestBurst = INT_MAX;
- for (int i = 0; i < n; ++i) {
- if (!visited[i] && arrivalTime[i] <= currentTime && burstTime[i] < shortestBurst) {
- shortestBurst = burstTime[i];
- shortestJob = i;
- }
- }
- if (shortestJob == -1) {
- currentTime++;
- } else {
- completionTime[shortestJob] = currentTime + burstTime[shortestJob];
- turnaroundTime[shortestJob] = completionTime[shortestJob] - arrivalTime[shortestJob];
- waitingTime[shortestJob] = turnaroundTime[shortestJob] - burstTime[shortestJob];
- visited[shortestJob] = true;
- currentTime = completionTime[shortestJob];
- completedProcesses++;
- }
- }
- // Display results
- cout << "Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
- << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
- }
- // Calculate and display average turnaround time and average waiting time
- float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
- float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
- cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
- cout << "Average Waiting Time: " << avgWaitingTime << endl;
- }
- void executeSJFPreemptive() {
- // Implementation of Shortest Job First Preemptive (SJF) scheduling algorithm
- cout << "SJF Preemptive Scheduling Algorithm" << endl;
- int n;
- cout << "Enter the number of processes: ";
- cin >> n;
- vector<int> arrivalTime(n);
- vector<int> burstTime(n);
- vector<int> remainingTime(n); // Remaining time for each process
- vector<int> completionTime(n);
- vector<int> turnaroundTime(n);
- vector<int> waitingTime(n);
- cout << "Enter arrival time and burst time for each process:" << endl;
- for (int i = 0; i < n; ++i) {
- cout << "Arrival time for process " << i + 1 << ": ";
- cin >> arrivalTime[i];
- cout << "Burst time for process " << i + 1 << ": ";
- cin >> burstTime[i];
- remainingTime[i] = burstTime[i]; // Initialize remaining time
- }
- int currentTime = 0;
- int completedProcesses = 0;
- while (completedProcesses < n) {
- int shortestJob = -1;
- int shortestBurst = INT_MAX;
- for (int i = 0; i < n; ++i) {
- if (arrivalTime[i] <= currentTime && remainingTime[i] < shortestBurst && remainingTime[i] > 0) {
- shortestBurst = remainingTime[i];
- shortestJob = i;
- }
- }
- if (shortestJob == -1) {
- currentTime++;
- } else {
- remainingTime[shortestJob]--;
- if (remainingTime[shortestJob] == 0) {
- completionTime[shortestJob] = currentTime + 1;
- turnaroundTime[shortestJob] = completionTime[shortestJob] - arrivalTime[shortestJob];
- waitingTime[shortestJob] = turnaroundTime[shortestJob] - burstTime[shortestJob];
- completedProcesses++;
- }
- currentTime++;
- }
- }
- // Display results
- cout << "Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
- << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
- }
- // Calculate and display average turnaround time and average waiting time
- float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
- float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
- cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
- cout << "Average Waiting Time: " << avgWaitingTime << endl;
- }
- void executePriorityScheduling() {
- // Implementation of Priority Scheduling algorithm
- cout << "Priority Scheduling Algorithm" << endl;
- int n;
- cout << "Enter the number of processes: ";
- cin >> n;
- vector<int> arrivalTime(n);
- vector<int> burstTime(n);
- vector<int> priority(n);
- vector<int> completionTime(n);
- vector<int> turnaroundTime(n);
- vector<int> waitingTime(n);
- vector<bool> visited(n, false);
- cout << "Enter arrival time, burst time, and priority for each process:" << endl;
- for (int i = 0; i < n; ++i) {
- cout << "Arrival time for process " << i + 1 << ": ";
- cin >> arrivalTime[i];
- cout << "Burst time for process " << i + 1 << ": ";
- cin >> burstTime[i];
- cout << "Priority for process " << i + 1 << ": ";
- cin >> priority[i];
- }
- int currentTime = 0;
- int completedProcesses = 0;
- while (completedProcesses < n) {
- int highestPriorityJob = -1;
- int highestPriority = INT_MAX;
- for (int i = 0; i < n; ++i) {
- if (!visited[i] && arrivalTime[i] <= currentTime && priority[i] < highestPriority) {
- highestPriority = priority[i];
- highestPriorityJob = i;
- }
- }
- if (highestPriorityJob == -1) {
- currentTime++;
- } else {
- completionTime[highestPriorityJob] = currentTime + burstTime[highestPriorityJob];
- turnaroundTime[highestPriorityJob] = completionTime[highestPriorityJob] - arrivalTime[highestPriorityJob];
- waitingTime[highestPriorityJob] = turnaroundTime[highestPriorityJob] - burstTime[highestPriorityJob];
- visited[highestPriorityJob] = true;
- currentTime = completionTime[highestPriorityJob];
- completedProcesses++;
- }
- }
- // Display results
- cout << "Process\tArrival Time\tBurst Time\tPriority\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << priority[i] << "\t\t"
- << completionTime[i] << "\t\t" << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
- }
- // Calculate and display average turnaround time and average waiting time
- float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
- float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
- cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
- cout << "Average Waiting Time: " << avgWaitingTime << endl;
- }
- void executeRoundRobin() {
- // Implementation of Round Robin scheduling algorithm
- cout << "Round Robin Scheduling Algorithm" << endl;
- int n;
- cout << "Enter the number of processes: ";
- cin >> n;
- vector<int> burstTime(n);
- vector<int> completionTime(n);
- vector<int> turnaroundTime(n);
- vector<int> waitingTime(n);
- cout << "Enter burst time for each process:" << endl;
- for (int i = 0; i < n; ++i) {
- cout << "Burst time for process " << i + 1 << ": ";
- cin >> burstTime[i];
- }
- int timeQuantum;
- cout << "Enter the time quantum: ";
- cin >> timeQuantum;
- queue<int> processQueue; // Queue to store the processes
- vector<int> remainingTime(burstTime); // Remaining time for each process
- int currentTime = 0;
- int completedProcesses = 0;
- while (completedProcesses < n) {
- for (int i = 0; i < n; ++i) {
- if (remainingTime[i] > 0) {
- int executionTime = min(timeQuantum, remainingTime[i]);
- remainingTime[i] -= executionTime;
- currentTime += executionTime;
- if (remainingTime[i] == 0) {
- completionTime[i] = currentTime;
- turnaroundTime[i] = completionTime[i];
- waitingTime[i] = turnaroundTime[i] - burstTime[i];
- completedProcesses++;
- } else {
- processQueue.push(i); // Push the process back into the queue
- }
- }
- }
- // Handle the processes in the queue when time quantum expires
- if (!processQueue.empty()) {
- int nextProcess = processQueue.front();
- processQueue.pop();
- processQueue.push(nextProcess); // Move the next process to the end of the queue
- currentTime++;
- }
- }
- // Display results
- cout << "Process\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << "\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
- << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
- }
- // Calculate and display average turnaround time and average waiting time
- float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
- float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
- cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
- cout << "Average Waiting Time: " << avgWaitingTime << endl;
- }
- void displayMenu() {
- cout << "Scheduling Algorithms Menu" << endl;
- cout << "1. First Come First Serve (FCFS)" << endl;
- cout << "2. Shortest Job First Non-Preemptive (SJF)" << endl;
- cout << "3. Shortest Job First Preemptive (SJF Preemptive)" << endl;
- cout << "4. Priority Scheduling" << endl;
- cout << "5. Round Robin Scheduling" << endl;
- cout << "6. Exit" << endl;
- }
- void executeAlgorithm(int choice) {
- switch (choice) {
- case 1:
- executeFCFS();
- break;
- case 2:
- executeSJFNonPreemptive();
- break;
- case 3:
- executeSJFPreemptive();
- break;
- case 4:
- executePriorityScheduling();
- break;
- case 5:
- executeRoundRobin();
- break;
- case 6:
- cout << "Exiting..." << endl;
- exit(0);
- default:
- cout << "Invalid choice!" << endl;
- }
- }
- int main() {
- int choice;
- do {
- displayMenu();
- cout << "Enter your choice: ";
- cin >> choice;
- executeAlgorithm(choice);
- } while (choice != 6);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement