├── fcfs_scheduling.cpp ├── nonpreemptive_priority.cpp ├── preemptive_priority_scheduling.cpp ├── roundrobin_scheduling.cpp ├── sjf_scheduling.cpp └── srtf_scheduling.cpp /fcfs_scheduling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct process { 7 | int pid; 8 | int arrival_time; 9 | int burst_time; 10 | int start_time; 11 | int completion_time; 12 | int turnaround_time; 13 | int waiting_time; 14 | int response_time; 15 | }; 16 | 17 | bool compareArrival(process p1, process p2) 18 | { 19 | return p1.arrival_time < p2.arrival_time; 20 | } 21 | 22 | bool compareID(process p1, process p2) 23 | { 24 | return p1.pid < p2.pid; 25 | } 26 | 27 | int main() { 28 | 29 | int n; 30 | struct process p[100]; 31 | float avg_turnaround_time; 32 | float avg_waiting_time; 33 | float avg_response_time; 34 | float cpu_utilisation; 35 | int total_turnaround_time = 0; 36 | int total_waiting_time = 0; 37 | int total_response_time = 0; 38 | int total_idle_time = 0; 39 | float throughput; 40 | 41 | cout << setprecision(2) << fixed; 42 | 43 | cout<<"Enter the number of processes: "; 44 | cin>>n; 45 | 46 | for(int i = 0; i < n; i++) { 47 | cout<<"Enter arrival time of process "<>p[i].arrival_time; 49 | cout<<"Enter burst time of process "<>p[i].burst_time; 51 | p[i].pid = i+1; 52 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct process { 8 | int pid; 9 | int arrival_time; 10 | int burst_time; 11 | int priority; 12 | int start_time; 13 | int completion_time; 14 | int turnaround_time; 15 | int waiting_time; 16 | int response_time; 17 | }; 18 | 19 | int main() { 20 | 21 | int n; 22 | struct process p[100]; 23 | float avg_turnaround_time; 24 | float avg_waiting_time; 25 | float avg_response_time; 26 | float cpu_utilisation; 27 | int total_turnaround_time = 0; 28 | int total_waiting_time = 0; 29 | int total_response_time = 0; 30 | int total_idle_time = 0; 31 | float throughput; 32 | int is_completed[100]; 33 | memset(is_completed,0,sizeof(is_completed)); 34 | 35 | cout << setprecision(2) << fixed; 36 | 37 | cout<<"Enter the number of processes: "; 38 | cin>>n; 39 | 40 | for(int i = 0; i < n; i++) { 41 | cout<<"Enter arrival time of process "<>p[i].arrival_time; 43 | cout<<"Enter burst time of process "<>p[i].burst_time; 45 | cout<<"Enter priority of the process "<>p[i].priority; 47 | p[i].pid = i+1; 48 | cout< mx) { 61 | mx = p[i].priority; 62 | idx = i; 63 | } 64 | if(p[i].priority == mx) { 65 | if(p[i].arrival_time < p[idx].arrival_time) { 66 | mx = p[i].priority; 67 | idx = i; 68 | } 69 | } 70 | } 71 | } 72 | if(idx != -1) { 73 | p[idx].start_time = current_time; 74 | p[idx].completion_time = p[idx].start_time + p[idx].burst_time; 75 | p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time; 76 | p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time; 77 | p[idx].response_time = p[idx].start_time - p[idx].arrival_time; 78 | 79 | total_turnaround_time += p[idx].turnaround_time; 80 | total_waiting_time += p[idx].waiting_time; 81 | total_response_time += p[idx].response_time; 82 | total_idle_time += p[idx].start_time - prev; 83 | 84 | is_completed[idx] = 1; 85 | completed++; 86 | current_time = p[idx].completion_time; 87 | prev = current_time; 88 | } 89 | else { 90 | current_time++; 91 | } 92 | 93 | } 94 | 95 | int min_arrival_time = 10000000; 96 | int max_completion_time = -1; 97 | for(int i = 0; i < n; i++) { 98 | min_arrival_time = min(min_arrival_time,p[i].arrival_time); 99 | max_completion_time = max(max_completion_time,p[i].completion_time); 100 | } 101 | 102 | avg_turnaround_time = (float) total_turnaround_time / n; 103 | avg_waiting_time = (float) total_waiting_time / n; 104 | avg_response_time = (float) total_response_time / n; 105 | cpu_utilisation = ((max_completion_time - total_idle_time) / (float) max_completion_time )*100; 106 | throughput = float(n) / (max_completion_time - min_arrival_time); 107 | 108 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct process { 8 | int pid; 9 | int arrival_time; 10 | int burst_time; 11 | int priority; 12 | int start_time; 13 | int completion_time; 14 | int turnaround_time; 15 | int waiting_time; 16 | int response_time; 17 | }; 18 | 19 | int main() { 20 | 21 | int n; 22 | struct process p[100]; 23 | float avg_turnaround_time; 24 | float avg_waiting_time; 25 | float avg_response_time; 26 | float cpu_utilisation; 27 | int total_turnaround_time = 0; 28 | int total_waiting_time = 0; 29 | int total_response_time = 0; 30 | int total_idle_time = 0; 31 | float throughput; 32 | int burst_remaining[100]; 33 | int is_completed[100]; 34 | memset(is_completed,0,sizeof(is_completed)); 35 | 36 | cout << setprecision(2) << fixed; 37 | 38 | cout<<"Enter the number of processes: "; 39 | cin>>n; 40 | 41 | for(int i = 0; i < n; i++) { 42 | cout<<"Enter arrival time of process "<>p[i].arrival_time; 44 | cout<<"Enter burst time of process "<>p[i].burst_time; 46 | cout<<"Enter priority of the process "<>p[i].priority; 48 | p[i].pid = i+1; 49 | burst_remaining[i] = p[i].burst_time; 50 | cout< mx) { 63 | mx = p[i].priority; 64 | idx = i; 65 | } 66 | if(p[i].priority == mx) { 67 | if(p[i].arrival_time < p[idx].arrival_time) { 68 | mx = p[i].priority; 69 | idx = i; 70 | } 71 | } 72 | } 73 | } 74 | 75 | if(idx != -1) { 76 | if(burst_remaining[idx] == p[idx].burst_time) { 77 | p[idx].start_time = current_time; 78 | total_idle_time += p[idx].start_time - prev; 79 | } 80 | burst_remaining[idx] -= 1; 81 | current_time++; 82 | prev = current_time; 83 | 84 | if(burst_remaining[idx] == 0) { 85 | p[idx].completion_time = current_time; 86 | p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time; 87 | p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time; 88 | p[idx].response_time = p[idx].start_time - p[idx].arrival_time; 89 | 90 | total_turnaround_time += p[idx].turnaround_time; 91 | total_waiting_time += p[idx].waiting_time; 92 | total_response_time += p[idx].response_time; 93 | 94 | is_completed[idx] = 1; 95 | completed++; 96 | } 97 | } 98 | else { 99 | current_time++; 100 | } 101 | } 102 | 103 | int min_arrival_time = 10000000; 104 | int max_completion_time = -1; 105 | for(int i = 0; i < n; i++) { 106 | min_arrival_time = min(min_arrival_time,p[i].arrival_time); 107 | max_completion_time = max(max_completion_time,p[i].completion_time); 108 | } 109 | 110 | avg_turnaround_time = (float) total_turnaround_time / n; 111 | avg_waiting_time = (float) total_waiting_time / n; 112 | avg_response_time = (float) total_response_time / n; 113 | cpu_utilisation = ((max_completion_time - total_idle_time) / (float) max_completion_time )*100; 114 | throughput = float(n) / (max_completion_time - min_arrival_time); 115 | 116 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct process { 9 | int pid; 10 | int arrival_time; 11 | int burst_time; 12 | int start_time; 13 | int completion_time; 14 | int turnaround_time; 15 | int waiting_time; 16 | int response_time; 17 | }; 18 | 19 | bool compare1(process p1, process p2) 20 | { 21 | return p1.arrival_time < p2.arrival_time; 22 | } 23 | 24 | bool compare2(process p1, process p2) 25 | { 26 | return p1.pid < p2.pid; 27 | } 28 | 29 | int main() { 30 | 31 | int n; 32 | int tq; 33 | struct process p[100]; 34 | float avg_turnaround_time; 35 | float avg_waiting_time; 36 | float avg_response_time; 37 | float cpu_utilisation; 38 | int total_turnaround_time = 0; 39 | int total_waiting_time = 0; 40 | int total_response_time = 0; 41 | int total_idle_time = 0; 42 | float throughput; 43 | int burst_remaining[100]; 44 | int idx; 45 | 46 | cout << setprecision(2) << fixed; 47 | 48 | cout<<"Enter the number of processes: "; 49 | cin>>n; 50 | cout<<"Enter time quantum: "; 51 | cin>>tq; 52 | 53 | for(int i = 0; i < n; i++) { 54 | cout<<"Enter arrival time of process "<>p[i].arrival_time; 56 | cout<<"Enter burst time of process "<>p[i].burst_time; 58 | burst_remaining[i] = p[i].burst_time; 59 | p[i].pid = i+1; 60 | cout< q; 66 | int current_time = 0; 67 | q.push(0); 68 | int completed = 0; 69 | int mark[100]; 70 | memset(mark,0,sizeof(mark)); 71 | mark[0] = 1; 72 | 73 | while(completed != n) { 74 | idx = q.front(); 75 | q.pop(); 76 | 77 | if(burst_remaining[idx] == p[idx].burst_time) { 78 | p[idx].start_time = max(current_time,p[idx].arrival_time); 79 | total_idle_time += p[idx].start_time - current_time; 80 | current_time = p[idx].start_time; 81 | } 82 | 83 | if(burst_remaining[idx]-tq > 0) { 84 | burst_remaining[idx] -= tq; 85 | current_time += tq; 86 | } 87 | else { 88 | current_time += burst_remaining[idx]; 89 | burst_remaining[idx] = 0; 90 | completed++; 91 | 92 | p[idx].completion_time = current_time; 93 | p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time; 94 | p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time; 95 | p[idx].response_time = p[idx].start_time - p[idx].arrival_time; 96 | 97 | total_turnaround_time += p[idx].turnaround_time; 98 | total_waiting_time += p[idx].waiting_time; 99 | total_response_time += p[idx].response_time; 100 | } 101 | 102 | for(int i = 1; i < n; i++) { 103 | if(burst_remaining[i] > 0 && p[i].arrival_time <= current_time && mark[i] == 0) { 104 | q.push(i); 105 | mark[i] = 1; 106 | } 107 | } 108 | if(burst_remaining[idx] > 0) { 109 | q.push(idx); 110 | } 111 | 112 | if(q.empty()) { 113 | for(int i = 1; i < n; i++) { 114 | if(burst_remaining[i] > 0) { 115 | q.push(i); 116 | mark[i] = 1; 117 | break; 118 | } 119 | } 120 | } 121 | 122 | 123 | } 124 | 125 | avg_turnaround_time = (float) total_turnaround_time / n; 126 | avg_waiting_time = (float) total_waiting_time / n; 127 | avg_response_time = (float) total_response_time / n; 128 | cpu_utilisation = ((p[n-1].completion_time - total_idle_time) / (float) p[n-1].completion_time)*100; 129 | throughput = float(n) / (p[n-1].completion_time - p[0].arrival_time); 130 | 131 | sort(p,p+n,compare2); 132 | 133 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct process { 8 | int pid; 9 | int arrival_time; 10 | int burst_time; 11 | int start_time; 12 | int completion_time; 13 | int turnaround_time; 14 | int waiting_time; 15 | int response_time; 16 | }; 17 | 18 | int main() { 19 | 20 | int n; 21 | struct process p[100]; 22 | float avg_turnaround_time; 23 | float avg_waiting_time; 24 | float avg_response_time; 25 | float cpu_utilisation; 26 | int total_turnaround_time = 0; 27 | int total_waiting_time = 0; 28 | int total_response_time = 0; 29 | int total_idle_time = 0; 30 | float throughput; 31 | int is_completed[100]; 32 | memset(is_completed,0,sizeof(is_completed)); 33 | 34 | cout << setprecision(2) << fixed; 35 | 36 | cout<<"Enter the number of processes: "; 37 | cin>>n; 38 | 39 | for(int i = 0; i < n; i++) { 40 | cout<<"Enter arrival time of process "<>p[i].arrival_time; 42 | cout<<"Enter burst time of process "<>p[i].burst_time; 44 | p[i].pid = i+1; 45 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct process { 8 | int pid; 9 | int arrival_time; 10 | int burst_time; 11 | int start_time; 12 | int completion_time; 13 | int turnaround_time; 14 | int waiting_time; 15 | int response_time; 16 | }; 17 | 18 | int main() { 19 | 20 | int n; 21 | struct process p[100]; 22 | float avg_turnaround_time; 23 | float avg_waiting_time; 24 | float avg_response_time; 25 | float cpu_utilisation; 26 | int total_turnaround_time = 0; 27 | int total_waiting_time = 0; 28 | int total_response_time = 0; 29 | int total_idle_time = 0; 30 | float throughput; 31 | int burst_remaining[100]; 32 | int is_completed[100]; 33 | memset(is_completed,0,sizeof(is_completed)); 34 | 35 | cout << setprecision(2) << fixed; 36 | 37 | cout<<"Enter the number of processes: "; 38 | cin>>n; 39 | 40 | for(int i = 0; i < n; i++) { 41 | cout<<"Enter arrival time of process "<>p[i].arrival_time; 43 | cout<<"Enter burst time of process "<>p[i].burst_time; 45 | p[i].pid = i+1; 46 | burst_remaining[i] = p[i].burst_time; 47 | cout<