-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
139 lines (126 loc) · 4.51 KB
/
project.cpp
File metadata and controls
139 lines (126 loc) · 4.51 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
//Name: Daniel Mahler, Course: CISC 361-010 Operating Systems
/*
* Compilation Instructions:
* To compile this program, use the following command:
* g++ -std=c++11 -o project project.cpp
*
* Execution Instructions:
* To run the compiled program, execute:
* ./project
*
* Description:
* This program simulates three types of CPU scheduling algorithms:
* 1. First-Come, First-Served (FCFS)
* 2. Shortest Job First (SJF)
* 3. Round Robin (RR)
*
* The user is prompted to choose one of the three scheduling algorithms.
* The program then simulates the selected scheduling algorithm by creating 100 tasks.
* Each task has a unique task ID, creation time, and burst time.
* After selecting the simulation method, the program makes each unique task, sleeping for 1 second between tasks.
* After 100 seconds, the program will display the tasks in the order they were processed.
*/
#include <iostream>
#include <queue>
#include <vector>
#include <ctime>
#include <chrono>
#include <cstdlib> // For rand() and srand()
#include <thread> // For sleep_for()
using namespace std;
// Task structure to hold information about each task
struct TaskStruct {
time_t creationTime; // Time when the task was created
int taskid; // Unique identifier for the task
int burstTime; // Duration the task needs to run (used in SJF and RR)
};
// Comparator for prioritizing tasks based on burst time (Shortest Job First)
struct Comparator {
bool operator()(const TaskStruct& t1, const TaskStruct& t2) {
return t1.burstTime > t2.burstTime;
}
};
// Simulates First-Come, First-Served (FCFS) scheduling
void FCFS() {
queue<TaskStruct> queue;
for (int i = 0; i < 100; i++) {
TaskStruct task;
chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
task.creationTime = chrono::system_clock::to_time_t(now);
task.taskid = rand() % 100 + 1;
queue.push(task);
this_thread::sleep_for(chrono::seconds(1)); // Ensure unique creation times
}
while (!queue.empty()) {
TaskStruct task = queue.front();
queue.pop();
cout << "Working on task ID " << task.taskid << " received at time " << ctime(&task.creationTime);
}
}
// Simulates Shortest Job First (SJF) scheduling
void SJF() {
priority_queue<TaskStruct, vector<TaskStruct>, Comparator> queue;
for (int i = 0; i < 100; i++) {
TaskStruct task;
chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
task.creationTime = chrono::system_clock::to_time_t(now);
task.taskid = rand() % 100 + 1;
task.burstTime = rand() % 6 + 1;
queue.push(task);
this_thread::sleep_for(chrono::seconds(1));
}
while (!queue.empty()) {
TaskStruct task = queue.top();
queue.pop();
cout << "Working on task ID " << task.taskid << " with burst time " << task.burstTime << " at time " << ctime(&task.creationTime);
}
}
// Simulates Round Robin (RR) scheduling
void RR() {
int quantum;
cout << "Enter the time slice (quantum) for RR: ";
cin >> quantum;
queue<TaskStruct> queue;
for (int i = 0; i < 100; i++) {
TaskStruct task;
chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
task.creationTime = chrono::system_clock::to_time_t(now);
task.taskid = rand() % 100 + 1;
task.burstTime = rand() % 6 + 1;
queue.push(task);
this_thread::sleep_for(chrono::seconds(1));
}
while (!queue.empty()) {
TaskStruct task = queue.front();
queue.pop();
cout << "Working on task ID " << task.taskid << " with remaining burst time " << task.burstTime << " at time " << ctime(&task.creationTime);
if (task.burstTime > quantum) {
task.burstTime -= quantum;
queue.push(task);
cout << "Task ID " << task.taskid << " will Get Back to it LATER\n";
} else {
cout << "Task ID " << task.taskid << " is DONE\n";
}
}
}
// Main function to drive the simulation
int main() {
srand(static_cast<unsigned>(time(nullptr))); // Seed the random number generator
cout << "Choose your simulation\n1. FCFS\n2. SJF\n3. RR\nEnter your selection: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
FCFS();
break;
case 2:
SJF();
break;
case 3:
RR();
break;
default:
cout << "Invalid choice" << endl;
}
return 0;
}