diff --git a/lab3/ans.cpp b/lab3/ans.cpp deleted file mode 100644 index f852ca8..0000000 --- a/lab3/ans.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace std; - -struct Process { - int pid; - int arrival_time; - vector burst_times; - int current_burst_index; - int completion_time; - int waiting_time; - int turnaround_time; - bool in_cpu; -}; - -vector processes; - -void fifo() { - queue ready_queue; - int current_time = 0; - int completed_processes = 0; - int process_count = processes.size(); - - while (completed_processes < process_count) { - // Add processes to the ready queue based on arrival time - for (auto& process : processes) { - if (process.arrival_time <= current_time && !process.in_cpu) { - ready_queue.push(&process); - process.in_cpu = true; - } - } - - if (!ready_queue.empty()) { - Process* current_process = ready_queue.front(); - ready_queue.pop(); - - // Simulate CPU execution - for (int i = current_process->current_burst_index; i < current_process->burst_times.size(); i += 2) { - int cpu_burst = current_process->burst_times[i]; - current_time += cpu_burst; // Advance time by CPU burst duration - current_process->current_burst_index++; - - // Handle I/O burst if there's one - if (i + 1 < current_process->burst_times.size()) { - int io_burst = current_process->burst_times[i + 1]; - current_time += io_burst; // Advance time by I/O burst duration - } - } - - current_process->completion_time = current_time; - current_process->turnaround_time = current_process->completion_time - current_process->arrival_time; - current_process->waiting_time = current_process->turnaround_time - (current_process->burst_times.size() / 2); - completed_processes++; - } else { - // No process is ready; advance time - current_time++; - } - } - - // Calculate averages - int total_waiting_time = 0, total_turnaround_time = 0; - for (const auto& process : processes) { - total_waiting_time += process.waiting_time; - total_turnaround_time += process.turnaround_time; - } - - double avg_waiting_time = static_cast(total_waiting_time) / process_count; - double avg_turnaround_time = static_cast(total_turnaround_time) / process_count; - - // Output results - cout << "FIFO Scheduling Results:\n"; - cout << "Processes:\n"; - for (const auto& process : processes) { - cout << "Process ID: " << process.pid - << ", Completion Time: " << process.completion_time - << ", Waiting Time: " << process.waiting_time - << ", Turnaround Time: " << process.turnaround_time << endl; - } - - cout << "Average Waiting Time: " << fixed << setprecision(2) << avg_waiting_time << endl; - cout << "Average Turnaround Time: " << fixed << setprecision(2) << avg_turnaround_time << endl; -} - -int main(int argc, char** argv) { - if (argc != 3) { - cout << "Usage: ./scheduler.out \n"; - return -1; - } - - ifstream file(argv[1]); - string line; - int pid = 0; - - while (getline(file, line)) { - if (line.empty()) continue; - Process process; - process.pid = pid++; - process.current_burst_index = 0; - process.in_cpu = false; - - istringstream iss(line); - iss >> process.arrival_time; - - int burst_time; - while (iss >> burst_time && burst_time != -1) { - process.burst_times.push_back(burst_time); - } - processes.push_back(process); - } - - string algorithm = argv[2]; - if (algorithm == "fifo") { - fifo(); - } else { - cout << "Invalid scheduling algorithm. Please use 'fifo'.\n"; - } - - return 0; -} diff --git a/lab3/cpu_times.txt b/lab3/cpu_times.txt index e54e05b..b5eeb42 100644 --- a/lab3/cpu_times.txt +++ b/lab3/cpu_times.txt @@ -1,9 +1,287 @@ P1,1 0 5 -P2,1 5 7 -P3,1 7 8 -P1,1 8 13 -P2,2 13 14 -P3,2 14 15 -P1,1 15 20 -P1,1 20 25 -P1,2 27 28 +P2,1 5 10 +P3,1 10 15 +P4,1 15 20 +P5,1 20 23 +P1,1 23 28 +P6,1 28 33 +P7,1 33 38 +P2,1 38 43 +P3,1 43 48 +P4,1 48 53 +P5,2 53 56 +P1,1 56 61 +P7,1 61 66 +P2,1 66 71 +P3,1 71 76 +P4,2 76 81 +P5,3 81 84 +P1,1 84 89 +P7,1 89 94 +P2,1 94 99 +P3,1 99 104 +P4,2 104 109 +P1,1 109 114 +P7,1 114 119 +P2,1 119 124 +P3,1 124 129 +P4,2 129 134 +P1,1 134 139 +P7,1 139 144 +P2,1 144 149 +P3,1 149 154 +P4,2 154 159 +P1,1 159 164 +P7,1 164 169 +P2,1 169 174 +P3,1 174 179 +P4,2 179 184 +P1,1 184 189 +P7,1 189 194 +P2,1 194 199 +P3,1 199 204 +P4,2 204 209 +P1,1 209 214 +P7,1 214 219 +P2,1 219 224 +P3,1 224 229 +P4,2 229 234 +P1,1 234 239 +P7,1 239 244 +P2,1 244 249 +P3,1 249 254 +P4,2 254 259 +P1,1 259 264 +P7,1 264 269 +P2,1 269 274 +P3,1 274 279 +P4,2 279 284 +P1,1 284 289 +P7,1 289 294 +P2,1 294 299 +P3,1 299 304 +P4,2 304 309 +P1,1 309 314 +P7,1 314 319 +P2,1 319 324 +P3,1 324 329 +P4,2 329 334 +P1,1 334 339 +P7,1 339 344 +P2,1 344 349 +P3,1 349 354 +P4,2 354 359 +P1,1 359 364 +P7,1 364 369 +P2,1 369 374 +P3,2 374 379 +P4,3 379 384 +P1,1 384 389 +P7,1 389 394 +P2,1 394 399 +P3,2 399 404 +P4,3 404 409 +P1,1 409 414 +P7,1 414 419 +P2,2 419 424 +P3,2 424 429 +P4,3 429 434 +P1,1 434 439 +P7,1 439 444 +P2,2 444 449 +P3,2 449 454 +P4,3 454 459 +P1,1 459 464 +P7,1 464 469 +P2,2 469 474 +P3,2 474 479 +P4,3 479 484 +P1,1 484 489 +P7,1 489 494 +P2,2 494 499 +P3,2 499 504 +P4,3 504 509 +P1,2 509 514 +P7,1 514 519 +P2,2 519 524 +P3,2 524 529 +P4,4 529 534 +P1,2 534 539 +P7,1 539 544 +P2,2 544 549 +P3,2 549 554 +P4,4 554 559 +P1,2 559 564 +P7,1 564 569 +P2,2 569 574 +P3,2 574 579 +P4,4 579 584 +P1,2 584 589 +P7,1 589 594 +P2,2 594 599 +P3,2 599 604 +P4,4 604 609 +P1,2 609 614 +P7,1 614 619 +P2,2 619 624 +P3,2 624 629 +P4,4 629 634 +P1,2 634 639 +P7,1 639 644 +P2,2 644 649 +P3,2 649 654 +P4,4 654 659 +P1,2 659 664 +P7,1 664 669 +P2,2 669 674 +P3,2 674 679 +P4,4 679 684 +P1,2 684 689 +P7,1 689 694 +P2,2 694 699 +P3,2 699 704 +P4,4 704 709 +P1,2 709 714 +P7,1 714 719 +P2,2 719 724 +P3,3 724 729 +P4,4 729 734 +P1,2 734 739 +P7,1 739 744 +P2,2 744 749 +P3,3 749 754 +P4,4 754 759 +P1,2 759 764 +P7,1 764 769 +P2,2 769 774 +P3,3 774 779 +P4,4 779 784 +P1,2 784 789 +P7,1 789 794 +P2,2 794 799 +P3,3 799 804 +P4,4 804 809 +P1,2 809 814 +P7,1 814 819 +P2,3 819 824 +P3,3 824 829 +P4,4 829 834 +P1,2 834 839 +P7,1 839 844 +P2,3 844 849 +P3,3 849 854 +P4,4 854 859 +P1,2 859 864 +P7,1 864 869 +P2,3 869 874 +P3,3 874 879 +P4,5 879 884 +P1,2 884 889 +P7,1 889 894 +P2,3 894 899 +P3,3 899 904 +P4,5 904 909 +P1,2 909 914 +P7,1 914 919 +P2,3 919 924 +P3,4 924 929 +P4,6 929 934 +P1,2 934 939 +P7,1 939 944 +P2,3 944 949 +P3,4 949 954 +P4,6 954 959 +P1,3 959 964 +P7,1 964 969 +P2,3 969 974 +P3,4 974 979 +P1,3 979 984 +P7,1 984 989 +P2,3 989 994 +P3,4 994 999 +P1,3 999 1004 +P7,1 1004 1009 +P2,3 1009 1014 +P3,4 1014 1019 +P1,3 1019 1024 +P7,2 1024 1027 +P2,3 1027 1032 +P3,4 1032 1037 +P1,3 1037 1042 +P2,4 1042 1047 +P3,4 1047 1052 +P1,3 1052 1057 +P2,4 1057 1062 +P3,4 1062 1067 +P1,3 1067 1072 +P2,4 1072 1077 +P3,4 1077 1082 +P1,3 1082 1087 +P2,4 1087 1092 +P3,4 1092 1097 +P1,3 1097 1102 +P2,4 1102 1107 +P3,4 1107 1112 +P1,3 1112 1117 +P2,4 1117 1122 +P3,4 1122 1127 +P1,3 1127 1132 +P2,4 1132 1137 +P3,4 1137 1142 +P1,3 1142 1147 +P2,4 1147 1152 +P3,4 1152 1157 +P1,3 1157 1162 +P2,4 1162 1167 +P3,5 1167 1172 +P1,3 1172 1177 +P2,4 1177 1182 +P3,5 1182 1187 +P1,3 1187 1192 +P2,4 1192 1197 +P3,5 1197 1202 +P1,3 1202 1207 +P2,4 1207 1212 +P3,5 1212 1217 +P1,4 1217 1222 +P2,4 1222 1227 +P3,6 1227 1232 +P1,4 1232 1237 +P2,4 1237 1242 +P3,6 1242 1247 +P1,4 1247 1252 +P2,5 1252 1257 +P1,4 1257 1262 +P2,5 1262 1267 +P1,4 1267 1272 +P2,5 1272 1277 +P1,4 1277 1282 +P2,5 1282 1287 +P1,4 1287 1292 +P2,5 1292 1297 +P1,4 1297 1302 +P2,5 1302 1307 +P1,4 1307 1312 +P2,5 1312 1317 +P1,4 1317 1322 +P2,5 1322 1327 +P1,4 1327 1332 +P2,6 1332 1337 +P1,4 1337 1342 +P2,6 1342 1347 +P1,4 1347 1352 +P1,4 1352 1357 +P1,5 1359 1364 +P1,5 1364 1369 +P1,5 1369 1374 +P1,5 1374 1379 +P1,5 1379 1384 +P1,5 1384 1389 +P1,5 1389 1394 +P1,5 1394 1399 +P1,5 1399 1404 +P1,5 1404 1409 +P1,5 1409 1414 +P1,5 1414 1419 +P1,6 1421 1426 +P1,6 1426 1431 diff --git a/lab3/multi_core_scheduler b/lab3/multi_core_scheduler deleted file mode 100755 index c3851cf..0000000 Binary files a/lab3/multi_core_scheduler and /dev/null differ diff --git a/lab3/scheduler b/lab3/scheduler deleted file mode 100755 index 726dbfd..0000000 Binary files a/lab3/scheduler and /dev/null differ diff --git a/lab3/src/cpu_times.txt b/lab3/src/cpu_times.txt deleted file mode 100644 index e69de29..0000000 diff --git a/lab3/src/multi_core_scheduler.cpp b/lab3/src/multi_core_scheduler.cpp index eb43406..ef528ef 100644 --- a/lab3/src/multi_core_scheduler.cpp +++ b/lab3/src/multi_core_scheduler.cpp @@ -16,42 +16,45 @@ struct process_detail { int in_cpu1; int in_cpu2; int current_burst_index; + int arrvival_time = 0; + int wait_time = 0; + int cpu_time = 0; + int completion_time = 0; }; struct clock{ - int push_signal; //boolean int timer; - }; vector processes; queue ready_queue_fifo; -vector waiting; process_detail* CPU1 = NULL; process_detail* CPU2 = NULL; vector out_cpu1; vector out_cpu2; - ofstream output_file("cpu_times.txt"); + // ------------------------------------- THE FIFO --------------------------------------- void fifo() { // 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 = ""; + vector waiting(process_count, NULL); 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++; + for (int j = 0; j < process_count; ++j) { + 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++; } } @@ -59,6 +62,7 @@ void fifo() { 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]) { + processes[i].arrvival_time = time.timer; ready_queue_fifo.push(&processes[i]); processes[i].current_burst_index++; } @@ -96,12 +100,13 @@ void fifo() { //check cpu_burst complete for(int i = 0; i < process_count; ++i) { if(processes[i].in_cpu1 == 1) { + processes[i].cpu_time += 1; if(CPU1->burst_times[processes[i].current_burst_index] == 0){ out_string1 += " " + to_string(time.timer); out_cpu1.push_back(out_string1); CPU1->in_cpu1 = 0; CPU1->current_burst_index++; - waiting.push_back(CPU1); // process added to waiting queue + waiting[CPU1->pid] = CPU1; // process added to waiting queue if(!ready_queue_fifo.empty()) { CPU1 = ready_queue_fifo.front(); // process added to CPU CPU1->in_cpu1 = 1; @@ -120,12 +125,13 @@ void fifo() { //check cpu_burst complete for(int i = 0; i < process_count; ++i) { if(processes[i].in_cpu2 == 1) { + processes[i].cpu_time += 1; if(CPU2->burst_times[processes[i].current_burst_index] == 0){ out_string2 += " " + to_string(time.timer); out_cpu2.push_back(out_string2); CPU2->in_cpu2 = 0; CPU2->current_burst_index++; - waiting.push_back(CPU2); // process added to waiting queue + waiting[CPU2->pid] = CPU2; // process added to waiting queue if(!ready_queue_fifo.empty()) { CPU2 = ready_queue_fifo.front(); // process added to CPU CPU2->in_cpu2 = 1; @@ -158,7 +164,6 @@ void fifo() { // Increment the timer time.timer++; } - // output_file.close(); return; } @@ -168,7 +173,16 @@ void fifo() { struct Compare { bool operator()(process_detail* a, process_detail* b) { // 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]; } }; @@ -179,18 +193,20 @@ void sjf() { 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; + vector waiting(process_count, NULL); 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++; + // breaking from the infinite loop + for (int j = 0; j < process_count; ++j) { + 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++; } } @@ -241,6 +257,7 @@ void sjf() { //check cpu_burst complete for(int i = 0; i < process_count; ++i) { if(processes[i].in_cpu1 == 1) { + processes[i].cpu_time += 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); @@ -248,7 +265,7 @@ void sjf() { out_cpu1.push_back(out_string1); CPU1->in_cpu1 = 0; CPU1->current_burst_index++; - waiting.push_back(CPU1); // process added to waiting queue + waiting[CPU1->pid] = CPU1; // process added to waiting queue if(!ready_queue.empty()) { CPU1 = ready_queue.top(); // process added to CPU CPU1->in_cpu1 = 1; @@ -267,6 +284,7 @@ void sjf() { //check cpu_burst complete for(int i = 0; i < process_count; ++i) { if(processes[i].in_cpu2 == 1) { + processes[i].cpu_time += 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); @@ -274,7 +292,7 @@ void sjf() { out_cpu2.push_back(out_string2); CPU2->in_cpu2 = 0; CPU2->current_burst_index++; - waiting.push_back(CPU2); // process added to waiting queue + waiting[CPU2->pid] = CPU2; // process added to waiting queue if(!ready_queue.empty()) { CPU2 = ready_queue.top(); // process added to CPU CPU2->in_cpu2 = 1; @@ -307,7 +325,6 @@ void sjf() { // Increment the timer time.timer++; } - // output_file.close(); return; } // --------------------------- The Pre-emptive Shortest Job First --------------------------------- @@ -318,18 +335,21 @@ void pre_sjf() { 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 = ""; + vector waiting(process_count, NULL); + 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++; + // breaking from the infinite loop + for (int j = 0; j < process_count; ++j) { + 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++; } } @@ -436,7 +456,7 @@ void pre_sjf() { out_cpu1.push_back(out_string1); CPU1->in_cpu1 = 0; CPU1->current_burst_index++; - waiting.push_back(CPU1); // process added to waiting queue + waiting[CPU1->pid] = CPU1; // process added to waiting queue if(!ready_queue.empty()) { CPU1 = ready_queue.top(); // process added to CPU CPU1->in_cpu1 = 1; @@ -462,7 +482,7 @@ void pre_sjf() { out_cpu2.push_back(out_string2); CPU2->in_cpu2 = 0; CPU2->current_burst_index++; - waiting.push_back(CPU2); // process added to waiting queue + waiting[CPU2->pid] = CPU2;// process added to waiting queue if(!ready_queue.empty()) { CPU2 = ready_queue.top(); // process added to CPU CPU2->in_cpu2 = 1; @@ -495,10 +515,167 @@ void pre_sjf() { // Increment the timer time.timer++; } - // output_file.close(); return; } +// ---------------------------------- The Round Robin-------------------------------------------- + +void round_robin() { + struct clock time; + memset(&time, 0, sizeof(struct clock)); + time.timer = 0; + int process_count = processes.size(); + int completed_processes = 0; + int time_quantum = 5; + int current_quantum1 = 0; + int current_quantum2 = 0; + string out_string1 = ""; + string out_string2 = ""; + // Initialize waiting vector with NULLs for each process slot + vector waiting(process_count, NULL); + + + while (completed_processes < process_count) { + // Check for process completion + for (int j = 0; j < process_count; ++j) { + 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++; + } + } + + // 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]) { + processes[i].arrvival_time = time.timer; + ready_queue_fifo.push(&processes[i]); + 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_fifo.push(waiting[j]); + waiting[j]->current_burst_index++; + waiting[j] = NULL; + } + } + } + + // Assign a process to CPU1 if available + if (CPU1 == NULL && !ready_queue_fifo.empty()) { + CPU1 = ready_queue_fifo.front(); + CPU1->in_cpu1 = 1; + out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->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(); + current_quantum1 = time_quantum; + } + + // Assign a process to CPU2 if available + if (CPU2 == NULL && !ready_queue_fifo.empty()) { + CPU2 = ready_queue_fifo.front(); + 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" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; + ready_queue_fifo.pop(); + current_quantum2 = time_quantum; + } + + if (CPU1 != NULL) { + for(int i = 0; i < process_count; ++i) { + if(processes[i].in_cpu1 == 1){ + processes[i].cpu_time += 1; + if (CPU1->burst_times[CPU1->current_burst_index] == 0 || current_quantum1 == 0) { + // output_file << " " << time.timer << endl; + out_string1 += " " + to_string(time.timer); + out_cpu1.push_back(out_string1); + CPU1->in_cpu1 = 0; + if (CPU1->burst_times[CPU1->current_burst_index] == 0){ + CPU1->current_burst_index++; + waiting[CPU1->pid] = CPU1; + } + + else if (current_quantum1 == 0) ready_queue_fifo.push(CPU1); + + // Place the process in its corresponding waiting slot by pid + + if (!ready_queue_fifo.empty()) { + CPU1 = ready_queue_fifo.front(); + CPU1->in_cpu1 = 1; + out_string1 = "P" + to_string(CPU1->pid+1) + "," + to_string((CPU1->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(); + current_quantum1 = time_quantum; + } else { + CPU1 = NULL; + } + } + } + } + } + if (CPU2 != NULL) { + for(int i = 0; i < process_count; ++i) { + if(processes[i].in_cpu2 == 1){ + processes[i].cpu_time += 1; + if (CPU2->burst_times[CPU2->current_burst_index] == 0 || current_quantum2 == 0) { + // output_file << " " << time.timer << endl; + out_string2 += " " + to_string(time.timer); + out_cpu2.push_back(out_string2); + CPU2->in_cpu2 = 0; + if (CPU2->burst_times[CPU2->current_burst_index] == 0){ + CPU2->current_burst_index++; + waiting[CPU2->pid] = CPU2; + } + + else if (current_quantum2 == 0) ready_queue_fifo.push(CPU2); + + // Place the process in its corresponding waiting slot by pid + + if (!ready_queue_fifo.empty()) { + CPU2 = ready_queue_fifo.front(); + 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" << CPU->pid + 1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; + ready_queue_fifo.pop(); + current_quantum2 = time_quantum; + } else { + CPU2 = NULL; + } + } + } + } + } + + if(CPU1 != NULL) { + CPU1->burst_times[CPU1->current_burst_index]--; + current_quantum1--; + } + + if(CPU2 != NULL) { + CPU2->burst_times[CPU2->current_burst_index]--; + current_quantum2--; + } + + // Manage IO bursts in waiting queue + for (int j = 0; j < process_count; ++j) { + if (waiting[j] != NULL && waiting[j]->burst_times[waiting[j]->current_burst_index] != 0) { + waiting[j]->burst_times[waiting[j]->current_burst_index]--; + } + } + + // Increment the timer + time.timer++; + } + +} + int main(int argc, char **argv) { if(argc != 3) @@ -543,6 +720,9 @@ int main(int argc, char **argv) { string temp1 = scheduler_algorithm; // string temp1 = "pre_sjf"; + // Start time point + auto start = std::chrono::high_resolution_clock::now(); + switch(temp[temp1]){ case 1: fifo(); @@ -553,12 +733,15 @@ int main(int argc, char **argv) { case 3: pre_sjf(); break; - // case 4: - // round_robin(); - // break; + case 4: + round_robin(); + break; default: cout << "enter fifo or sjf or pre_sjf or rr" << endl; } + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(end - start); + output_file << "CPU1" << endl; for(int i = 0; i < out_cpu1.size(); ++i) { output_file << out_cpu1[i] << endl; @@ -568,5 +751,21 @@ int main(int argc, char **argv) { for(int i = 0; i < out_cpu2.size(); ++i) { output_file << out_cpu2[i] << endl; } + float tot = 0; + int count = processes.size(); + for(int i = 0; i < processes.size(); ++i) { + tot += processes[i].completion_time; + cout << "Process " << i+1 << " Completion Time: " << processes[i].completion_time << endl; + } + cout << "Average Completion Time: " << tot/count << endl; + tot = 0; + for(int i = 0; i < processes.size(); ++i) { + tot += processes[i].completion_time - processes[i].cpu_time; + 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; + } + cout << "Average Waiting Time: " << tot/count << endl; + + std::cout << "Execution time: " << duration.count() << " ms" << std::endl; return 0; } \ No newline at end of file diff --git a/lab3/src/scheduler.cpp b/lab3/src/scheduler.cpp index 330dd72..3645ece 100644 --- a/lab3/src/scheduler.cpp +++ b/lab3/src/scheduler.cpp @@ -22,15 +22,12 @@ struct process_detail { }; struct clock{ - int push_signal; //boolean int timer; - }; vector processes; queue ready_queue_fifo; struct process_detail* CPU = NULL; - ofstream output_file("cpu_times.txt"); vector out_strings; @@ -43,7 +40,6 @@ void fifo() { struct clock time; memset(&time, 0, sizeof(struct clock)); time.timer = 0; - time.push_signal = 0; int process_count = processes.size(); int completed_processes = 0; vector waiting(process_count, NULL); @@ -169,7 +165,6 @@ void sjf() { struct clock time; memset(&time, 0, sizeof(struct clock)); time.timer = 0; - time.push_signal = 0; int process_count = processes.size(); int completed_processes = 0; // Initialize waiting vector with NULLs for each process slot @@ -276,7 +271,6 @@ void pre_sjf() { struct clock time; memset(&time, 0, sizeof(struct clock)); time.timer = 0; - time.push_signal = 0; int process_count = processes.size(); int completed_processes = 0; // Initialize waiting vector with NULLs for each process slot @@ -404,12 +398,11 @@ void pre_sjf() { // ------------------------------------------- Round Robin -------------------------------------------------- -// vector waiting; + void round_robin() { struct clock time; memset(&time, 0, sizeof(struct clock)); time.timer = 0; - time.push_signal = 0; int process_count = processes.size(); int completed_processes = 0; int time_quantum = 5;