├── makefile ├── testcases ├── 12a-input.txt ├── 01a-input.txt ├── 01b-input.txt ├── 04a-input.txt ├── 04b-input.txt ├── 05a-input.txt ├── 05b-input.txt ├── 06a-input.txt ├── 06b-input.txt ├── 07a-input.txt ├── 07b-input.txt ├── 08a-input.txt ├── 08b-input.txt ├── 02a-input.txt ├── 02b-input.txt ├── 03a-input.txt ├── 03b-input.txt ├── 09a-input.txt ├── 09b-input.txt ├── 11a-input.txt ├── 10a-input.txt ├── 10b-input.txt ├── 12a-output.txt ├── 04b-output.txt ├── 05b-output.txt ├── 01b-output.txt ├── 02b-output.txt ├── 03b-output.txt ├── 06b-output.txt ├── 07b-output.txt ├── 08b-output.txt ├── 09b-output.txt ├── 01a-output.txt ├── 02a-output.txt ├── 03a-output.txt ├── 04a-output.txt ├── 05a-output.txt ├── 06a-output.txt ├── 07a-output.txt ├── 08a-output.txt ├── 09a-output.txt ├── 11a-output.txt ├── 10b-output.txt └── 10a-output.txt ├── .gitignore ├── parser.h ├── README.md └── main.cpp /makefile: -------------------------------------------------------------------------------- 1 | target: 2 | g++ -c main.cpp 3 | g++ -o lab4 main.o 4 | 5 | -------------------------------------------------------------------------------- /testcases/12a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 8-1 3 | 20 4 | 2 5 | A,0,2 6 | B,2,4 7 | -------------------------------------------------------------------------------- /testcases/01a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 1 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/01b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 1 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/04a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 3 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/04b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 3 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/05a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 4 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/05b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 4 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/06a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 5 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/06b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 5 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/07a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 6 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/07b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 6 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/08a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 7 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/08b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 7 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/02a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 2-1 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/02b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 2-1 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/03a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 2-4 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/03b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 2-4 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/09a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 2-2 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/09b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 2-2 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/11a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 8-1 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/10a-input.txt: -------------------------------------------------------------------------------- 1 | trace 2 | 1,2-1,2-4,3,4,5,6,7,2-2 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/10b-input.txt: -------------------------------------------------------------------------------- 1 | stats 2 | 1,2-1,2-4,3,4,5,6,7,2-2 3 | 20 4 | 5 5 | A,0,3 6 | B,2,6 7 | C,4,4 8 | D,6,5 9 | E,8,2 10 | -------------------------------------------------------------------------------- /testcases/12a-output.txt: -------------------------------------------------------------------------------- 1 | Aging 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|.|.|*|.|.|*|.|.|*|.|.|*|.|.|*|.|.|*| 4 | B | | |*|*|.|*|*|.|*|*|.|*|*|.|*|*|.|*|*|.| 5 | ------------------------------------------------ 6 | -------------------------------------------------------------------------------- /testcases/04b-output.txt: -------------------------------------------------------------------------------- 1 | SPN 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 3 | 9 | 15 | 20 | 11 |-----| 6 | Turnaround | 3 | 7 | 11 | 14 | 3 | 7.60| 7 | NormTurn | 1.00| 1.17| 2.75| 2.80| 1.50| 1.84| 8 | 9 | -------------------------------------------------------------------------------- /testcases/05b-output.txt: -------------------------------------------------------------------------------- 1 | SRT 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 3 | 15 | 8 | 20 | 10 |-----| 6 | Turnaround | 3 | 13 | 4 | 14 | 2 | 7.20| 7 | NormTurn | 1.00| 2.17| 1.00| 2.80| 1.00| 1.59| 8 | 9 | -------------------------------------------------------------------------------- /testcases/01b-output.txt: -------------------------------------------------------------------------------- 1 | FCFS 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 3 | 9 | 13 | 18 | 20 |-----| 6 | Turnaround | 3 | 7 | 9 | 12 | 12 | 8.60| 7 | NormTurn | 1.00| 1.17| 2.25| 2.40| 6.00| 2.56| 8 | 9 | -------------------------------------------------------------------------------- /testcases/02b-output.txt: -------------------------------------------------------------------------------- 1 | RR-1 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 4 | 18 | 17 | 20 | 15 |-----| 6 | Turnaround | 4 | 16 | 13 | 14 | 7 |10.80| 7 | NormTurn | 1.33| 2.67| 3.25| 2.80| 3.50| 2.71| 8 | 9 | -------------------------------------------------------------------------------- /testcases/03b-output.txt: -------------------------------------------------------------------------------- 1 | RR-4 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 3 | 17 | 11 | 20 | 19 |-----| 6 | Turnaround | 3 | 15 | 7 | 14 | 11 |10.00| 7 | NormTurn | 1.00| 2.50| 1.75| 2.80| 5.50| 2.71| 8 | 9 | -------------------------------------------------------------------------------- /testcases/06b-output.txt: -------------------------------------------------------------------------------- 1 | HRRN 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 3 | 9 | 13 | 20 | 15 |-----| 6 | Turnaround | 3 | 7 | 9 | 14 | 7 | 8.00| 7 | NormTurn | 1.00| 1.17| 2.25| 2.80| 3.50| 2.14| 8 | 9 | -------------------------------------------------------------------------------- /testcases/07b-output.txt: -------------------------------------------------------------------------------- 1 | FB-1 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 4 | 20 | 16 | 19 | 11 |-----| 6 | Turnaround | 4 | 18 | 12 | 13 | 3 |10.00| 7 | NormTurn | 1.33| 3.00| 3.00| 2.60| 1.50| 2.29| 8 | 9 | -------------------------------------------------------------------------------- /testcases/08b-output.txt: -------------------------------------------------------------------------------- 1 | FB-2i 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 4 | 17 | 18 | 20 | 14 |-----| 6 | Turnaround | 4 | 15 | 14 | 14 | 6 |10.60| 7 | NormTurn | 1.33| 2.50| 3.50| 2.80| 3.00| 2.63| 8 | 9 | -------------------------------------------------------------------------------- /testcases/09b-output.txt: -------------------------------------------------------------------------------- 1 | RR-2 2 | Process | A | B | C | D | E | 3 | Arrival | 0 | 2 | 4 | 6 | 8 | 4 | Service | 3 | 6 | 4 | 5 | 2 | Mean| 5 | Finish | 5 | 17 | 13 | 20 | 15 |-----| 6 | Turnaround | 5 | 15 | 9 | 14 | 7 |10.00| 7 | NormTurn | 1.67| 2.50| 2.25| 2.80| 3.50| 2.54| 8 | 9 | -------------------------------------------------------------------------------- /testcases/01a-output.txt: -------------------------------------------------------------------------------- 1 | FCFS 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|*| | | | | | | | | | | | | | | | | | 4 | B | | |.|*|*|*|*|*|*| | | | | | | | | | | | 5 | C | | | | |.|.|.|.|.|*|*|*|*| | | | | | | | 6 | D | | | | | | |.|.|.|.|.|.|.|*|*|*|*|*| | | 7 | E | | | | | | | | |.|.|.|.|.|.|.|.|.|.|*|*| 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/02a-output.txt: -------------------------------------------------------------------------------- 1 | RR-1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|.|*| | | | | | | | | | | | | | | | | 4 | B | | |*|.|*|.|*|.|.|*|.|.|.|*|.|.|.|*| | | 5 | C | | | | |.|*|.|.|*|.|.|.|*|.|.|.|*| | | | 6 | D | | | | | | |.|*|.|.|.|*|.|.|.|*|.|.|*|*| 7 | E | | | | | | | | |.|.|*|.|.|.|*| | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/03a-output.txt: -------------------------------------------------------------------------------- 1 | RR-4 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|*| | | | | | | | | | | | | | | | | | 4 | B | | |.|*|*|*|*|.|.|.|.|.|.|.|.|*|*| | | | 5 | C | | | | |.|.|.|*|*|*|*| | | | | | | | | | 6 | D | | | | | | |.|.|.|.|.|*|*|*|*|.|.|.|.|*| 7 | E | | | | | | | | |.|.|.|.|.|.|.|.|.|*|*| | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/04a-output.txt: -------------------------------------------------------------------------------- 1 | SPN 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|*| | | | | | | | | | | | | | | | | | 4 | B | | |.|*|*|*|*|*|*| | | | | | | | | | | | 5 | C | | | | |.|.|.|.|.|.|.|*|*|*|*| | | | | | 6 | D | | | | | | |.|.|.|.|.|.|.|.|.|*|*|*|*|*| 7 | E | | | | | | | | |.|*|*| | | | | | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/05a-output.txt: -------------------------------------------------------------------------------- 1 | SRT 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|*| | | | | | | | | | | | | | | | | | 4 | B | | |.|*|.|.|.|.|.|.|*|*|*|*|*| | | | | | 5 | C | | | | |*|*|*|*| | | | | | | | | | | | | 6 | D | | | | | | |.|.|.|.|.|.|.|.|.|*|*|*|*|*| 7 | E | | | | | | | | |*|*| | | | | | | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/06a-output.txt: -------------------------------------------------------------------------------- 1 | HRRN 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|*| | | | | | | | | | | | | | | | | | 4 | B | | |.|*|*|*|*|*|*| | | | | | | | | | | | 5 | C | | | | |.|.|.|.|.|*|*|*|*| | | | | | | | 6 | D | | | | | | |.|.|.|.|.|.|.|.|.|*|*|*|*|*| 7 | E | | | | | | | | |.|.|.|.|.|*|*| | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/07a-output.txt: -------------------------------------------------------------------------------- 1 | FB-1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|.|*| | | | | | | | | | | | | | | | | 4 | B | | |*|.|.|*|.|.|.|.|.|*|.|.|*|.|.|*|.|*| 5 | C | | | | |*|.|.|*|.|.|.|.|*|.|.|*| | | | | 6 | D | | | | | | |*|.|.|*|.|.|.|*|.|.|*|.|*| | 7 | E | | | | | | | | |*|.|*| | | | | | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/08a-output.txt: -------------------------------------------------------------------------------- 1 | FB-2i 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|.|*| | | | | | | | | | | | | | | | | 4 | B | | |*|.|.|*|*|.|.|.|.|.|.|.|*|*|*| | | | 5 | C | | | | |*|.|.|.|.|*|*|.|.|.|.|.|.|*| | | 6 | D | | | | | | |.|*|.|.|.|*|*|.|.|.|.|.|*|*| 7 | E | | | | | | | | |*|.|.|.|.|*| | | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/09a-output.txt: -------------------------------------------------------------------------------- 1 | RR-2 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|.|.|*| | | | | | | | | | | | | | | | 4 | B | | |*|*|.|.|.|*|*|.|.|.|.|.|.|*|*| | | | 5 | C | | | | |.|*|*|.|.|.|.|*|*| | | | | | | | 6 | D | | | | | | |.|.|.|*|*|.|.|.|.|.|.|*|*|*| 7 | E | | | | | | | | |.|.|.|.|.|*|*| | | | | | 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /testcases/11a-output.txt: -------------------------------------------------------------------------------- 1 | Aging 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 2 | ------------------------------------------------ 3 | A |*|*|.|.|.|*|.|.|.|.|*|.|.|.|.|*|.|.|.|.| 4 | B | | |*|*|*|.|.|*|.|*|.|.|.|*|.|.|.|.|*|.| 5 | C | | | | |.|.|*|.|.|.|.|*|.|.|.|.|*|.|.|.| 6 | D | | | | | | |.|.|*|.|.|.|*|.|.|.|.|*|.|.| 7 | E | | | | | | | | |.|.|.|.|.|.|*|.|.|.|.|*| 8 | ------------------------------------------------ 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | .vscode 35 | CPU-Scheduling-Algorithms.layout 36 | CPU-Scheduling-Algorithms.depend 37 | input.txt 38 | main 39 | -------------------------------------------------------------------------------- /parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H_INCLUDED 2 | #define PARSER_H_INCLUDED 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | /** This file handles parsing the data we are going to work with **/ 9 | /** It also holds all the global variables we parse into **/ 10 | 11 | 12 | string operation; 13 | int last_instant, process_count; 14 | vector> algorithms; 15 | vector> processes; 16 | vector>timeline; 17 | unordered_mapprocessToIndex; 18 | 19 | 20 | //Results 21 | 22 | vectorfinishTime; 23 | vectorturnAroundTime; 24 | vectornormTurn; 25 | 26 | 27 | void parse_algorithms(string algorithm_chunk) 28 | { 29 | stringstream stream(algorithm_chunk); 30 | while (stream.good()) 31 | { 32 | string temp_str; 33 | getline(stream, temp_str, ','); 34 | stringstream ss(temp_str); 35 | getline(ss, temp_str, '-'); 36 | char algorithm_id = temp_str[0]; 37 | getline(ss, temp_str, '-'); 38 | int quantum = temp_str.size() >= 1 ? stoi(temp_str) : -1; 39 | algorithms.push_back( make_pair(algorithm_id, quantum) ); 40 | } 41 | } 42 | 43 | void parse_processes() 44 | { 45 | string process_chunk, process_name; 46 | int process_arrival_time, process_service_time; 47 | for(int i=0; i> process_chunk; 50 | 51 | stringstream stream(process_chunk); 52 | string temp_str; 53 | getline(stream, temp_str, ','); 54 | process_name = temp_str; 55 | getline(stream, temp_str, ','); 56 | process_arrival_time = stoi(temp_str); 57 | getline(stream, temp_str, ','); 58 | process_service_time = stoi(temp_str); 59 | 60 | processes.push_back( make_tuple(process_name, process_arrival_time, process_service_time) ); 61 | processToIndex[process_name] = i; 62 | } 63 | } 64 | 65 | void parse() 66 | { 67 | string algorithm_chunk; 68 | cin >> operation >> algorithm_chunk >> last_instant >> process_count; 69 | parse_algorithms(algorithm_chunk); 70 | parse_processes(); 71 | finishTime.resize(process_count); 72 | turnAroundTime.resize(process_count); 73 | normTurn.resize(process_count); 74 | timeline.resize(last_instant); 75 | for(int i=0; i Check the attached [testcases](https://github.com/yousefkotp/CPU-Scheduling-Algorithms/tree/main/testcases) for more details. 133 | 134 | 135 | ## Contributors 136 | 137 | - [Yousef Kotp](https://github.com/yousefkotp) 138 | 139 | - [Adham Mohammed](https://github.com/adhammohamed1) 140 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "parser.h" 3 | 4 | #define all(v) v.begin(), v.end() 5 | 6 | using namespace std; 7 | 8 | /** Global Constants **/ 9 | const string TRACE = "trace"; 10 | const string SHOW_STATISTICS = "stats"; 11 | const string ALGORITHMS[9] = {"", "FCFS", "RR-", "SPN", "SRT", "HRRN", "FB-1", "FB-2i", "AGING"}; 12 | 13 | bool sortByServiceTime(const tuple &a, const tuple &b) 14 | { 15 | return (get<2>(a) < get<2>(b)); 16 | } 17 | bool sortByArrivalTime(const tuple &a, const tuple &b) 18 | { 19 | return (get<1>(a) < get<1>(b)); 20 | } 21 | 22 | bool descendingly_by_response_ratio(tuple a, tuple b) 23 | { 24 | return get<1>(a) > get<1>(b); 25 | } 26 | 27 | bool byPriorityLevel (const tuple&a,const tuple&b){ 28 | if(get<0>(a)==get<0>(b)) 29 | return get<2>(a)> get<2>(b); 30 | return get<0>(a) > get<0>(b); 31 | } 32 | 33 | void clear_timeline() 34 | { 35 | for(int i=0; i &a) 41 | { 42 | return get<0>(a); 43 | } 44 | 45 | int getArrivalTime(tuple &a) 46 | { 47 | return get<1>(a); 48 | } 49 | 50 | int getServiceTime(tuple &a) 51 | { 52 | return get<2>(a); 53 | } 54 | 55 | int getPriorityLevel(tuple &a) 56 | { 57 | return get<2>(a); 58 | } 59 | 60 | double calculate_response_ratio(int wait_time, int service_time) 61 | { 62 | return (wait_time + service_time)*1.0 / service_time; 63 | } 64 | 65 | void fillInWaitTime(){ 66 | for (int i = 0; i < process_count; i++) 67 | { 68 | int arrivalTime = getArrivalTime(processes[i]); 69 | for (int k = arrivalTime; k < finishTime[i]; k++) 70 | { 71 | if (timeline[k][i] != '*') 72 | timeline[k][i] = '.'; 73 | } 74 | } 75 | } 76 | 77 | void firstComeFirstServe() 78 | { 79 | int time = getArrivalTime(processes[0]); 80 | for (int i = 0; i < process_count; i++) 81 | { 82 | int processIndex = i; 83 | int arrivalTime = getArrivalTime(processes[i]); 84 | int serviceTime = getServiceTime(processes[i]); 85 | 86 | finishTime[processIndex] = (time + serviceTime); 87 | turnAroundTime[processIndex] = (finishTime[processIndex] - arrivalTime); 88 | normTurn[processIndex] = (turnAroundTime[processIndex] * 1.0 / serviceTime); 89 | 90 | for (int j = time; j < finishTime[processIndex]; j++) 91 | timeline[j][processIndex] = '*'; 92 | for (int j = arrivalTime; j < time; j++) 93 | timeline[j][processIndex] = '.'; 94 | time += serviceTime; 95 | } 96 | } 97 | 98 | void roundRobin(int originalQuantum) 99 | { 100 | queue>q; 101 | int j=0; 102 | if(getArrivalTime(processes[j])==0){ 103 | q.push(make_pair(j,getServiceTime(processes[j]))); 104 | j++; 105 | } 106 | int currentQuantum = originalQuantum; 107 | for(int time =0;time, vector>, greater>> pq; // pair of service time and index 150 | int j = 0; 151 | for (int i = 0; i < last_instant; i++) 152 | { 153 | while(j, vector>, greater>> pq; 183 | int j = 0; 184 | for (int i = 0; i < last_instant; i++) 185 | { 186 | while(j for processes that are in the ready queue 218 | vector> present_processes; 219 | int j=0; 220 | for (int current_instant = 0; current_instant < last_instant; current_instant++) 221 | { 222 | while(j(proc); 230 | int process_index = processToIndex[process_name]; 231 | int wait_time = current_instant - getArrivalTime(processes[process_index]); 232 | int service_time = getServiceTime(processes[process_index]); 233 | get<1>(proc) = calculate_response_ratio(wait_time, service_time); 234 | } 235 | 236 | // Sort present processes by highest to lowest response ratio 237 | sort(all(present_processes), descendingly_by_response_ratio); 238 | 239 | if (!present_processes.empty()) 240 | { 241 | int process_index = processToIndex[get<0>(present_processes[0])]; 242 | while(current_instant(present_processes[0]) != getServiceTime(processes[process_index])){ 243 | timeline[current_instant][process_index]='*'; 244 | current_instant++; 245 | get<2>(present_processes[0])++; 246 | } 247 | current_instant--; 248 | present_processes.erase(present_processes.begin()); 249 | finishTime[process_index] = current_instant + 1; 250 | turnAroundTime[process_index] = finishTime[process_index] - getArrivalTime(processes[process_index]); 251 | normTurn[process_index] = (turnAroundTime[process_index] * 1.0 / getServiceTime(processes[process_index])); 252 | } 253 | } 254 | fillInWaitTime(); 255 | } 256 | 257 | void feedbackQ1() 258 | { 259 | priority_queue, vector>, greater>> pq; //pair of priority level and process index 260 | unordered_mapremainingServiceTime; //map from process index to the remaining service time 261 | int j=0; 262 | if(getArrivalTime(processes[0])==0){ 263 | pq.push(make_pair(0,j)); 264 | remainingServiceTime[j]=getServiceTime(processes[j]); 265 | j++; 266 | } 267 | for(int time =0;time=1) 287 | pq.push(make_pair(priorityLevel+1,processIndex)); 288 | else 289 | pq.push(make_pair(priorityLevel,processIndex)); 290 | } 291 | } 292 | while(j, vector>, greater>> pq; //pair of priority level and process index 304 | unordered_mapremainingServiceTime; //map from process index to the remaining service time 305 | int j=0; 306 | if(getArrivalTime(processes[0])==0){ 307 | pq.push(make_pair(0,j)); 308 | remainingServiceTime[j]=getServiceTime(processes[j]); 309 | j++; 310 | } 311 | for(int time =0;time=1) 338 | pq.push(make_pair(priorityLevel+1,processIndex)); 339 | else 340 | pq.push(make_pair(priorityLevel,processIndex)); 341 | } 342 | time = temp-1; 343 | } 344 | while(j>v; //tuple of priority level, process index and total waiting time 356 | int j=0,currentProcess=-1; 357 | for(int time =0;time(v[i])==currentProcess){ 365 | get<2>(v[i])=0; 366 | get<0>(v[i])=getPriorityLevel(processes[currentProcess]); 367 | } 368 | else{ 369 | get<0>(v[i])++; 370 | get<2>(v[i])++; 371 | } 372 | } 373 | sort(v.begin(),v.end(),byPriorityLevel); 374 | currentProcess=get<1>(v[0]); 375 | int currentQuantum = originalQuantum; 376 | while(currentQuantum-- && time=10) 432 | printf("%2.2f|\n",(1.0 * sum / turnAroundTime.size())); 433 | else 434 | printf(" %2.2f|\n",(1.0 * sum / turnAroundTime.size())); 435 | } 436 | 437 | void printNormTurn() 438 | { 439 | cout << "NormTurn |"; 440 | float sum = 0; 441 | for (int i = 0; i < process_count; i++) 442 | { 443 | if( normTurn[i]>=10 ) 444 | printf("%2.2f|",normTurn[i]); 445 | else 446 | printf(" %2.2f|",normTurn[i]); 447 | sum += normTurn[i]; 448 | } 449 | 450 | if( (1.0 * sum / normTurn.size()) >=10 ) 451 | printf("%2.2f|\n",(1.0 * sum / normTurn.size())); 452 | else 453 | printf(" %2.2f|\n",(1.0 * sum / normTurn.size())); 454 | } 455 | void printStats(int algorithm_index) 456 | { 457 | printAlgorithm(algorithm_index); 458 | printProcesses(); 459 | printArrivalTime(); 460 | printServiceTime(); 461 | printFinishTime(); 462 | printTurnAroundTime(); 463 | printNormTurn(); 464 | } 465 | 466 | void printTimeline(int algorithm_index) 467 | { 468 | for (int i = 0; i <= last_instant; i++) 469 | cout << i % 10<<" "; 470 | cout <<"\n"; 471 | cout << "------------------------------------------------\n"; 472 | for (int i = 0; i < process_count; i++) 473 | { 474 | cout << getProcessName(processes[i]) << " |"; 475 | for (int j = 0; j < last_instant; j++) 476 | { 477 | cout << timeline[j][i]<<"|"; 478 | } 479 | cout << " \n"; 480 | } 481 | cout << "------------------------------------------------\n"; 482 | } 483 | 484 | void execute_algorithm(char algorithm_id, int quantum,string operation) 485 | { 486 | switch (algorithm_id) 487 | { 488 | case '1': 489 | if(operation==TRACE)cout<<"FCFS "; 490 | firstComeFirstServe(); 491 | break; 492 | case '2': 493 | if(operation==TRACE)cout<<"RR-"<