single cpu completed

This commit is contained in:
jazzy1902 2024-09-26 02:36:42 +05:30
parent b2ae341467
commit 296c926b7e
7 changed files with 290 additions and 181 deletions

View File

@ -1,6 +1,95 @@
{ {
"files.associations": { "files.associations": {
"ostream": "cpp", "ostream": "cpp",
"fstream": "cpp" "fstream": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"barrier": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"coroutine": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"source_location": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"latch": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ranges": "cpp",
"scoped_allocator": "cpp",
"semaphore": "cpp",
"shared_mutex": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"syncstream": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp",
"expected": "cpp",
"format": "cpp",
"spanstream": "cpp",
"stacktrace": "cpp",
"stdfloat": "cpp"
} }
} }

View File

@ -1,6 +1,7 @@
<html> <html>
<body> <body>
<pre> <pre>
0 2 2 1 -1 0 20 2 1 -1
1 2 2 1 -1 1 2 2 1 -1
2 1 2 1 -1
</pre></body></html> </pre></body></html>

View File

@ -1,58 +1,9 @@
P1,1 0 2 P1,1 0 5
P2,0 2 3 P2,1 5 7
P3,0 3 4 P3,1 7 8
P4,0 4 5 P1,1 8 13
P5,0 5 6 P2,2 13 14
P5,1 6 8 P3,2 14 15
P6,1 8 10 P1,1 15 20
P6,1 10 10 P1,1 20 25
P5,1 10 13 P1,2 27 28
P6,1 13 15
P5,2 15 18
P6,1 18 19
P4,1 19 28
P3,1 28 30
P4,1 30 90
P3,1 90 92
P4,2 92 122
P3,1 122 125
P4,3 125 195
P3,1 195 197
P4,4 197 207
P3,1 207 209
P4,5 209 219
P3,1 219 277
P2,1 277 279
P3,1 279 349
P2,1 349 351
P3,2 351 391
P2,1 391 394
P3,3 394 464
P2,1 464 466
P3,4 466 486
P2,1 486 488
P3,5 488 498
P2,1 498 566
P1,1 566 568
P2,1 568 648
P1,1 648 650
P2,2 650 700
P1,1 700 703
P2,3 703 773
P1,1 773 775
P2,4 775 815
P1,1 815 817
P2,5 817 827
P1,1 827 914
P7,1 914 916
P1,1 916 1006
P7,1 1006 1008
P1,2 1008 1088
P7,1 1088 1091
P1,3 1091 1161
P7,1 1161 1163
P1,4 1163 1223
P7,1 1223 1225
P1,5 1225 1235
P7,1 1235 1424
P7,2 1426 1429

0
lab3/src/cpu_times.txt Normal file
View File

BIN
lab3/src/scheduler Executable file

Binary file not shown.

View File

@ -15,6 +15,10 @@ struct process_detail {
vector<int> burst_times; vector<int> burst_times;
int in_cpu; int in_cpu;
int current_burst_index; int current_burst_index;
int arrvival_time = 0;
int wait_time = 0;
int cpu_time = 0;
int completion_time = 0;
}; };
struct clock{ struct clock{
@ -26,9 +30,10 @@ struct clock{
vector<process_detail> processes; vector<process_detail> processes;
queue<process_detail*> ready_queue_fifo; queue<process_detail*> ready_queue_fifo;
struct process_detail* CPU = NULL; struct process_detail* CPU = NULL;
vector<process_detail*> waiting;
ofstream output_file("cpu_times.txt"); ofstream output_file("cpu_times.txt");
vector<string> out_strings;
// -------------------------------------- THE FIFO -------------------------------------------------- // -------------------------------------- THE FIFO --------------------------------------------------
@ -41,14 +46,18 @@ void fifo() {
time.push_signal = 0; time.push_signal = 0;
int process_count = processes.size(); int process_count = processes.size();
int completed_processes = 0; int completed_processes = 0;
vector<process_detail*> waiting(process_count, NULL);
string out_string = "";
while(completed_processes < process_count){ while(completed_processes < process_count){
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction // ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
// breaking from the infinite loop // breaking from the infinite loop
for(int i = 0; i < process_count; ++i) { for (int j = 0; j < process_count; ++j) {
if(processes[i].burst_times[processes[i].current_burst_index] == -2) { if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
waiting[j] = NULL;
completed_processes++; completed_processes++;
} }
} }
@ -58,6 +67,7 @@ void fifo() {
//if process not in cpu //if process not in cpu
if(processes[i].in_cpu != 1) { if(processes[i].in_cpu != 1) {
if(time.timer == processes[i].burst_times[0]) { if(time.timer == processes[i].burst_times[0]) {
processes[i].arrvival_time = time.timer;
ready_queue_fifo.push(&processes[i]); ready_queue_fifo.push(&processes[i]);
processes[i].current_burst_index++; processes[i].current_burst_index++;
} }
@ -79,7 +89,8 @@ void fifo() {
CPU = ready_queue_fifo.front(); CPU = ready_queue_fifo.front();
CPU->in_cpu = 1; CPU->in_cpu = 1;
// Record in_time when the process enters the CPU // Record in_time when the process enters the CPU
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer; out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer;
ready_queue_fifo.pop(); ready_queue_fifo.pop();
} }
@ -88,16 +99,20 @@ void fifo() {
//check cpu_burst complete //check cpu_burst complete
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu == 1) { if(processes[i].in_cpu == 1) {
processes[i].cpu_time += 1;
if(CPU->burst_times[processes[i].current_burst_index] == 0){ if(CPU->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU // Record out_time when the process exits the CPU
output_file << " " << time.timer << endl; out_string += " " + to_string(time.timer);
out_strings.push_back(out_string);
// output_file << " " << time.timer << endl;
CPU->in_cpu = 0; CPU->in_cpu = 0;
CPU->current_burst_index++; CPU->current_burst_index++;
waiting.push_back(CPU); // process added to waiting queue waiting[CPU->pid] = CPU; // process added to waiting queue
if(!ready_queue_fifo.empty()) { if(!ready_queue_fifo.empty()) {
CPU = ready_queue_fifo.front(); // process added to CPU CPU = ready_queue_fifo.front(); // process added to CPU
CPU->in_cpu = 1; CPU->in_cpu = 1;
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue_fifo.pop(); ready_queue_fifo.pop();
} }
else { else {
@ -123,7 +138,6 @@ void fifo() {
time.timer++; time.timer++;
} }
output_file.close();
return; return;
} }
@ -134,7 +148,16 @@ void fifo() {
struct Compare { struct Compare {
bool operator()(process_detail* a, process_detail* b) { bool operator()(process_detail* a, process_detail* b) {
// Compare the elements in the vector at the given indices // Compare the elements in the vector at the given indices
return a->burst_times[a->current_burst_index] > b->burst_times[b->current_burst_index]; if (a->current_burst_index == 0) {
return a->burst_times[a->current_burst_index + 1] > b->burst_times[b->current_burst_index];
}
else if(b->current_burst_index == 0) {
return a->burst_times[a->current_burst_index] > b->burst_times[b->current_burst_index+1];
}
else if(b->current_burst_index == 0 && a->current_burst_index == 0)
return a->burst_times[a->current_burst_index+1] > b->burst_times[b->current_burst_index+1];
else return a->burst_times[a->current_burst_index] > b->burst_times[b->current_burst_index];
} }
}; };
@ -149,14 +172,19 @@ void sjf() {
time.push_signal = 0; time.push_signal = 0;
int process_count = processes.size(); int process_count = processes.size();
int completed_processes = 0; int completed_processes = 0;
// Initialize waiting vector with NULLs for each process slot
vector<process_detail*> waiting(process_count, NULL);
string out_string = "";
while(completed_processes < process_count){ while(completed_processes < process_count){
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction // ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
// breaking from the infinite loop // breaking from the infinite loop
for(int i = 0; i < process_count; ++i) { for (int j = 0; j < process_count; ++j) {
if(processes[i].burst_times[processes[i].current_burst_index] == -2) { if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
waiting[j] = NULL;
completed_processes++; completed_processes++;
} }
} }
@ -173,7 +201,7 @@ void sjf() {
} }
// managing waiting queue // managing waiting queue
for(int j = 0; j < waiting.size(); ++j) { for(int j = 0; j < process_count; ++j) {
if(waiting[j] != NULL) { if(waiting[j] != NULL) {
if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) { if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
ready_queue.push(waiting[j]); ready_queue.push(waiting[j]);
@ -187,24 +215,29 @@ void sjf() {
CPU = ready_queue.top(); CPU = ready_queue.top();
CPU->in_cpu = 1; CPU->in_cpu = 1;
// Record in_time when the process enters the CPU // Record in_time when the process enters the CPU
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
ready_queue.pop(); ready_queue.pop();
} }
else if(CPU != NULL){ if(CPU != NULL){
//check cpu_burst complete //check cpu_burst complete
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu == 1) { if(processes[i].in_cpu == 1) {
processes[i].cpu_time += 1;
if(CPU->burst_times[processes[i].current_burst_index] == 0){ if(CPU->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU // Record out_time when the process exits the CPU
output_file << " " << time.timer << endl; out_string += " " + to_string(time.timer);
out_strings.push_back(out_string);
// output_file << " " << time.timer << endl;
CPU->in_cpu = 0; CPU->in_cpu = 0;
CPU->current_burst_index++; CPU->current_burst_index++;
waiting.push_back(CPU); // process added to waiting queue waiting[CPU->pid] = CPU; // process added to waiting queue
if(!ready_queue.empty()) { if(!ready_queue.empty()) {
CPU = ready_queue.top(); // process added to CPU CPU = ready_queue.top(); // process added to CPU
CPU->in_cpu = 1; CPU->in_cpu = 1;
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
else { else {
@ -232,7 +265,6 @@ void sjf() {
time.timer++; time.timer++;
} }
output_file.close();
return; return;
} }
@ -247,14 +279,19 @@ void pre_sjf() {
time.push_signal = 0; time.push_signal = 0;
int process_count = processes.size(); int process_count = processes.size();
int completed_processes = 0; int completed_processes = 0;
// Initialize waiting vector with NULLs for each process slot
vector<process_detail*> waiting(process_count, NULL);
string out_string = "";
while(completed_processes < process_count){ while(completed_processes < process_count){
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction // ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
// breaking from the infinite loop // breaking from the infinite loop
for(int i = 0; i < process_count; ++i) { for (int j = 0; j < process_count; ++j) {
if(processes[i].burst_times[processes[i].current_burst_index] == -2) { if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
waiting[j] = NULL;
completed_processes++; completed_processes++;
} }
} }
@ -268,10 +305,13 @@ void pre_sjf() {
if(CPU != NULL) { if(CPU != NULL) {
ready_queue.push(CPU); ready_queue.push(CPU);
CPU->in_cpu = 0; CPU->in_cpu = 0;
output_file << " " << time.timer << endl; out_string += " " + to_string(time.timer);
out_strings.push_back(out_string);
// output_file << " " << time.timer << endl;
CPU = ready_queue.top(); CPU = ready_queue.top();
CPU->in_cpu = 1; CPU->in_cpu = 1;
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
@ -281,22 +321,25 @@ void pre_sjf() {
} }
// managing waiting queue // managing waiting queue
for(int j = 0; j < waiting.size(); ++j) { for(int j = 0; j < process_count; ++j) {
if(waiting[j] != NULL) { if(waiting[j] != NULL) {
if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) { if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
ready_queue.push(waiting[j]); ready_queue.push(waiting[j]);
if(CPU != NULL) { if(CPU != NULL) {
ready_queue.push(CPU); ready_queue.push(CPU);
CPU->in_cpu = 0; CPU->in_cpu = 0;
output_file << " " << time.timer << endl; out_string += " " + to_string(time.timer);
out_strings.push_back(out_string);
// output_file << " " << time.timer << endl;
CPU = ready_queue.top(); CPU = ready_queue.top();
CPU->in_cpu = 1; CPU->in_cpu = 1;
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
waiting[j]->current_burst_index++; waiting[j]->current_burst_index++;
waiting[j] = NULL; waiting[j] = NULL; // Process leaves waiting queue
} }
} }
} }
@ -305,24 +348,29 @@ void pre_sjf() {
CPU = ready_queue.top(); CPU = ready_queue.top();
CPU->in_cpu = 1; CPU->in_cpu = 1;
// Record in_time when the process enters the CPU // Record in_time when the process enters the CPU
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
ready_queue.pop(); ready_queue.pop();
} }
else if(CPU != NULL){ if(CPU != NULL){
//check cpu_burst complete //check cpu_burst complete
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu == 1) { if(processes[i].in_cpu == 1) {
processes[i].cpu_time += 1;
if(CPU->burst_times[processes[i].current_burst_index] == 0){ if(CPU->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU // Record out_time when the process exits the CPU
output_file << " " << time.timer << endl; out_string += " " + to_string(time.timer);
out_strings.push_back(out_string);
// output_file << " " << time.timer << endl;
CPU->in_cpu = 0; CPU->in_cpu = 0;
CPU->current_burst_index++; CPU->current_burst_index++;
waiting.push_back(CPU); // process added to waiting queue waiting[CPU->pid] = CPU; // process added to waiting queue
if(!ready_queue.empty()) { if(!ready_queue.empty()) {
CPU = ready_queue.top(); // process added to CPU CPU = ready_queue.top(); // process added to CPU
CPU->in_cpu = 1; CPU->in_cpu = 1;
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
else { else {
@ -340,6 +388,7 @@ void pre_sjf() {
} }
for(int j = 0; j < waiting.size(); ++j) { for(int j = 0; j < waiting.size(); ++j) {
// if(waiting[j] != NULL) waiting[j]->wait_time += 1;
if(waiting[j] != NULL) { if(waiting[j] != NULL) {
if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) { if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
// reducing the io burst till it reaches 0 // reducing the io burst till it reaches 0
@ -350,7 +399,6 @@ void pre_sjf() {
time.timer++; time.timer++;
} }
output_file.close();
return; return;
} }
@ -358,80 +406,82 @@ void pre_sjf() {
// ------------------------------------------- Round Robin -------------------------------------------------- // ------------------------------------------- Round Robin --------------------------------------------------
// vector<process_detail*> waiting; // vector<process_detail*> waiting;
void round_robin() { void round_robin() {
//clock initialized to 0
struct clock time; struct clock time;
memset(&time, 0, sizeof(struct clock)); memset(&time, 0, sizeof(struct clock));
time.timer = 0; time.timer = 0;
time.push_signal = 0; time.push_signal = 0;
int process_count = processes.size(); int process_count = processes.size();
// memset(&waiting, 0, process_count);
int completed_processes = 0; int completed_processes = 0;
int time_quantum = 5; int time_quantum = 5;
int current_quantum = 0; int current_quantum = 0;
// Initialize waiting vector with NULLs for each process slot
vector<process_detail*> waiting(process_count, NULL);
string out_string = "";
while (completed_processes < process_count) { while (completed_processes < process_count) {
// Check for process completion
// ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction for (int j = 0; j < process_count; ++j) {
if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == -2) {
// breaking from the infinite loop waiting[j]->completion_time = time.timer - waiting[j]->arrvival_time - 1;
for(int i = 0; i < process_count; ++i) { waiting[j] = NULL;
if(processes[i].burst_times[processes[i].current_burst_index] == -2) {
completed_processes++; completed_processes++;
} }
} }
//managing arrival times // Manage arrival times
for (int i = 0; i < process_count; ++i) { for (int i = 0; i < process_count; ++i) {
//if process not in cpu if (processes[i].in_cpu != 1 && time.timer == processes[i].burst_times[0]) {
if(processes[i].in_cpu != 1) {
if(time.timer == processes[i].burst_times[0]) {
ready_queue_fifo.push(&processes[i]); ready_queue_fifo.push(&processes[i]);
processes[i].current_burst_index++; processes[i].current_burst_index++;
} }
} }
}
// managing waiting queue // Manage waiting queue
for(int j = 0; j < waiting.size(); ++j) { for (int j = 0; j < process_count; ++j) {
if(waiting[j] != NULL) { if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
if(waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
ready_queue_fifo.push(waiting[j]); ready_queue_fifo.push(waiting[j]);
waiting[j]->current_burst_index++; waiting[j]->current_burst_index++;
waiting[j] = NULL; waiting[j] = NULL; // Process leaves waiting queue
}
} }
} }
// Assign a process to CPU if available
if (CPU == NULL && !ready_queue_fifo.empty()) { if (CPU == NULL && !ready_queue_fifo.empty()) {
CPU = ready_queue_fifo.front(); CPU = ready_queue_fifo.front();
CPU->in_cpu = 1; CPU->in_cpu = 1;
// Record in_time when the process enters the CPU out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // output_file << "P" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
ready_queue_fifo.pop(); ready_queue_fifo.pop();
current_quantum = time_quantum; current_quantum = time_quantum;
} }
else if(CPU != NULL){ if (CPU != NULL) {
//check cpu_burst complete
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu == 1){ if(processes[i].in_cpu == 1){
if(CPU->burst_times[processes[i].current_burst_index] == 0 || current_quantum == 0){ processes[i].cpu_time += 1;
// Record out_time when the process exits the CPU if (CPU->burst_times[CPU->current_burst_index] == 0 || current_quantum == 0) {
output_file << " " << time.timer << endl; // output_file << " " << time.timer << endl;
out_string += " " + to_string(time.timer);
out_strings.push_back(out_string);
CPU->in_cpu = 0; CPU->in_cpu = 0;
if(CPU->burst_times[processes[i].current_burst_index] == 0) CPU->current_burst_index++; if (CPU->burst_times[CPU->current_burst_index] == 0){
if(current_quantum == 0) ready_queue_fifo.push(CPU); CPU->current_burst_index++;
waiting.push_back(CPU); // process added to waiting queue waiting[CPU->pid] = CPU;
}
else if (current_quantum == 0) ready_queue_fifo.push(CPU);
// Place the process in its corresponding waiting slot by pid
if (!ready_queue_fifo.empty()) { if (!ready_queue_fifo.empty()) {
CPU = ready_queue_fifo.front(); // process added to CPU CPU = ready_queue_fifo.front();
CPU->in_cpu = 1; CPU->in_cpu = 1;
output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time out_string = "P" + to_string(CPU->pid+1) + "," + to_string((CPU->current_burst_index + 1 ) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
ready_queue_fifo.pop(); ready_queue_fifo.pop();
current_quantum = time_quantum; current_quantum = time_quantum;
} } else {
else {
CPU = NULL; CPU = NULL;
} }
} }
@ -439,29 +489,26 @@ void round_robin() {
} }
} }
// Process CPU burst and quantum
if (CPU != NULL) { if (CPU != NULL) {
CPU->burst_times[CPU->current_burst_index]--; CPU->burst_times[CPU->current_burst_index]--;
current_quantum--; current_quantum--;
} }
for(int j = 0; j < waiting.size(); ++j) { // Manage IO bursts in waiting queue
if(waiting[j] != NULL) { for (int j = 0; j < process_count; ++j) {
if(waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) { if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) {
waiting[j]->burst_times[waiting[j]->current_burst_index]--; // reducing the io burst till it reaches 0 waiting[j]->burst_times[waiting[j]->current_burst_index]--;
}
} }
} }
// Increment the timer
time.timer++; time.timer++;
} }
output_file.close();
return;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
if(argc != 3) if(argc != 3)
@ -476,7 +523,7 @@ int main(int argc, char **argv) {
char *scheduler_algorithm = argv[2]; char *scheduler_algorithm = argv[2];
ifstream file(file_to_search_in, ios::binary); ifstream file(file_to_search_in, ios::binary);
// ifstream file("process1.dat", ios::binary); // ifstream file("../WorkloadFiles/process1.dat", ios::binary);
string buffer; string buffer;
int pid = 0; int pid = 0;
@ -504,7 +551,10 @@ int main(int argc, char **argv) {
temp["rr"] = 4; temp["rr"] = 4;
string temp1 = scheduler_algorithm; string temp1 = scheduler_algorithm;
// string temp1 = "rr"; // string temp1 = "sjf";
// Start time point
auto start = std::chrono::high_resolution_clock::now();
switch(temp[temp1]){ switch(temp[temp1]){
case 1: case 1:
@ -523,5 +573,23 @@ int main(int argc, char **argv) {
cout << "enter fifo or sjf or pre_sjf or rr" << endl; cout << "enter fifo or sjf or pre_sjf or rr" << endl;
} }
// End time point
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
for(int i = 0; i < out_strings.size(); ++i){
output_file << out_strings[i] << endl;
}
output_file.close();
for(int i = 0; i < processes.size(); ++i) {
cout << "Process " << i+1 << " Completion Time: " << processes[i].completion_time << endl;
}
for(int i = 0; i < processes.size(); ++i) {
cout << "Process " << i+1 << " Waiting Time: " << processes[i].completion_time - processes[i].cpu_time << endl;
// cout << "Process " << i+1 << " Waiting Time: " << processes[i].wait_time << endl;
}
std::cout << "Execution time: " << duration.count() << " ms" << std::endl;
return 0; return 0;
} }