-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_scheduling.cpp
More file actions
151 lines (122 loc) · 5.03 KB
/
priority_scheduling.cpp
File metadata and controls
151 lines (122 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
// Class to represent a Job
class Job {
public:
int id;
int burstTime;
int arrivalTime;
int priority;
// Constructor
Job(int jobId, int burst, int arrival, int pri) : id(jobId), burstTime(burst), arrivalTime(arrival), priority(pri) {}
};
// Structure to compare Jobs based on their priority
struct CompareJob {
bool operator()(const Job& job1, const Job& job2) {
return job1.priority > job2.priority;
}
};
// Function to calculate waiting time, turnaround time, starting time, and ending time for each Job
void calculateTimes(const vector<Job>& jobs, vector<int>& waitingTimes, vector<int>& turnaroundTimes, vector<int>& startingTimes, vector<int>& endingTimes) {
int n = jobs.size();
vector<int> remainingTimes(n, 0);
priority_queue<Job, vector<Job>, CompareJob> pq;
for (int i = 0; i < n; i++) {
remainingTimes[i] = jobs[i].burstTime;
}
int completedJobs = 0;
int currentTime = 0;
int prevJobId = -1;
while (completedJobs < n) {
for (int i = 0; i < n; i++) {
if (jobs[i].arrivalTime <= currentTime && remainingTimes[i] > 0 && i != prevJobId) {
pq.push(jobs[i]);
}
}
if (!pq.empty()) {
Job currentJob = pq.top();
pq.pop();
if (prevJobId != -1 && remainingTimes[prevJobId] > 0) {
pq.push(jobs[prevJobId]);
}
if (startingTimes[currentJob.id - 1] == -1) {
startingTimes[currentJob.id - 1] = currentTime;
}
remainingTimes[currentJob.id - 1]--;
prevJobId = currentJob.id - 1;
if (remainingTimes[prevJobId] == 0) {
completedJobs++;
int finishTime = currentTime + 1;
int waitingTime = finishTime - jobs[prevJobId].burstTime - jobs[prevJobId].arrivalTime;
int turnaroundTime = finishTime - jobs[prevJobId].arrivalTime;
waitingTimes[prevJobId] = waitingTime;
turnaroundTimes[prevJobId] = turnaroundTime;
endingTimes[prevJobId] = finishTime;
prevJobId = -1;
}
}
currentTime++;
}
}
// Function to calculate average waiting time and average turnaround time
void calculateAverageTimes(const vector<int>& waitingTimes, const vector<int>& turnaroundTimes) {
int n = waitingTimes.size();
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTimes[i];
totalTurnaroundTime += turnaroundTimes[i];
}
double averageWaitingTime = static_cast<double>(totalWaitingTime) / n;
double averageTurnaroundTime = static_cast<double>(totalTurnaroundTime) / n;
cout << "Average Waiting Time: " << averageWaitingTime << endl;
cout << "Average Turnaround Time: " << averageTurnaroundTime << endl;
}
// Function to display the job schedule table
void displayJobSchedule(const vector<Job>& jobs, const vector<int>& startingTimes, const vector<int>& endingTimes, const vector<int>& waitingTimes, const vector<int>& turnaroundTimes) {
int n = jobs.size();
cout << "Job Schedule Table:\n";
cout << "-----------------------------------------------------------\n";
cout << "Job ID\tStarting Time\tEnding Time\tWaiting Time\tTurnaround Time\n";
cout << "-----------------------------------------------------------\n";
for (int i = 0; i < n; i++) {
cout << jobs[i].id << "\t" << startingTimes[i] << "\t\t" << endingTimes[i] << "\t\t" << waitingTimes[i] << "\t\t" << turnaroundTimes[i] << endl;
}
cout << "-----------------------------------------------------------\n";
}
int main() {
int n;
cout << "Enter the number of jobs: ";
cin >> n;
vector<Job> jobs;
vector<int> waitingTimes(n);
vector<int> turnaroundTimes(n);
vector<int> startingTimes(n, -1);
vector<int> endingTimes(n, -1);
cout << "Enter the burst time, arrival time, and priority for each job:\n";
for (int i = 0; i < n; i++) {
int burst, arrival, priority;
cout << "Job " << i + 1 << ":\n";
cout << "Burst Time: ";
cin >> burst;
cout << "Arrival Time: ";
cin >> arrival;
cout << "Priority: ";
cin >> priority;
jobs.push_back(Job(i + 1, burst, arrival, priority));
}
// Sort jobs based on their arrival time
sort(jobs.begin(), jobs.end(), [](const Job& job1, const Job& job2) {
return job1.arrivalTime < job2.arrivalTime;
});
// Calculate waiting time, turnaround time, starting time, and ending time for each job
calculateTimes(jobs, waitingTimes, turnaroundTimes, startingTimes, endingTimes);
// Calculate average waiting time and average turnaround time
calculateAverageTimes(waitingTimes, turnaroundTimes);
// Display the job schedule table
displayJobSchedule(jobs, startingTimes, endingTimes, waitingTimes, turnaroundTimes);
return 0;
}