-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriority_NP.cpp
113 lines (96 loc) · 3.23 KB
/
Priority_NP.cpp
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
#include <iostream>
#include <vector>
using namespace std;
// Struct to represent a process
struct Process
{
int id;
int arrival_time;
int burst_time;
int priority;
};
// Function to calculate waiting time, turnaround time, and finish time for each process
void calculateWaitingTurnaroundFinishTime(vector<Process> &processes, vector<int> &waitingTime, vector<int> &turnaroundTime, vector<int> &finishTime)
{
int n = processes.size();
waitingTime[0] = 0;
turnaroundTime[0] = processes[0].burst_time;
finishTime[0] = processes[0].burst_time;
for (int i = 1; i < n; i++)
{
finishTime[i] = finishTime[i - 1] + processes[i].burst_time;
turnaroundTime[i] = finishTime[i] - processes[i].arrival_time;
waitingTime[i] = turnaroundTime[i] - processes[i].burst_time;
}
}
// Function to manually sort processes based on priority (higher priority first)
void manualSort(vector<Process> &processes)
{
int n = processes.size();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (processes[j].priority < processes[j + 1].priority)
{
swap(processes[j], processes[j + 1]);
}
}
}
}
// Function to display Gantt chart
void displayGanttChart(vector<Process> &processes, vector<int> &finishTime)
{
cout << "Gantt Chart:\n";
int totalTime = 0;
for (const Process &process : processes)
{
cout << "| P" << process.id << " ";
for (int i = 0; i < process.burst_time; i++)
{
cout << " ";
}
totalTime += process.burst_time;
}
cout << "|\n";
// Display timeline
cout << "0 ";
for (int time : finishTime)
{
cout << " " << time << " ";
}
cout << endl;
}
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
vector<int> waitingTime(n, 0);
vector<int> turnaroundTime(n, 0);
vector<int> finishTime(n, 0);
// Input process information
for (int i = 0; i < n; i++)
{
processes[i].id = i + 1;
cout << "Enter arrival time for process " << i + 1 << ": ";
cin >> processes[i].arrival_time;
cout << "Enter burst time for process " << i + 1 << ": ";
cin >> processes[i].burst_time;
cout << "Enter priority for process " << i + 1 << ": ";
cin >> processes[i].priority;
}
// Manually sort processes based on priority (higher priority first)
manualSort(processes);
calculateWaitingTurnaroundFinishTime(processes, waitingTime, turnaroundTime, finishTime);
// Display Gantt chart
displayGanttChart(processes, finishTime);
// Display tabular information
cout << "Process ID\tArrival Time\tBurst Time\tPriority\tTurnaround Time\tFinish Time\tWaiting Time\n";
for (int i = 0; i < n; i++)
{
cout << processes[i].id << "\t\t" << processes[i].arrival_time << "\t\t" << processes[i].burst_time << "\t\t" << processes[i].priority << "\t\t" << turnaroundTime[i] << "\t\t" << finishTime[i] << "\t\t" << waitingTime[i] << endl;
}
return 0;
}