Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 71 additions & 102 deletions CPU SCHEDULING/SJN.cpp
Original file line number Diff line number Diff line change
@@ -1,129 +1,98 @@
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Process
struct Process
{
public:
int PID;
double arrival_time;
double execution_time;
double starting_time;
double waiting_time;

Process()
{
}

Process(int PID, double arrival_time, double execution_time)
{
this->PID = PID;
this->arrival_time = arrival_time;
this->execution_time = execution_time;
starting_time = 0;
waiting_time = 0;
}

void Scan()
{
cout << "Enter PID : ";
cin >> PID;
cout << "Enter Arrival Time : ";
cin >> arrival_time;
cout << "Enter Execution Time : ";
cin >> execution_time;
starting_time = 0;
waiting_time = 0;
}

void Print()
{
cout << "|" << PID << "\t|\t" << arrival_time << "\t|\t" << execution_time << "\t|\t" << starting_time << "\t|\t" << waiting_time << "\t|" << endl;
}
int id;
int arrival_time;
int burst_time;
};

bool operator<(const Process &p1, const Process &p2)
bool compare_arrival_time(Process a, Process b)
{
return p1.execution_time > p2.execution_time;
return a.arrival_time < b.arrival_time;
}

bool comparator(Process a, Process b)
bool compare_burst_time(Process a, Process b)
{
return (a.arrival_time < b.arrival_time);
return a.burst_time < b.burst_time;
}

void Output(vector<Process> p)
int main()
{
cout << "\n\n-------------------------------------------------------------------------\n";
cout << "|PID\t|Arrival Time\t|Execution Time\t|Starting Time\t|Waiting Time\t|\n";
cout << "-------------------------------------------------------------------------\n";
for (auto i : p)
int n;
cout << "Enter the number of processes: ";
cin >> n;

vector<Process> processes(n);

for (int i = 0; i < n; i++)
{
i.Print();
cout << "Enter the process ID, arrival time, and burst time for process " << i + 1 << ": ";
cin >> processes[i].id >> processes[i].arrival_time >> processes[i].burst_time;
}
cout << "-------------------------------------------------------------------------\n";
}

int main()
{
// Sort processes based on arrival time
sort(processes.begin(), processes.end(), compare_arrival_time);

freopen("input_SJN.txt", "r", stdin);
// freopen("output.txt","w",stdout);
vector<int> completion_times(n);
vector<int> waiting_times(n);
vector<int> turnaround_times(n);

vector<Process> p;
int current_time = 0;

int n;
cout << "Enter the number of processes : ";
cin >> n;
for (int i = 0; i < n; i++)
{
// Sort processes based on burst time among those that have arrived
sort(processes.begin() + i, processes.end(), compare_burst_time);

// Calculate completion time of the current process
completion_times[i] = max(current_time, processes[i].arrival_time) + processes[i].burst_time;

// Calculate waiting time and turnaround time of the current process
waiting_times[i] = max(0, current_time - processes[i].arrival_time);
turnaround_times[i] = completion_times[i] - processes[i].arrival_time;

// Update current time
current_time = completion_times[i];
}

// Calculate average waiting time and turnaround time
double avg_waiting_time = 0;
double avg_turnaround_time = 0;
for (int i = 0; i < n; i++)
{
avg_waiting_time += waiting_times[i];
avg_turnaround_time += turnaround_times[i];
}
avg_waiting_time /= n;
avg_turnaround_time /= n;

// Print the Gantt chart
cout << "Gantt chart:" << endl;
current_time = processes[0].arrival_time;
for (int i = 0; i < n; i++)
{
Process temp;
temp.Scan();
p.push_back(temp);
cout << "| P" << processes[i].id << " ";
current_time = completion_times[i];
}
cout << "|" << endl;

sort(p.begin(), p.end(), comparator);
// Output(p);

priority_queue<Process> pq;
vector<Process> Final;

double st = 0, wt = 0;
pq.push(p[0]);
int i = 1;
while (!pq.empty())
{
// priority_queue<Process> tpq;
// while(!pq.empty()){
// Process tp = pq.top();
// tp.Print();
// tpq.push(tp);
// pq.pop();
// }
// while(!tpq.empty()){
// Process tp = tpq.top();
// pq.push(tp);
// tpq.pop();
// }
// cout<<endl;

Process temp = pq.top();
wt = st - temp.arrival_time;
temp.starting_time = st;
temp.waiting_time = wt;
st += temp.execution_time;
Final.push_back(temp);
pq.pop();

double upto = temp.starting_time + temp.execution_time;
// cout<<upto<<endl;
for(;p[i].arrival_time<=upto && i<n; i++){
// cout<<p[i].PID<<","<<p[i].arrival_time<<","<<upto<<endl;
pq.push(p[i]);
}
// Print process details, completion times, waiting times, and turnaround times
cout << endl
<< "Process Details:" << endl;
cout << "ID\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround Time" << endl;
for (int i = 0; i < n; i++)
{
cout << processes[i].id << "\t" << processes[i].arrival_time << "\t\t" << processes[i].burst_time << "\t\t" << completion_times[i] << "\t\t" << waiting_times[i] << "\t\t" << turnaround_times[i] << endl;
}

Output(Final);
// Print average waiting time and turnaround time
cout << endl
<< "Average Waiting Time: " << avg_waiting_time << endl;
cout << "Average Turnaround Time: " << avg_turnaround_time << endl;

return 0;
}
}