diff --git a/.gitignore b/.gitignore index e0d393b..41b8790 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.out -*.pl +*.pl \ No newline at end of file diff --git a/lab1/images/3rotated.ppm b/lab1/images/3rotated.ppm new file mode 100644 index 0000000..9f2920b Binary files /dev/null and b/lab1/images/3rotated.ppm differ diff --git a/lab1/src/detail_times.txt b/lab1/src/detail_times.txt index 398a219..c8a5123 100644 --- a/lab1/src/detail_times.txt +++ b/lab1/src/detail_times.txt @@ -35,3 +35,4 @@ ../images/7.ppm 1.79435 ../images/1.ppm 0.00378666 ../images/2.ppm 0.0142673 +../images/3.ppm 0.0612914 diff --git a/lab1/src/read_times.txt b/lab1/src/read_times.txt index 44c8ca3..90f0eb6 100644 --- a/lab1/src/read_times.txt +++ b/lab1/src/read_times.txt @@ -35,3 +35,4 @@ ../images/7.ppm 1.80572 ../images/1.ppm 0.0197896 ../images/2.ppm 0.0329337 +../images/3.ppm 0.199503 diff --git a/lab1/src/sharp_times.txt b/lab1/src/sharp_times.txt index 3827297..508db81 100644 --- a/lab1/src/sharp_times.txt +++ b/lab1/src/sharp_times.txt @@ -35,3 +35,4 @@ ../images/7.ppm 2.54863 ../images/1.ppm 0.00357817 ../images/2.ppm 0.0139955 +../images/3.ppm 0.0578726 diff --git a/lab1/src/smooth_times.txt b/lab1/src/smooth_times.txt index 88f9625..1125f62 100644 --- a/lab1/src/smooth_times.txt +++ b/lab1/src/smooth_times.txt @@ -35,3 +35,4 @@ ../images/7.ppm 2.50061 ../images/1.ppm 0.00590421 ../images/2.ppm 0.0222273 +../images/3.ppm 0.0992293 diff --git a/lab1/src/write_times.txt b/lab1/src/write_times.txt index 8cfcfd1..9ae6c8f 100644 --- a/lab1/src/write_times.txt +++ b/lab1/src/write_times.txt @@ -35,3 +35,4 @@ ../images/7.ppm 1.0496 ../images/1.ppm 0.00731258 ../images/2.ppm 0.02392 +../images/3.ppm 0.152686 diff --git a/lab3/.gitignore b/lab3/.gitignore new file mode 100644 index 0000000..8be5542 --- /dev/null +++ b/lab3/.gitignore @@ -0,0 +1,3 @@ +/bin + +*.out \ No newline at end of file diff --git a/lab3/Makefile b/lab3/Makefile index a43f083..744f955 100644 --- a/lab3/Makefile +++ b/lab3/Makefile @@ -1,8 +1,18 @@ -scheduler.out: scheduler.cpp - g++ scheduler.cpp -o scheduler.out +build-sched: src/scheduler.cpp + mkdir -p bin + g++ src/scheduler.cpp -o bin/scheduler.out -run_sched: scheduler.out - ./scheduler.out $(WF) $(SA) +run-sched: build-sched + bin/scheduler.out WorkloadFiles/$(WF) $(SA) + +build-sched-multi: src/multi_core_scheduler.cpp + mkdir -p bin + g++ src/multi_core_scheduler.cpp -o bin/multi_core_scheduler.out + +run-sched-multi: build-sched-multi + bin/multi_core_scheduler.out WorkloadFiles/$(WF) $(SA) clean: - rm *.out \ No newline at end of file + rm -r bin + +.PHONY: build-sched build-sched-multi \ No newline at end of file diff --git a/lab3/process1.dat b/lab3/WorkloadFiles/process1.dat similarity index 100% rename from lab3/process1.dat rename to lab3/WorkloadFiles/process1.dat diff --git a/lab3/process2.dat b/lab3/WorkloadFiles/process2.dat similarity index 100% rename from lab3/process2.dat rename to lab3/WorkloadFiles/process2.dat diff --git a/lab3/process3.dat b/lab3/WorkloadFiles/process3.dat similarity index 100% rename from lab3/process3.dat rename to lab3/WorkloadFiles/process3.dat diff --git a/lab3/temp.dat b/lab3/WorkloadFiles/temp.dat similarity index 100% rename from lab3/temp.dat rename to lab3/WorkloadFiles/temp.dat diff --git a/lab3/cpu_times.txt b/lab3/cpu_times.txt index e1dcc65..77ce860 100644 --- a/lab3/cpu_times.txt +++ b/lab3/cpu_times.txt @@ -1,32 +1,58 @@ -CPU1 -P1,1 0 100 -P5,2 100 103 -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 +P1,1 0 2 +P2,0 2 3 +P3,0 3 4 +P4,0 4 5 +P5,0 5 6 +P5,1 6 8 +P6,1 8 10 +P6,1 10 10 +P5,1 10 13 +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 diff --git a/lab3/3_process_scheduling.pdf b/lab3/doc/3_process_scheduling.pdf similarity index 100% rename from lab3/3_process_scheduling.pdf rename to lab3/doc/3_process_scheduling.pdf diff --git a/lab3/schedule_format.txt b/lab3/doc/schedule_format.txt similarity index 100% rename from lab3/schedule_format.txt rename to lab3/doc/schedule_format.txt diff --git a/lab3/multi_core_scheduler.cpp b/lab3/src/multi_core_scheduler.cpp similarity index 100% rename from lab3/multi_core_scheduler.cpp rename to lab3/src/multi_core_scheduler.cpp diff --git a/lab3/scheduler.cpp b/lab3/src/scheduler.cpp similarity index 96% rename from lab3/scheduler.cpp rename to lab3/src/scheduler.cpp index 4e3f374..a686c9a 100644 --- a/lab3/scheduler.cpp +++ b/lab3/src/scheduler.cpp @@ -1,527 +1,527 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -struct process_detail { - //cpu_burst_times[0] is arrival time - int pid; - vector burst_times; - int in_cpu; - int current_burst_index; -}; - -struct clock{ - int push_signal; //boolean - int timer; - -}; - -vector processes; -queue ready_queue_fifo; -struct process_detail* CPU = NULL; -vector waiting; - -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 = 0; - int process_count = processes.size(); - int completed_processes = 0; - - while(completed_processes < process_count){ - - // 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++; - } - } - - //managing arrival times - for(int i = 0; i < process_count; ++i) { - //if process not in cpu - if(processes[i].in_cpu != 1) { - if(time.timer == processes[i].burst_times[0]) { - 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; - } - } - } - - if(CPU == NULL && !ready_queue_fifo.empty()) { - CPU = ready_queue_fifo.front(); - CPU->in_cpu = 1; - // Record in_time when the process enters the CPU - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer; - ready_queue_fifo.pop(); - - } - - if(CPU != NULL){ - //check cpu_burst complete - for(int i = 0; i < process_count; ++i) { - if(processes[i].in_cpu == 1) { - if(CPU->burst_times[processes[i].current_burst_index] == 0){ - // Record out_time when the process exits the CPU - output_file << " " << time.timer << endl; - CPU->in_cpu = 0; - CPU->current_burst_index++; - waiting.push_back(CPU); // process added to waiting queue - if(!ready_queue_fifo.empty()) { - CPU = ready_queue_fifo.front(); // process added to CPU - CPU->in_cpu = 1; - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time - ready_queue_fifo.pop(); - } - else { - CPU = NULL; - } - } - } - } - } - - - if(CPU != NULL) { - CPU->burst_times[CPU->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 - } - } - } - - time.timer++; - } - output_file.close(); - return; -} - - - -// ----------------------------------------- The Shortest Job Fist ------------------------------------------- -// Custom comparator for the priority queue -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]; - } -}; - -priority_queue, Compare> ready_queue; - -void sjf() { - - //clock initialized to 0 - 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; - - while(completed_processes < process_count){ - - // 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++; - } - } - - //managing arrival times - for(int i = 0; i < process_count; ++i) { - //if process not in cpu - if(processes[i].in_cpu != 1) { - if(time.timer == processes[i].burst_times[0]) { - ready_queue.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.push(waiting[j]); - waiting[j]->current_burst_index++; - waiting[j] = NULL; - } - } - } - - if(CPU == NULL && !ready_queue.empty()) { - CPU = ready_queue.top(); - CPU->in_cpu = 1; - // Record in_time when the process enters the CPU - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; - ready_queue.pop(); - } - - else if(CPU != NULL){ - //check cpu_burst complete - for(int i = 0; i < process_count; ++i) { - if(processes[i].in_cpu == 1) { - if(CPU->burst_times[processes[i].current_burst_index] == 0){ - // Record out_time when the process exits the CPU - output_file << " " << time.timer << endl; - CPU->in_cpu = 0; - CPU->current_burst_index++; - waiting.push_back(CPU); // process added to waiting queue - if(!ready_queue.empty()) { - CPU = ready_queue.top(); // process added to CPU - CPU->in_cpu = 1; - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time - ready_queue.pop(); - } - else { - CPU = NULL; - } - } - } - } - } - - - if(CPU != NULL) { - // reducing the cpu burst till it reaches 0 - CPU->burst_times[CPU->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) { - // reducing the io burst till it reaches 0 - waiting[j]->burst_times[waiting[j]->current_burst_index]--; - } - } - } - - time.timer++; - } - output_file.close(); - return; -} - -// ------------------------------------Pre-emptive shortest job -------------------------------------------- - -void pre_sjf() { - - //clock initialized to 0 - 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; - - while(completed_processes < process_count){ - - // 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++; - } - } - - //managing arrival times - for(int i = 0; i < process_count; ++i) { - //if process not in cpu - if(processes[i].in_cpu != 1) { - if(time.timer == processes[i].burst_times[0]) { - ready_queue.push(&processes[i]); - if(CPU != NULL) { - ready_queue.push(CPU); - CPU->in_cpu = 0; - output_file << " " << time.timer << endl; - CPU = ready_queue.top(); - CPU->in_cpu = 1; - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time - 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(CPU != NULL) { - ready_queue.push(CPU); - CPU->in_cpu = 0; - output_file << " " << time.timer << endl; - CPU = ready_queue.top(); - CPU->in_cpu = 1; - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time - ready_queue.pop(); - - } - waiting[j]->current_burst_index++; - waiting[j] = NULL; - } - } - } - - if(CPU == NULL && !ready_queue.empty()) { - CPU = ready_queue.top(); - CPU->in_cpu = 1; - // Record in_time when the process enters the CPU - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; - ready_queue.pop(); - } - - else if(CPU != NULL){ - //check cpu_burst complete - for(int i = 0; i < process_count; ++i) { - if(processes[i].in_cpu == 1) { - if(CPU->burst_times[processes[i].current_burst_index] == 0){ - // Record out_time when the process exits the CPU - output_file << " " << time.timer << endl; - CPU->in_cpu = 0; - CPU->current_burst_index++; - waiting.push_back(CPU); // process added to waiting queue - if(!ready_queue.empty()) { - CPU = ready_queue.top(); // process added to CPU - CPU->in_cpu = 1; - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time - ready_queue.pop(); - } - else { - CPU = NULL; - } - } - } - } - } - - - if(CPU != NULL) { - // reducing the cpu burst till it reaches 0 - CPU->burst_times[CPU->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) { - // reducing the io burst till it reaches 0 - waiting[j]->burst_times[waiting[j]->current_burst_index]--; - } - } - } - - time.timer++; - } - output_file.close(); - return; -} - - -// ------------------------------------------- Round Robin -------------------------------------------------- -// vector waiting; -void round_robin() { - //clock initialized to 0 - struct clock time; - memset(&time, 0, sizeof(struct clock)); - time.timer = 0; - time.push_signal = 0; - int process_count = processes.size(); - // memset(&waiting, 0, process_count); - int completed_processes = 0; - int time_quantum = 5; - 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 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 process not in cpu - if(processes[i].in_cpu != 1) { - if(time.timer == processes[i].burst_times[0]) { - 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; - } - } - } - - if(CPU == NULL && !ready_queue_fifo.empty()) { - CPU = ready_queue_fifo.front(); - CPU->in_cpu = 1; - // Record in_time when the process enters the CPU - output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; - ready_queue_fifo.pop(); - current_quantum = time_quantum; - - } - - else if(CPU != NULL){ - //check cpu_burst complete - 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){ - // Record out_time when the process exits the CPU - output_file << " " << time.timer << endl; - CPU->in_cpu = 0; - if(CPU->burst_times[processes[i].current_burst_index] == 0) CPU->current_burst_index++; - if(current_quantum == 0) ready_queue_fifo.push(CPU); - waiting.push_back(CPU); // process added to waiting queue - if(!ready_queue_fifo.empty()) { - CPU = ready_queue_fifo.front(); // process added to CPU - CPU->in_cpu = 1; - 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) { - CPU->burst_times[CPU->current_burst_index]--; - current_quantum--; - } - - 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 - } - } - } - - time.timer++; - } - output_file.close(); - return; -} - - - - -int main(int argc, char **argv) { - - if(argc != 3) - { - cout <<"usage: ./scheduler.out \nprovided arguments:\n"; - for(int i = 0; i < argc; i++) - cout << argv[i] << "\n"; - return -1; - } - - char *file_to_search_in = argv[1]; - char *scheduler_algorithm = argv[2]; - - ifstream file(file_to_search_in, ios::binary); - // ifstream file("process1.dat", ios::binary); - string buffer; - int pid = 0; - - while(getline(file, buffer)) { - if(buffer[0] == '<'){ - continue; - } - istringstream iss(buffer); - string word; - struct process_detail pd; - memset(&pd,0,sizeof(struct process_detail)); - pd.pid = pid++; - pd.current_burst_index = 0; - - while(iss>>word){ - pd.burst_times.push_back(stoi(word)); - } - processes.push_back(pd); - } - - map temp; - temp["fifo"] = 1; - temp["sjf"] = 2; - temp["pre_sjf"] = 3; - temp["rr"] = 4; - - string temp1 = scheduler_algorithm; - // string temp1 = "rr"; - - switch(temp[temp1]){ - case 1: - fifo(); - break; - case 2: - sjf(); - break; - case 3: - pre_sjf(); - break; - case 4: - round_robin(); - break; - default: - cout << "enter fifo or sjf or pre_sjf or rr" << endl; - } - - return 0; -} +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct process_detail { + //cpu_burst_times[0] is arrival time + int pid; + vector burst_times; + int in_cpu; + int current_burst_index; +}; + +struct clock{ + int push_signal; //boolean + int timer; + +}; + +vector processes; +queue ready_queue_fifo; +struct process_detail* CPU = NULL; +vector waiting; + +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 = 0; + int process_count = processes.size(); + int completed_processes = 0; + + while(completed_processes < process_count){ + + // 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++; + } + } + + //managing arrival times + for(int i = 0; i < process_count; ++i) { + //if process not in cpu + if(processes[i].in_cpu != 1) { + if(time.timer == processes[i].burst_times[0]) { + 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; + } + } + } + + if(CPU == NULL && !ready_queue_fifo.empty()) { + CPU = ready_queue_fifo.front(); + CPU->in_cpu = 1; + // Record in_time when the process enters the CPU + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1 ) / 2<< " " << time.timer; + ready_queue_fifo.pop(); + + } + + if(CPU != NULL){ + //check cpu_burst complete + for(int i = 0; i < process_count; ++i) { + if(processes[i].in_cpu == 1) { + if(CPU->burst_times[processes[i].current_burst_index] == 0){ + // Record out_time when the process exits the CPU + output_file << " " << time.timer << endl; + CPU->in_cpu = 0; + CPU->current_burst_index++; + waiting.push_back(CPU); // process added to waiting queue + if(!ready_queue_fifo.empty()) { + CPU = ready_queue_fifo.front(); // process added to CPU + CPU->in_cpu = 1; + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time + ready_queue_fifo.pop(); + } + else { + CPU = NULL; + } + } + } + } + } + + + if(CPU != NULL) { + CPU->burst_times[CPU->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 + } + } + } + + time.timer++; + } + output_file.close(); + return; +} + + + +// ----------------------------------------- The Shortest Job Fist ------------------------------------------- +// Custom comparator for the priority queue +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]; + } +}; + +priority_queue, Compare> ready_queue; + +void sjf() { + + //clock initialized to 0 + 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; + + while(completed_processes < process_count){ + + // 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++; + } + } + + //managing arrival times + for(int i = 0; i < process_count; ++i) { + //if process not in cpu + if(processes[i].in_cpu != 1) { + if(time.timer == processes[i].burst_times[0]) { + ready_queue.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.push(waiting[j]); + waiting[j]->current_burst_index++; + waiting[j] = NULL; + } + } + } + + if(CPU == NULL && !ready_queue.empty()) { + CPU = ready_queue.top(); + CPU->in_cpu = 1; + // Record in_time when the process enters the CPU + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; + ready_queue.pop(); + } + + else if(CPU != NULL){ + //check cpu_burst complete + for(int i = 0; i < process_count; ++i) { + if(processes[i].in_cpu == 1) { + if(CPU->burst_times[processes[i].current_burst_index] == 0){ + // Record out_time when the process exits the CPU + output_file << " " << time.timer << endl; + CPU->in_cpu = 0; + CPU->current_burst_index++; + waiting.push_back(CPU); // process added to waiting queue + if(!ready_queue.empty()) { + CPU = ready_queue.top(); // process added to CPU + CPU->in_cpu = 1; + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time + ready_queue.pop(); + } + else { + CPU = NULL; + } + } + } + } + } + + + if(CPU != NULL) { + // reducing the cpu burst till it reaches 0 + CPU->burst_times[CPU->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) { + // reducing the io burst till it reaches 0 + waiting[j]->burst_times[waiting[j]->current_burst_index]--; + } + } + } + + time.timer++; + } + output_file.close(); + return; +} + +// ------------------------------------Pre-emptive shortest job -------------------------------------------- + +void pre_sjf() { + + //clock initialized to 0 + 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; + + while(completed_processes < process_count){ + + // 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++; + } + } + + //managing arrival times + for(int i = 0; i < process_count; ++i) { + //if process not in cpu + if(processes[i].in_cpu != 1) { + if(time.timer == processes[i].burst_times[0]) { + ready_queue.push(&processes[i]); + if(CPU != NULL) { + ready_queue.push(CPU); + CPU->in_cpu = 0; + output_file << " " << time.timer << endl; + CPU = ready_queue.top(); + CPU->in_cpu = 1; + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time + 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(CPU != NULL) { + ready_queue.push(CPU); + CPU->in_cpu = 0; + output_file << " " << time.timer << endl; + CPU = ready_queue.top(); + CPU->in_cpu = 1; + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time + ready_queue.pop(); + + } + waiting[j]->current_burst_index++; + waiting[j] = NULL; + } + } + } + + if(CPU == NULL && !ready_queue.empty()) { + CPU = ready_queue.top(); + CPU->in_cpu = 1; + // Record in_time when the process enters the CPU + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; + ready_queue.pop(); + } + + else if(CPU != NULL){ + //check cpu_burst complete + for(int i = 0; i < process_count; ++i) { + if(processes[i].in_cpu == 1) { + if(CPU->burst_times[processes[i].current_burst_index] == 0){ + // Record out_time when the process exits the CPU + output_file << " " << time.timer << endl; + CPU->in_cpu = 0; + CPU->current_burst_index++; + waiting.push_back(CPU); // process added to waiting queue + if(!ready_queue.empty()) { + CPU = ready_queue.top(); // process added to CPU + CPU->in_cpu = 1; + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; // New entry time + ready_queue.pop(); + } + else { + CPU = NULL; + } + } + } + } + } + + + if(CPU != NULL) { + // reducing the cpu burst till it reaches 0 + CPU->burst_times[CPU->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) { + // reducing the io burst till it reaches 0 + waiting[j]->burst_times[waiting[j]->current_burst_index]--; + } + } + } + + time.timer++; + } + output_file.close(); + return; +} + + +// ------------------------------------------- Round Robin -------------------------------------------------- +// vector waiting; +void round_robin() { + //clock initialized to 0 + struct clock time; + memset(&time, 0, sizeof(struct clock)); + time.timer = 0; + time.push_signal = 0; + int process_count = processes.size(); + // memset(&waiting, 0, process_count); + int completed_processes = 0; + int time_quantum = 5; + 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 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 process not in cpu + if(processes[i].in_cpu != 1) { + if(time.timer == processes[i].burst_times[0]) { + 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; + } + } + } + + if(CPU == NULL && !ready_queue_fifo.empty()) { + CPU = ready_queue_fifo.front(); + CPU->in_cpu = 1; + // Record in_time when the process enters the CPU + output_file << "P" << CPU->pid+1 << "," << (CPU->current_burst_index + 1) / 2 << " " << time.timer; + ready_queue_fifo.pop(); + current_quantum = time_quantum; + + } + + else if(CPU != NULL){ + //check cpu_burst complete + 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){ + // Record out_time when the process exits the CPU + output_file << " " << time.timer << endl; + CPU->in_cpu = 0; + if(CPU->burst_times[processes[i].current_burst_index] == 0) CPU->current_burst_index++; + if(current_quantum == 0) ready_queue_fifo.push(CPU); + waiting.push_back(CPU); // process added to waiting queue + if(!ready_queue_fifo.empty()) { + CPU = ready_queue_fifo.front(); // process added to CPU + CPU->in_cpu = 1; + 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) { + CPU->burst_times[CPU->current_burst_index]--; + current_quantum--; + } + + 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 + } + } + } + + time.timer++; + } + output_file.close(); + return; +} + + + + +int main(int argc, char **argv) { + + if(argc != 3) + { + cout <<"usage: ./scheduler.out \nprovided arguments:\n"; + for(int i = 0; i < argc; i++) + cout << argv[i] << "\n"; + return -1; + } + + char *file_to_search_in = argv[1]; + char *scheduler_algorithm = argv[2]; + + ifstream file(file_to_search_in, ios::binary); + // ifstream file("process1.dat", ios::binary); + string buffer; + int pid = 0; + + while(getline(file, buffer)) { + if(buffer[0] == '<'){ + continue; + } + istringstream iss(buffer); + string word; + struct process_detail pd; + memset(&pd,0,sizeof(struct process_detail)); + pd.pid = pid++; + pd.current_burst_index = 0; + + while(iss>>word){ + pd.burst_times.push_back(stoi(word)); + } + processes.push_back(pd); + } + + map temp; + temp["fifo"] = 1; + temp["sjf"] = 2; + temp["pre_sjf"] = 3; + temp["rr"] = 4; + + string temp1 = scheduler_algorithm; + // string temp1 = "rr"; + + switch(temp[temp1]){ + case 1: + fifo(); + break; + case 2: + sjf(); + break; + case 3: + pre_sjf(); + break; + case 4: + round_robin(); + break; + default: + cout << "enter fifo or sjf or pre_sjf or rr" << endl; + } + + return 0; +}