Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <limits> // For std::numeric_limits
- #include <iomanip> // For std::setw
- using namespace std;
- struct Process {
- int id;
- int arrival_time;
- int burst_time;
- int completion_time;
- int turnaround_time;
- int waiting_time;
- int response_time;
- int remaining_burst_time;
- };
- struct GanttChartEntry {
- int process_id;
- int start_time;
- int end_time;
- };
- void shortest_job_first(vector<Process>& processes, vector<GanttChartEntry>& gantt_chart) {
- int n = processes.size();
- int current_time = 0;
- int completed = 0;
- while (completed < n) {
- int min_burst_time = numeric_limits<int>::max();
- int idx = -1;
- for (int i = 0; i < n; ++i) {
- if (processes[i].arrival_time <= current_time &&
- processes[i].remaining_burst_time > 0 &&
- processes[i].remaining_burst_time < min_burst_time) {
- min_burst_time = processes[i].remaining_burst_time;
- idx = i;
- }
- }
- if (idx != -1) {
- // Set response time only if this is the first time the process is being executed
- if (processes[idx].remaining_burst_time == processes[idx].burst_time) {
- processes[idx].response_time = current_time - processes[idx].arrival_time;
- }
- // Record Gantt Chart Entry
- GanttChartEntry entry;
- entry.process_id = processes[idx].id;
- entry.start_time = current_time;
- current_time += processes[idx].remaining_burst_time;
- processes[idx].completion_time = current_time;
- processes[idx].turnaround_time = processes[idx].completion_time - processes[idx].arrival_time;
- processes[idx].waiting_time = processes[idx].turnaround_time - processes[idx].burst_time;
- processes[idx].remaining_burst_time = 0;
- ++completed;
- // End time for Gantt chart
- entry.end_time = current_time;
- gantt_chart.push_back(entry);
- } else {
- ++current_time; // If no process is ready, just increment time
- }
- }
- }
- void print_gantt_chart(const vector<GanttChartEntry>& gantt_chart) {
- cout << "\nGantt Chart:\n";
- cout << "-------------------------------------------------\n";
- // Print the processes in the Gantt chart
- for (const auto& entry : gantt_chart) {
- cout << "| P" << entry.process_id << " ";
- }
- cout << "|\n";
- cout << "-------------------------------------------------\n";
- // Print the start times for each process
- for (const auto& entry : gantt_chart) {
- cout << entry.start_time << "\t";
- }
- // Print the last end time
- cout << gantt_chart.back().end_time << "\n";
- }
- void print_results(const vector<Process>& processes) {
- cout << "\nProcess ID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\tResponse Time" << endl;
- for (const auto& p : processes) {
- cout << "P" << p.id << "\t\t"
- << p.arrival_time << "\t\t"
- << p.burst_time << "\t\t"
- << p.completion_time << "\t\t"
- << p.turnaround_time << "\t\t"
- << p.waiting_time << "\t\t"
- << p.response_time << endl;
- }
- }
- int main() {
- vector<Process> processes(5);
- vector<GanttChartEntry> gantt_chart;
- cout << "Enter arrival and burst times for 5 processes:" << endl;
- for (int i = 0; i < 5; ++i) {
- processes[i].id = i + 1;
- cout << "Arrival time for Process " << processes[i].id << ": ";
- cin >> processes[i].arrival_time;
- cout << "Burst time for Process " << processes[i].id << ": ";
- cin >> processes[i].burst_time;
- processes[i].remaining_burst_time = processes[i].burst_time;
- }
- shortest_job_first(processes, gantt_chart);
- print_results(processes);
- print_gantt_chart(gantt_chart);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement