Almost ho hi gya lab3

This commit is contained in:
jazzy1902 2024-09-21 22:54:41 +05:30
parent 0361dedfab
commit 50cc1bb26b
10 changed files with 392 additions and 115 deletions

7
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -1,6 +1,8 @@
{ {
"files.associations": { "files.associations": {
"ostream": "cpp", "ostream": "cpp",
"chrono": "cpp" "chrono": "cpp",
"iostream": "cpp",
"iosfwd": "cpp"
} }
} }

28
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-13 build active file",
"command": "/usr/bin/g++-13",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

6
lab3/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"ostream": "cpp",
"fstream": "cpp"
}
}

28
lab3/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-13 build active file",
"command": "/usr/bin/g++-13",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

View File

@ -1,4 +1,32 @@
P1 0 2 CPU1
P2 2 4 P1,1 0 100
P1 6 7 P5,2 100 103
P2 9 10 P1,2 103 193
P6,1 193 198
P1,3 198 278
P4,2 278 338
P2,5 338 378
P1,5 378 438
P3,1 438 508
P4,6 508 518
P3,2 518 588
P3,3 590 630
P3,4 633 703
P3,5 705 725
P3,6 727 737
CPU2
P2,1 2 82
P5,1 82 85
P2,2 85 165
P5,3 165 168
P2,3 168 218
P4,1 218 228
P2,4 228 298
P1,4 298 368
P4,3 368 398
P2,6 398 408
P4,4 408 478
P1,6 478 488
P4,5 488 498
P7,1 498 698
P7,2 700 703

BIN
lab3/multi_core_scheduler Executable file

Binary file not shown.

View File

@ -29,6 +29,8 @@ queue<process_detail*> ready_queue_fifo;
vector<process_detail*> waiting; vector<process_detail*> waiting;
process_detail* CPU1 = NULL; process_detail* CPU1 = NULL;
process_detail* CPU2 = NULL; process_detail* CPU2 = NULL;
vector<string> out_cpu1;
vector<string> out_cpu2;
ofstream output_file("cpu_times.txt"); ofstream output_file("cpu_times.txt");
@ -76,23 +78,16 @@ void fifo() {
} }
if (CPU1 == NULL && !ready_queue_fifo.empty()) { if (CPU1 == NULL && !ready_queue_fifo.empty()) {
// Assign the first process from the ready queue to the CPU
CPU1 = ready_queue_fifo.front(); CPU1 = ready_queue_fifo.front();
CPU1->in_cpu1 = 1; CPU1->in_cpu1 = 1;
// Record in_time when the process enters the CPU out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
out_string1 = "P" + to_string(CPU1->pid+1) + ",1 " + to_string(time.timer);
// output_file << "P" << CPU1->pid + 1 << ",1 " << time.timer;
ready_queue_fifo.pop(); ready_queue_fifo.pop();
} }
if (CPU2 == NULL && !ready_queue_fifo.empty()) { if (CPU2 == NULL && !ready_queue_fifo.empty()) {
// Assign the first process from the ready queue to the CPU
CPU2 = ready_queue_fifo.front(); CPU2 = ready_queue_fifo.front();
CPU2->in_cpu2 = 1; CPU2->in_cpu2 = 1;
// Record in_time when the process enters the CPU out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << endl;
out_string2 = "P" + to_string(CPU2->pid+1) + ",2 " + to_string(time.timer);
// output_file << "P" << CPU2->pid + 1 << ",2 " << time.timer;
ready_queue_fifo.pop(); ready_queue_fifo.pop();
} }
@ -102,18 +97,15 @@ void fifo() {
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu1 == 1) { if(processes[i].in_cpu1 == 1) {
if(CPU1->burst_times[processes[i].current_burst_index] == 0){ if(CPU1->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU
out_string1 += " " + to_string(time.timer); out_string1 += " " + to_string(time.timer);
output_file << out_string1 << endl; out_cpu1.push_back(out_string1);
// output_file << " " << time.timer << endl;
CPU1->in_cpu1 = 0; CPU1->in_cpu1 = 0;
CPU1->current_burst_index++; CPU1->current_burst_index++;
waiting.push_back(CPU1); // process added to waiting queue waiting.push_back(CPU1); // process added to waiting queue
if(!ready_queue_fifo.empty()) { if(!ready_queue_fifo.empty()) {
CPU1 = ready_queue_fifo.front(); // process added to CPU CPU1 = ready_queue_fifo.front(); // process added to CPU
CPU1->in_cpu1 = 1; CPU1->in_cpu1 = 1;
// output_file << "P" << CPU1->pid+1 << ",1" << " " << time.timer; // New entry time out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
out_string1 = "P" + to_string(CPU1->pid+1) + ",1 " + to_string(time.timer);
ready_queue_fifo.pop(); ready_queue_fifo.pop();
} }
else { else {
@ -129,18 +121,15 @@ void fifo() {
for(int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu2 == 1) { if(processes[i].in_cpu2 == 1) {
if(CPU2->burst_times[processes[i].current_burst_index] == 0){ if(CPU2->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU
out_string2 += " " + to_string(time.timer); out_string2 += " " + to_string(time.timer);
output_file << out_string2 << endl; out_cpu2.push_back(out_string2);
// output_file << " " << time.timer << endl;
CPU2->in_cpu2 = 0; CPU2->in_cpu2 = 0;
CPU2->current_burst_index++; CPU2->current_burst_index++;
waiting.push_back(CPU2); // process added to waiting queue waiting.push_back(CPU2); // process added to waiting queue
if(!ready_queue_fifo.empty()) { if(!ready_queue_fifo.empty()) {
CPU2 = ready_queue_fifo.front(); // process added to CPU CPU2 = ready_queue_fifo.front(); // process added to CPU
CPU2->in_cpu2 = 1; CPU2->in_cpu2 = 1;
out_string2 = "P" + to_string(CPU2->pid+1) + ",2 " + to_string(time.timer); out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU2->pid+1 << ",2" << " " << time.timer; // New entry time
ready_queue_fifo.pop(); ready_queue_fifo.pop();
} }
else { else {
@ -169,7 +158,7 @@ void fifo() {
// Increment the timer // Increment the timer
time.timer++; time.timer++;
} }
output_file.close(); // output_file.close();
return; return;
} }
@ -232,8 +221,7 @@ void sjf() {
CPU1 = ready_queue.top(); CPU1 = ready_queue.top();
CPU1->in_cpu1 = 1; CPU1->in_cpu1 = 1;
// Record in_time when the process enters the CPU // Record in_time when the process enters the CPU
out_string1 = "P" + to_string(CPU1->pid+1) + ",1 " + to_string(time.timer); out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU1->pid + 1 << ",1 " << time.timer;
ready_queue.pop(); ready_queue.pop();
} }
@ -243,7 +231,7 @@ void sjf() {
CPU2->in_cpu2 = 1; CPU2->in_cpu2 = 1;
// Record in_time when the process enters the CPU // Record in_time when the process enters the CPU
// output_file << endl; // output_file << endl;
out_string2 = "P" + to_string(CPU2->pid+1) + ",2 " + to_string(time.timer); out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU2->pid + 1 << ",2 " << time.timer; // output_file << "P" << CPU2->pid + 1 << ",2 " << time.timer;
ready_queue.pop(); ready_queue.pop();
} }
@ -256,16 +244,15 @@ void sjf() {
if(CPU1->burst_times[processes[i].current_burst_index] == 0){ if(CPU1->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
out_string1 += " " + to_string(time.timer); out_string1 += " " + to_string(time.timer);
output_file << out_string1 << endl; // output_file << out_string1 << endl;
// output_file << " " << time.timer << endl; out_cpu1.push_back(out_string1);
CPU1->in_cpu1 = 0; CPU1->in_cpu1 = 0;
CPU1->current_burst_index++; CPU1->current_burst_index++;
waiting.push_back(CPU1); // process added to waiting queue waiting.push_back(CPU1); // process added to waiting queue
if(!ready_queue.empty()) { if(!ready_queue.empty()) {
CPU1 = ready_queue.top(); // process added to CPU CPU1 = ready_queue.top(); // process added to CPU
CPU1->in_cpu1 = 1; CPU1->in_cpu1 = 1;
// output_file << "P" << CPU1->pid+1 << ",1" << " " << time.timer; // New entry time out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
out_string1 = "P" + to_string(CPU1->pid+1) + ",1 " + to_string(time.timer);
ready_queue.pop(); ready_queue.pop();
} }
else { else {
@ -283,16 +270,15 @@ void sjf() {
if(CPU2->burst_times[processes[i].current_burst_index] == 0){ if(CPU2->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
out_string2 += " " + to_string(time.timer); out_string2 += " " + to_string(time.timer);
output_file << out_string2 << endl; // output_file << out_string2 << endl;
// output_file << " " << time.timer << endl; out_cpu2.push_back(out_string2);
CPU2->in_cpu2 = 0; CPU2->in_cpu2 = 0;
CPU2->current_burst_index++; CPU2->current_burst_index++;
waiting.push_back(CPU2); // process added to waiting queue waiting.push_back(CPU2); // process added to waiting queue
if(!ready_queue.empty()) { if(!ready_queue.empty()) {
CPU2 = ready_queue.top(); // process added to CPU CPU2 = ready_queue.top(); // process added to CPU
CPU2->in_cpu2 = 1; CPU2->in_cpu2 = 1;
out_string2 = "P" + to_string(CPU2->pid+1) + ",2 " + to_string(time.timer); out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU2->pid+1 << ",2" << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
else { else {
@ -321,7 +307,195 @@ void sjf() {
// Increment the timer // Increment the timer
time.timer++; time.timer++;
} }
output_file.close(); // output_file.close();
return;
}
// --------------------------- The Pre-emptive Shortest Job First ---------------------------------
void pre_sjf() {
// Clock initialized to 0
struct clock time;
memset(&time, 0, sizeof(struct clock));
time.timer = 0;
time.push_signal = 5;
int process_count = processes.size();
int completed_processes = 0;
string out_string1 = "";
string out_string2 = "";
while(completed_processes < process_count) {
// Breaking from the infinite loop
for (int i = 0; i < process_count; ++i) {
if (processes[i].burst_times[processes[i].current_burst_index] == -2) {
completed_processes++;
}
}
// Managing arrival times
for (int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu1 != 1 || processes[i].in_cpu2 != 1) {
if(time.timer == processes[i].burst_times[0]) {
ready_queue.push(&processes[i]);
if(CPU1 != NULL) {
ready_queue.push(CPU1);
CPU1->in_cpu1 = 0;
out_string1 += " " + to_string(time.timer);
// output_file << out_string1 << endl;
out_cpu1.push_back(out_string1);
// output_file << " " << time.timer << endl;
CPU1 = ready_queue.top();
CPU1->in_cpu1 = 1;
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
ready_queue.pop();
}
if(CPU2 != NULL) {
ready_queue.push(CPU2);
CPU2->in_cpu2 = 0;
out_string2 += " " + to_string(time.timer);
// output_file << out_string2 << endl;
out_cpu2.push_back(out_string2);
CPU2 = ready_queue.top();
CPU2->in_cpu2 = 1;
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
ready_queue.pop();
}
processes[i].current_burst_index++;
}
}
}
// Managing waiting queue
for (int j = 0; j < waiting.size(); ++j) {
if (waiting[j] != NULL) {
if (waiting[j]->burst_times[waiting[j]->current_burst_index] == 0) {
ready_queue.push(waiting[j]);
if(CPU1 != NULL) {
ready_queue.push(CPU1);
CPU1->in_cpu1 = 0;
out_string1 += " " + to_string(time.timer);
// output_file << out_string1 << endl;
out_cpu1.push_back(out_string1);
CPU1 = ready_queue.top();
CPU1->in_cpu1 = 1;
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
ready_queue.pop();
}
if(CPU2 != NULL) {
ready_queue.push(CPU2);
CPU2->in_cpu2 = 0;
out_string2 += " " + to_string(time.timer);
// output_file << out_string2 << endl;
out_cpu2.push_back(out_string2);
CPU2 = ready_queue.top();
CPU2->in_cpu2 = 1;
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU2->pid+1 << ",2" << " " << time.timer; // New entry time
ready_queue.pop();
}
waiting[j]->current_burst_index++;
waiting[j] = NULL;
}
}
}
if (CPU1 == NULL && !ready_queue.empty()) {
// Assign the first process from the ready queue to the CPU
CPU1 = ready_queue.top();
CPU1->in_cpu1 = 1;
// Record in_time when the process enters the CPU
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU1->pid + 1 << ",1 " << time.timer;
ready_queue.pop();
}
if (CPU2 == NULL && !ready_queue.empty()) {
// Assign the first process from the ready queue to the CPU
CPU2 = ready_queue.top();
CPU2->in_cpu2 = 1;
// Record in_time when the process enters the CPU
// output_file << endl;
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
// output_file << "P" << CPU2->pid + 1 << ",2 " << time.timer;
ready_queue.pop();
}
// Check CPU1
if(CPU1 != NULL) {
//check cpu_burst complete
for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu1 == 1) {
if(CPU1->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU
out_string1 += " " + to_string(time.timer);
// output_file << out_string1 << endl;
out_cpu1.push_back(out_string1);
CPU1->in_cpu1 = 0;
CPU1->current_burst_index++;
waiting.push_back(CPU1); // process added to waiting queue
if(!ready_queue.empty()) {
CPU1 = ready_queue.top(); // process added to CPU
CPU1->in_cpu1 = 1;
out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->current_burst_index + 1) / 2) + " " + to_string(time.timer);
ready_queue.pop();
}
else {
CPU1 = NULL;
}
}
}
}
}
if(CPU2 != NULL) {
//check cpu_burst complete
for(int i = 0; i < process_count; ++i) {
if(processes[i].in_cpu2 == 1) {
if(CPU2->burst_times[processes[i].current_burst_index] == 0){
// Record out_time when the process exits the CPU
out_string2 += " " + to_string(time.timer);
// output_file << out_string2 << endl;
out_cpu2.push_back(out_string2);
CPU2->in_cpu2 = 0;
CPU2->current_burst_index++;
waiting.push_back(CPU2); // process added to waiting queue
if(!ready_queue.empty()) {
CPU2 = ready_queue.top(); // process added to CPU
CPU2->in_cpu2 = 1;
out_string2 = "P" + to_string(CPU2->pid+1) + "," + to_string((CPU2->current_burst_index + 1) / 2) + " " + to_string(time.timer);
ready_queue.pop();
}
else {
CPU2 = NULL;
}
}
}
}
}
if(CPU1 != NULL) {
CPU1->burst_times[CPU1->current_burst_index]--;
}
if(CPU2 != NULL) {
CPU2->burst_times[CPU2->current_burst_index]--;
}
for(int j = 0; j < waiting.size(); ++j) {
if(waiting[j] != NULL) {
if(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
}
}
}
// Increment the timer
time.timer++;
}
// output_file.close();
return; return;
} }
@ -339,7 +513,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("temp.dat", ios::binary); // ifstream file("process1.dat", ios::binary);
string buffer; string buffer;
int pid = 0; int pid = 0;
@ -367,7 +541,7 @@ int main(int argc, char **argv) {
temp["rr"] = 4; temp["rr"] = 4;
string temp1 = scheduler_algorithm; string temp1 = scheduler_algorithm;
// string temp1 = "fifo"; // string temp1 = "pre_sjf";
switch(temp[temp1]){ switch(temp[temp1]){
case 1: case 1:
@ -376,15 +550,23 @@ int main(int argc, char **argv) {
case 2: case 2:
sjf(); sjf();
break; break;
// case 3: case 3:
// pre_sjf(); pre_sjf();
// break; break;
// case 4: // case 4:
// round_robin(); // round_robin();
// break; // break;
default: default:
cout << "enter fifo or sjf or pre_sjf or rr" << endl; cout << "enter fifo or sjf or pre_sjf or rr" << endl;
} }
output_file << "CPU1" << endl;
for(int i = 0; i < out_cpu1.size(); ++i) {
output_file << out_cpu1[i] << endl;
}
output_file << "CPU2" << endl;
for(int i = 0; i < out_cpu2.size(); ++i) {
output_file << out_cpu2[i] << endl;
}
return 0; return 0;
} }

Binary file not shown.

View File

@ -79,12 +79,12 @@ 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 << ",1" << " " << time.timer; output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer;
ready_queue_fifo.pop(); ready_queue_fifo.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) {
@ -97,7 +97,7 @@ void fifo() {
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 << ",1" << " " << time.timer; // New entry time 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 {
@ -187,7 +187,7 @@ 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 << ",1" << " " << time.timer; output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
ready_queue.pop(); ready_queue.pop();
} }
@ -204,7 +204,7 @@ void sjf() {
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 << ",1" << " " << time.timer; // New entry time 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 {
@ -271,7 +271,7 @@ void pre_sjf() {
output_file << " " << time.timer << endl; 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 << ",1" << " " << time.timer; // New entry time output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
@ -291,7 +291,7 @@ void pre_sjf() {
output_file << " " << time.timer << endl; 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 << ",1" << " " << time.timer; // New entry time output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue.pop(); ready_queue.pop();
} }
@ -305,7 +305,7 @@ 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 << ",1" << " " << time.timer; output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer;
ready_queue.pop(); ready_queue.pop();
} }
@ -322,7 +322,7 @@ void pre_sjf() {
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 << ",1" << " " << time.timer; // New entry time 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 {
@ -356,46 +356,46 @@ void pre_sjf() {
// ------------------------------------------- Round Robin -------------------------------------------------- // ------------------------------------------- Round Robin --------------------------------------------------
// vector<process_detail*> waiting;
void round_robin() { void round_robin() {
//clock initialized to 0
// 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 = 5; 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 = 2; // Define a time quantum for Round Robin int time_quantum = 5;
// To keep track of the remaining quantum for the current process
int current_quantum = 0; int current_quantum = 0;
while (completed_processes < process_count) {
// Ready queue, waiting queue, CPU in check, ready queue subtraction, waiting queue subtraction
// Breaking from the loop while(completed_processes < process_count){
for (int i = 0; i < process_count; ++i) {
if (processes[i].burst_times[processes[i].current_burst_index] == -2) { // ready queue, waiting queue, cpu in check, ready queue subtraction, waiting queue subtraction
// breaking from the infinite loop
for(int i = 0; i < process_count; ++i) {
if(processes[i].burst_times[processes[i].current_burst_index] == -2) {
completed_processes++; completed_processes++;
} }
} }
// Managing arrival times //managing arrival times
for (int i = 0; i < process_count; ++i) { for(int i = 0; i < process_count; ++i) {
// 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]) {
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 // managing waiting queue
for (int j = 0; j < waiting.size(); ++j) { for(int j = 0; j < waiting.size(); ++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_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;
@ -403,71 +403,65 @@ void round_robin() {
} }
} }
if (CPU == NULL && !ready_queue_fifo.empty()) { if(CPU == NULL && !ready_queue_fifo.empty()) {
// Assign the first process from the ready queue to the CPU
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 << ",1 " << 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; // Reset the time quantum for the new process current_quantum = time_quantum;
} }
else if (CPU != NULL) {
// Check if CPU burst is complete or quantum expired
if (CPU->burst_times[CPU->current_burst_index] == 0 || current_quantum == 0) {
// Record out_time when the process exits the CPU
output_file << " " << time.timer << endl;
CPU->in_cpu = 0;
CPU->current_burst_index++;
// If the process still has bursts left, move it to the waiting queue else if(CPU != NULL){
if (CPU->burst_times[CPU->current_burst_index] > 0) { //check cpu_burst complete
waiting.push_back(CPU); for(int i = 0; i < process_count; ++i) {
} if(processes[i].in_cpu == 1) {
if(CPU->burst_times[processes[i].current_burst_index] == 0 || current_quantum == 0){
// Assign the next process from the ready queue to the CPU // Record out_time when the process exits the CPU
if (!ready_queue_fifo.empty()) { output_file << " " << time.timer << endl;
CPU = ready_queue_fifo.front(); CPU->in_cpu = 0;
CPU->in_cpu = 1; if(CPU->burst_times[processes[i].current_burst_index] == 0) CPU->current_burst_index++;
output_file << "P" << CPU->pid + 1 << ",1 " << time.timer; // New entry time if(current_quantum == 0) ready_queue_fifo.push(CPU);
ready_queue_fifo.pop(); waiting.push_back(CPU); // process added to waiting queue
current_quantum = time_quantum; // Reset the time quantum for the new process if(!ready_queue_fifo.empty()) {
} CPU = ready_queue_fifo.front(); // process added to CPU
else { CPU->in_cpu = 1;
CPU = NULL; output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time
ready_queue_fifo.pop();
current_quantum = time_quantum;
}
else {
CPU = NULL;
}
}
} }
} }
} }
if (CPU != NULL) {
// Decrement the burst time of the process currently in the CPU if(CPU != NULL) {
CPU->burst_times[CPU->current_burst_index]--; CPU->burst_times[CPU->current_burst_index]--;
current_quantum--; // Decrement the quantum counter current_quantum--;
// If quantum is exhausted but burst isn't finished, preempt the process
if (current_quantum == 0 && CPU->burst_times[CPU->current_burst_index] > 0) {
ready_queue_fifo.push(CPU); // Re-add the process to the ready queue
CPU->in_cpu = 0;
CPU = NULL; // Preempt the process from the CPU
}
} }
// Reduce the IO burst times of the processes in the waiting queue for(int j = 0; j < waiting.size(); ++j) {
for (int j = 0; j < waiting.size(); ++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) { 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]--; // Reducing the IO burst until it reaches 0
} }
} }
} }
time.timer++; time.timer++;
} }
output_file.close(); output_file.close();
return; return;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
if(argc != 3) if(argc != 3)
@ -482,6 +476,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);
string buffer; string buffer;
int pid = 0; int pid = 0;
@ -509,6 +504,7 @@ int main(int argc, char **argv) {
temp["rr"] = 4; temp["rr"] = 4;
string temp1 = scheduler_algorithm; string temp1 = scheduler_algorithm;
// string temp1 = "rr";
switch(temp[temp1]){ switch(temp[temp1]){
case 1: case 1: