0% found this document useful (0 votes)
9 views3 pages

FCFS

Uploaded by

tejassmm222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

FCFS

Uploaded by

tejassmm222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>
#include <algorithm>

using namespace std;

struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
int completion_time;
};

bool compareArrivalTime(const Process& a, const Process& b) {


return a.arrival_time < b.arrival_time;
}

void calculateTimes(vector<Process>& processes) {


int current_time = 0;
processes[0].waiting_time = 0;
current_time = processes[0].arrival_time + processes[0].burst_time;
processes[0].completion_time = current_time;

for (int i = 1; i < processes.size(); i++) {


if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time;
}
processes[i].waiting_time = current_time - processes[i].arrival_time;
current_time += processes[i].burst_time;
processes[i].completion_time = current_time;
}
}

void calculateTurnaroundTime(vector<Process>& processes) {


for (int i = 0; i < processes.size(); i++) {
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
}
}

void displayResults(const vector<Process>& processes) {


int total_waiting_time = 0, total_turnaround_time = 0;
cout << "Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\t\tCompletion Time\n";
for (const Process& p : processes) {
total_waiting_time += p.waiting_time;
total_turnaround_time += p.turnaround_time;
cout << "P" << p.pid << "\t" << p.arrival_time << "\t\t" << p.burst_time << "\t\t"
<< p.waiting_time << "\t\t" << p.turnaround_time << "\t\t\t" << p.completion_time << "\n";
}

cout << "\nAverage Waiting Time: " << (float)total_waiting_time / processes.size() << "\n";
cout << "Average Turnaround Time: " << (float)total_turnaround_time / processes.size() << "\n";

cout << "\nGantt Chart:\n";


cout << "|";
int current_time = processes[0].arrival_time;
for (const Process& p : processes) {
if (current_time < p.arrival_time) {
cout << " idle |";
current_time = p.arrival_time;
}
cout << " P" << p.pid << " |";
current_time += p.burst_time;
}
cout << "\n" << processes[0].arrival_time;
current_time = processes[0].arrival_time;
for (const Process& p : processes) {
if (current_time < p.arrival_time) {
cout << "\t" << p.arrival_time;
current_time = p.arrival_time;

}
current_time += p.burst_time;
cout << "\t" << current_time;
}
cout << "\n";
}

int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;

vector<Process> processes(n);

for (int i = 0; i < n; i++) {


processes[i].pid = i + 1;
cout << "Enter arrival time for process P" << i + 1 << ": ";
cin >> processes[i].arrival_time;
cout << "Enter burst time for process P" << i + 1 << ": ";
cin >> processes[i].burst_time;
}
sort(processes.begin(), processes.end(), compareArrivalTime);

calculateTimes(processes);
calculateTurnaroundTime(processes);

displayResults(processes);

return 0;
}
//output:
//Enter the number of processes: 5
//Enter arrival time for process P1: 0
//Enter burst time for process P1: 4
//Enter arrival time for process P2: 1
//Enter burst time for process P2: 3
//Enter arrival time for process P3: 2
//Enter burst time for process P3: 1
//Enter arrival time for process P4: 3
//Enter burst time for process P4: 2
//Enter arrival time for process P5: 4
//Enter burst time for process P5: 5
//Process Arrival Time Burst Time Waiting Time Turnaround Time Completion Time
//P1 0 4 0 4 4
//P2 1 3 3 6 7
//P3 2 1 5 6 8
//P4 3 2 5 7 10
//P5 4 5 6 11 15
//
//Average Waiting Time: 3.8
//Average Turnaround Time: 6.8
//
//Gantt Chart:
//| P1 | P2 | P3 | P4 | P5 |
//0 4 7 8 10 15
//
//--------------------------------
//Process exited after 31.11 seconds with return value 0
//Press any key to continue . . .

You might also like