Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (140 loc) · 4.65 KB
/
Copy pathmain.cpp
File metadata and controls
157 lines (140 loc) · 4.65 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
152
153
154
155
156
157
#include "Scheduler.h"
#include <iostream>
using namespace std;
void loadDummyData(ProcessList &list) {
Process processes[] = {// ID, Arrival, Burst, Priority
{1, 0, 5, 3},
{2, 1, 3, 2},
{3, 2, 4, 1},
{4, 3, 2, 5},
{5, 50, 4, 4}};
for (int i = 0; i < 5; i++) {
list.addProcess(processes[i]);
}
cout << "\n✓ Loaded 5 dummy processes successfully!" << endl;
}
void enterManualData(ProcessList &list) {
int numProcesses;
cout << "\nEnter number of processes: ";
cin >> numProcesses;
for (int i = 0; i < numProcesses; i++) {
Process p;
cout << "\n--- Process " << (i + 1) << " ---" << endl;
p.id = i + 1;
cout << "Arrival Time: ";
cin >> p.arrivalTime;
cout << "Burst Time: ";
cin >> p.burstTime;
cout << "Priority (lower number = higher priority): ";
cin >> p.priority;
p.remainingTime = p.burstTime;
list.addProcess(p);
}
cout << "\n✓ Added " << numProcesses << " processes successfully!" << endl;
}
void displayProcessList(ProcessList &list) {
cout << "\n=== Current Process List ===" << endl;
cout << "ID\tArrival\tBurst\tPriority" << endl;
cout << "-----------------------------------" << endl;
Node *curr = list.getHead();
while (curr != nullptr) {
cout << "P" << curr->data.id << "\t" << curr->data.arrivalTime << "\t"
<< curr->data.burstTime << "\t" << curr->data.priority << endl;
curr = curr->next;
}
cout << "===================================" << endl;
}
int main() {
ProcessList list;
bool dataLoaded = false;
int choice;
cout << "\n╔════════════════════════════════════════╗" << endl;
cout << "║ CPU Scheduling Simulator (CLI) ║" << endl;
cout << "╚════════════════════════════════════════╝" << endl;
do {
cout << "\n--- Main Menu ---" << endl;
if (!dataLoaded) {
cout << "1. Load Dummy Process Data" << endl;
cout << "2. Enter Processes Manually" << endl;
cout << "3. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
list.clear();
loadDummyData(list);
displayProcessList(list);
dataLoaded = true;
break;
case 2:
list.clear();
enterManualData(list);
displayProcessList(list);
dataLoaded = true;
break;
case 3:
cout << "\nExiting... Goodbye!" << endl;
return 0;
default:
cout << "\n✗ Invalid choice! Please try again." << endl;
}
} else {
cout << "=== Scheduling Algorithms ===" << endl;
cout << "1. Run FCFS (First Come First Serve)" << endl;
cout << "2. Run SJF (Shortest Job First)" << endl;
cout << "3. Run SRTF (Preemptive SJF)" << endl;
cout << "4. Run Priority (Non-Preemptive)" << endl;
cout << "5. Run Preemptive Priority" << endl;
cout << "6. Run Round Robin" << endl;
cout << "7. View Process List" << endl;
cout << "8. Reset & Load New Data" << endl;
cout << "9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\n=== Running FCFS Algorithm ===" << endl;
Scheduler::runFCFS(list);
break;
case 2:
cout << "\n=== Running SJF (Non-Preemptive) ===" << endl;
Scheduler::runSJF(list, false);
break;
case 3:
cout << "\n=== Running SRTF (Preemptive SJF) ===" << endl;
Scheduler::runSJF(list, true);
break;
case 4:
cout << "\n=== Running Priority (Non-Preemptive) ===" << endl;
Scheduler::runPriority(list, false);
break;
case 5:
cout << "\n=== Running Preemptive Priority ===" << endl;
Scheduler::runPriority(list, true);
break;
case 6: {
int quantum;
cout << "\nEnter time quantum for Round Robin: ";
cin >> quantum;
cout << "\n=== Running Round Robin (Quantum=" << quantum
<< ") ===" << endl;
Scheduler::roundRobin(list, quantum);
break;
}
case 7:
displayProcessList(list);
break;
case 8:
dataLoaded = false;
cout << "\n✓ Reset successful! You can now load new data." << endl;
break;
case 9:
cout << "\nExiting... Goodbye!" << endl;
return 0;
default:
cout << "\n✗ Invalid choice! Please try again." << endl;
}
}
} while (true);
return 0;
}