Advertisement
xi_arma

Untitled

Sep 5th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <limits>  // For std::numeric_limits
  4. #include <iomanip> // For std::setw
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. struct Process {
  10.     int id;
  11.     int arrival_time;
  12.     int burst_time;
  13.     int completion_time;
  14.     int turnaround_time;
  15.     int waiting_time;
  16.     int response_time;
  17.     int remaining_burst_time;
  18. };
  19.  
  20. void shortest_job_first(vector<Process>& processes) {
  21.     int n = processes.size();
  22.     int current_time = 0;
  23.     int completed = 0;
  24.  
  25.     while (completed < n) {
  26.         int min_burst_time = numeric_limits<int>::max();
  27.         int idx = -1;
  28.  
  29.         for (int i = 0; i < n; ++i) {
  30.             if (processes[i].arrival_time <= current_time &&
  31.                 processes[i].remaining_burst_time > 0 &&
  32.                 processes[i].remaining_burst_time < min_burst_time) {
  33.                 min_burst_time = processes[i].remaining_burst_time;
  34.                 idx = i;
  35.             }
  36.         }
  37.  
  38.         if (idx != -1) {
  39.             if (processes[idx].remaining_burst_time == processes[idx].burst_time) {
  40.                 // Response Time
  41.                 processes[idx].response_time = current_time - processes[idx].arrival_time;
  42.             }
  43.  
  44.             current_time += processes[idx].remaining_burst_time;
  45.             processes[idx].completion_time = current_time;
  46.             processes[idx].turnaround_time = processes[idx].completion_time - processes[idx].arrival_time;
  47.             processes[idx].waiting_time = processes[idx].turnaround_time - processes[idx].burst_time;
  48.             processes[idx].remaining_burst_time = 0;
  49.             ++completed;
  50.         } else {
  51.             ++current_time;  // If no process is ready, just increment time
  52.         }
  53.     }
  54. }
  55.  
  56. void print_results(const vector<Process>& processes) {
  57.     cout << "\nProcess ID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\tResponse Time" << endl;
  58.     for (const auto& p : processes) {
  59.         cout << p.id << "\t\t"
  60.              << p.arrival_time << "\t\t"
  61.              << p.burst_time << "\t\t"
  62.              << p.completion_time << "\t\t"
  63.              << p.turnaround_time << "\t\t"
  64.              << p.waiting_time << "\t\t"
  65.              << p.response_time << endl;
  66.     }
  67. }
  68.  
  69. void print_gantt_chart(const vector<Process>& processes) {
  70.     map<int, int> time_to_process;  // Maps time unit to process ID
  71.  
  72.     for (const auto& p : processes) {
  73.         for (int t = p.arrival_time; t < p.completion_time; ++t) {
  74.             time_to_process[t] = p.id;
  75.         }
  76.     }
  77.  
  78.     cout << "\nGantt Chart:" << endl;
  79.  
  80.     int prev_time = 0;
  81.     for (const auto& entry : time_to_process) {
  82.         int t = entry.first;
  83.         cout << setw(3) << t;
  84.         prev_time = t;
  85.     }
  86.     cout << setw(3) << prev_time + 1 << endl;
  87.  
  88.     prev_time = 0;
  89.     for (const auto& entry : time_to_process) {
  90.         int t = entry.first;
  91.         if (t > prev_time) {
  92.             for (int i = prev_time; i < t; ++i) {
  93.                 cout << "   ";
  94.             }
  95.             cout << "P" << entry.second;
  96.             prev_time = t;
  97.         }
  98.     }
  99.     for (int i = prev_time; i <= (prev_time + 1); ++i) {
  100.         cout << "   ";
  101.     }
  102.     cout << endl;
  103. }
  104.  
  105. int main() {
  106.     vector<Process> processes(5);
  107.  
  108.     cout << "Enter arrival and burst times for 5 processes:" << endl;
  109.     for (int i = 0; i < 5; ++i) {
  110.         processes[i].id = i + 1;
  111.         cout << "Arrival time for Process " << processes[i].id << ": ";
  112.         cin >> processes[i].arrival_time;
  113.         cout << "Burst time for Process " << processes[i].id << ": ";
  114.         cin >> processes[i].burst_time;
  115.         processes[i].remaining_burst_time = processes[i].burst_time;
  116.     }
  117.  
  118.     shortest_job_first(processes);
  119.     print_results(processes);
  120.     print_gantt_chart(processes);
  121.  
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement