Advertisement
Eternoseeker

OS ass2 menu driven - (round robin not correct)

Feb 19th, 2024
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.60 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <numeric>
  3. #include <cstdlib>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <climits>
  8. using namespace std;
  9.  
  10. void executeFCFS() {
  11.     // Implementation of First Come First Serve (FCFS) scheduling algorithm
  12.     cout << "First Come First Serve (FCFS) Scheduling Algorithm" << endl;
  13.    
  14.     int n;
  15.     cout << "Enter the number of processes: ";
  16.     cin >> n;
  17.  
  18.     vector<int> arrivalTime(n);
  19.     vector<int> burstTime(n);
  20.     vector<int> completionTime(n);
  21.     vector<int> turnaroundTime(n);
  22.     vector<int> waitingTime(n);
  23.  
  24.     cout << "Enter arrival time and burst time for each process:" << endl;
  25.     for (int i = 0; i < n; ++i) {
  26.         cout << "Arrival time for process " << i + 1 << ": ";
  27.         cin >> arrivalTime[i];
  28.         cout << "Burst time for process " << i + 1 << ": ";
  29.         cin >> burstTime[i];
  30.     }
  31.  
  32.     // Calculate completion time, turnaround time, and waiting time
  33.     completionTime[0] = burstTime[0] + arrivalTime[0];
  34.     turnaroundTime[0] = completionTime[0] - arrivalTime[0];
  35.     waitingTime[0] = turnaroundTime[0] - burstTime[0];
  36.  
  37.     for (int i = 1; i < n; ++i) {
  38.         completionTime[i] = completionTime[i - 1] + burstTime[i];
  39.         turnaroundTime[i] = completionTime[i] - arrivalTime[i];
  40.         waitingTime[i] = turnaroundTime[i] - burstTime[i];
  41.     }
  42.  
  43.     // Display results
  44.     cout << "Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
  45.     for (int i = 0; i < n; ++i) {
  46.         cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
  47.              << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
  48.     }
  49.  
  50.     // Calculate and display average turnaround time and average waiting time
  51.     float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
  52.     float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
  53.     cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
  54.     cout << "Average Waiting Time: " << avgWaitingTime << endl;
  55. }
  56.  
  57. void executeSJFNonPreemptive() {
  58.     // Implementation of Shortest Job First Non-Preemptive (SJF) scheduling algorithm
  59.     cout << "SJF Non-Preemptive Scheduling Algorithm" << endl;
  60.     int n;
  61.     cout << "Enter the number of processes: ";
  62.     cin >> n;
  63.  
  64.     vector<int> arrivalTime(n);
  65.     vector<int> burstTime(n);
  66.     vector<int> completionTime(n);
  67.     vector<int> turnaroundTime(n);
  68.     vector<int> waitingTime(n);
  69.     vector<bool> visited(n, false);
  70.  
  71.     cout << "Enter arrival time and burst time for each process:" << endl;
  72.     for (int i = 0; i < n; ++i) {
  73.         cout << "Arrival time for process " << i + 1 << ": ";
  74.         cin >> arrivalTime[i];
  75.         cout << "Burst time for process " << i + 1 << ": ";
  76.         cin >> burstTime[i];
  77.     }
  78.  
  79.     int currentTime = 0;
  80.     int completedProcesses = 0;
  81.     while (completedProcesses < n) {
  82.         int shortestJob = -1;
  83.         int shortestBurst = INT_MAX;
  84.  
  85.         for (int i = 0; i < n; ++i) {
  86.             if (!visited[i] && arrivalTime[i] <= currentTime && burstTime[i] < shortestBurst) {
  87.                 shortestBurst = burstTime[i];
  88.                 shortestJob = i;
  89.             }
  90.         }
  91.  
  92.         if (shortestJob == -1) {
  93.             currentTime++;
  94.         } else {
  95.             completionTime[shortestJob] = currentTime + burstTime[shortestJob];
  96.             turnaroundTime[shortestJob] = completionTime[shortestJob] - arrivalTime[shortestJob];
  97.             waitingTime[shortestJob] = turnaroundTime[shortestJob] - burstTime[shortestJob];
  98.             visited[shortestJob] = true;
  99.             currentTime = completionTime[shortestJob];
  100.             completedProcesses++;
  101.         }
  102.     }
  103.  
  104.     // Display results
  105.     cout << "Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
  106.     for (int i = 0; i < n; ++i) {
  107.         cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
  108.              << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
  109.     }
  110.  
  111.     // Calculate and display average turnaround time and average waiting time
  112.     float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
  113.     float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
  114.     cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
  115.     cout << "Average Waiting Time: " << avgWaitingTime << endl;
  116. }
  117.  
  118. void executeSJFPreemptive() {
  119.     // Implementation of Shortest Job First Preemptive (SJF) scheduling algorithm
  120.     cout << "SJF Preemptive Scheduling Algorithm" << endl;
  121.     int n;
  122.     cout << "Enter the number of processes: ";
  123.     cin >> n;
  124.  
  125.     vector<int> arrivalTime(n);
  126.     vector<int> burstTime(n);
  127.     vector<int> remainingTime(n); // Remaining time for each process
  128.     vector<int> completionTime(n);
  129.     vector<int> turnaroundTime(n);
  130.     vector<int> waitingTime(n);
  131.  
  132.     cout << "Enter arrival time and burst time for each process:" << endl;
  133.     for (int i = 0; i < n; ++i) {
  134.         cout << "Arrival time for process " << i + 1 << ": ";
  135.         cin >> arrivalTime[i];
  136.         cout << "Burst time for process " << i + 1 << ": ";
  137.         cin >> burstTime[i];
  138.         remainingTime[i] = burstTime[i]; // Initialize remaining time
  139.     }
  140.  
  141.     int currentTime = 0;
  142.     int completedProcesses = 0;
  143.     while (completedProcesses < n) {
  144.         int shortestJob = -1;
  145.         int shortestBurst = INT_MAX;
  146.  
  147.         for (int i = 0; i < n; ++i) {
  148.             if (arrivalTime[i] <= currentTime && remainingTime[i] < shortestBurst && remainingTime[i] > 0) {
  149.                 shortestBurst = remainingTime[i];
  150.                 shortestJob = i;
  151.             }
  152.         }
  153.  
  154.         if (shortestJob == -1) {
  155.             currentTime++;
  156.         } else {
  157.             remainingTime[shortestJob]--;
  158.             if (remainingTime[shortestJob] == 0) {
  159.                 completionTime[shortestJob] = currentTime + 1;
  160.                 turnaroundTime[shortestJob] = completionTime[shortestJob] - arrivalTime[shortestJob];
  161.                 waitingTime[shortestJob] = turnaroundTime[shortestJob] - burstTime[shortestJob];
  162.                 completedProcesses++;
  163.             }
  164.             currentTime++;
  165.         }
  166.     }
  167.  
  168.     // Display results
  169.     cout << "Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
  170.     for (int i = 0; i < n; ++i) {
  171.         cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
  172.              << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
  173.     }
  174.  
  175.     // Calculate and display average turnaround time and average waiting time
  176.     float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
  177.     float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
  178.     cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
  179.     cout << "Average Waiting Time: " << avgWaitingTime << endl;
  180. }
  181.  
  182. void executePriorityScheduling() {
  183.     // Implementation of Priority Scheduling algorithm
  184.     cout << "Priority Scheduling Algorithm" << endl;
  185.     int n;
  186.     cout << "Enter the number of processes: ";
  187.     cin >> n;
  188.  
  189.     vector<int> arrivalTime(n);
  190.     vector<int> burstTime(n);
  191.     vector<int> priority(n);
  192.     vector<int> completionTime(n);
  193.     vector<int> turnaroundTime(n);
  194.     vector<int> waitingTime(n);
  195.     vector<bool> visited(n, false);
  196.  
  197.     cout << "Enter arrival time, burst time, and priority for each process:" << endl;
  198.     for (int i = 0; i < n; ++i) {
  199.         cout << "Arrival time for process " << i + 1 << ": ";
  200.         cin >> arrivalTime[i];
  201.         cout << "Burst time for process " << i + 1 << ": ";
  202.         cin >> burstTime[i];
  203.         cout << "Priority for process " << i + 1 << ": ";
  204.         cin >> priority[i];
  205.     }
  206.  
  207.     int currentTime = 0;
  208.     int completedProcesses = 0;
  209.     while (completedProcesses < n) {
  210.         int highestPriorityJob = -1;
  211.         int highestPriority = INT_MAX;
  212.  
  213.         for (int i = 0; i < n; ++i) {
  214.             if (!visited[i] && arrivalTime[i] <= currentTime && priority[i] < highestPriority) {
  215.                 highestPriority = priority[i];
  216.                 highestPriorityJob = i;
  217.             }
  218.         }
  219.  
  220.         if (highestPriorityJob == -1) {
  221.             currentTime++;
  222.         } else {
  223.             completionTime[highestPriorityJob] = currentTime + burstTime[highestPriorityJob];
  224.             turnaroundTime[highestPriorityJob] = completionTime[highestPriorityJob] - arrivalTime[highestPriorityJob];
  225.             waitingTime[highestPriorityJob] = turnaroundTime[highestPriorityJob] - burstTime[highestPriorityJob];
  226.             visited[highestPriorityJob] = true;
  227.             currentTime = completionTime[highestPriorityJob];
  228.             completedProcesses++;
  229.         }
  230.     }
  231.  
  232.     // Display results
  233.     cout << "Process\tArrival Time\tBurst Time\tPriority\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
  234.     for (int i = 0; i < n; ++i) {
  235.         cout << i + 1 << "\t" << arrivalTime[i] << "\t\t" << burstTime[i] << "\t\t" << priority[i] << "\t\t"
  236.              << completionTime[i] << "\t\t" << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
  237.     }
  238.  
  239.     // Calculate and display average turnaround time and average waiting time
  240.     float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
  241.     float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
  242.     cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
  243.     cout << "Average Waiting Time: " << avgWaitingTime << endl;
  244. }
  245.  
  246. void executeRoundRobin() {
  247.     // Implementation of Round Robin scheduling algorithm
  248.     cout << "Round Robin Scheduling Algorithm" << endl;
  249.    
  250.     int n;
  251.     cout << "Enter the number of processes: ";
  252.     cin >> n;
  253.  
  254.     vector<int> burstTime(n);
  255.     vector<int> completionTime(n);
  256.     vector<int> turnaroundTime(n);
  257.     vector<int> waitingTime(n);
  258.  
  259.     cout << "Enter burst time for each process:" << endl;
  260.     for (int i = 0; i < n; ++i) {
  261.         cout << "Burst time for process " << i + 1 << ": ";
  262.         cin >> burstTime[i];
  263.     }
  264.  
  265.     int timeQuantum;
  266.     cout << "Enter the time quantum: ";
  267.     cin >> timeQuantum;
  268.  
  269.     queue<int> processQueue; // Queue to store the processes
  270.     vector<int> remainingTime(burstTime); // Remaining time for each process
  271.  
  272.     int currentTime = 0;
  273.     int completedProcesses = 0;
  274.     while (completedProcesses < n) {
  275.         for (int i = 0; i < n; ++i) {
  276.             if (remainingTime[i] > 0) {
  277.                 int executionTime = min(timeQuantum, remainingTime[i]);
  278.                 remainingTime[i] -= executionTime;
  279.                 currentTime += executionTime;
  280.  
  281.                 if (remainingTime[i] == 0) {
  282.                     completionTime[i] = currentTime;
  283.                     turnaroundTime[i] = completionTime[i];
  284.                     waitingTime[i] = turnaroundTime[i] - burstTime[i];
  285.                     completedProcesses++;
  286.                 } else {
  287.                     processQueue.push(i); // Push the process back into the queue
  288.                 }
  289.             }
  290.         }
  291.  
  292.         // Handle the processes in the queue when time quantum expires
  293.         if (!processQueue.empty()) {
  294.             int nextProcess = processQueue.front();
  295.             processQueue.pop();
  296.             processQueue.push(nextProcess); // Move the next process to the end of the queue
  297.             currentTime++;
  298.         }
  299.     }
  300.  
  301.     // Display results
  302.     cout << "Process\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time" << endl;
  303.     for (int i = 0; i < n; ++i) {
  304.         cout << i + 1 << "\t" << burstTime[i] << "\t\t" << completionTime[i] << "\t\t"
  305.              << turnaroundTime[i] << "\t\t" << waitingTime[i] << endl;
  306.     }
  307.  
  308.     // Calculate and display average turnaround time and average waiting time
  309.     float avgTurnaroundTime = accumulate(turnaroundTime.begin(), turnaroundTime.end(), 0) / static_cast<float>(n);
  310.     float avgWaitingTime = accumulate(waitingTime.begin(), waitingTime.end(), 0) / static_cast<float>(n);
  311.     cout << "Average Turnaround Time: " << avgTurnaroundTime << endl;
  312.     cout << "Average Waiting Time: " << avgWaitingTime << endl;
  313. }
  314.  
  315. void displayMenu() {
  316.     cout << "Scheduling Algorithms Menu" << endl;
  317.     cout << "1. First Come First Serve (FCFS)" << endl;
  318.     cout << "2. Shortest Job First Non-Preemptive (SJF)" << endl;
  319.     cout << "3. Shortest Job First Preemptive (SJF Preemptive)" << endl;
  320.     cout << "4. Priority Scheduling" << endl;
  321.     cout << "5. Round Robin Scheduling" << endl;
  322.     cout << "6. Exit" << endl;
  323. }
  324.  
  325. void executeAlgorithm(int choice) {
  326.     switch (choice) {
  327.         case 1:
  328.             executeFCFS();
  329.             break;
  330.         case 2:
  331.             executeSJFNonPreemptive();
  332.             break;
  333.         case 3:
  334.             executeSJFPreemptive();
  335.             break;
  336.         case 4:
  337.             executePriorityScheduling();
  338.             break;
  339.         case 5:
  340.             executeRoundRobin();
  341.             break;
  342.         case 6:
  343.             cout << "Exiting..." << endl;
  344.             exit(0);
  345.         default:
  346.             cout << "Invalid choice!" << endl;
  347.     }
  348. }
  349.  
  350. int main() {
  351.     int choice;
  352.     do {
  353.         displayMenu();
  354.         cout << "Enter your choice: ";
  355.         cin >> choice;
  356.         executeAlgorithm(choice);
  357.     } while (choice != 6);
  358.  
  359.     return 0;
  360. }
  361.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement