Advertisement
xi_arma

ACTIVITY 1 FR

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