preemptive priority scheduling implementation in c

Solutions on MaxInterview for preemptive priority scheduling implementation in c by the best coders in the world

showing results for - "preemptive priority scheduling implementation in c"
Ilona
28 Jul 2017
1#include <iostream>
2#include <algorithm> 
3#include <iomanip>
4#include <string.h> 
5using namespace std;
6
7struct 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
19int 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 "<<i+1<<": ";
43        cin>>p[i].arrival_time;
44        cout<<"Enter burst time of process "<<i+1<<": ";
45        cin>>p[i].burst_time;
46        cout<<"Enter priority of the process "<<i+1<<": ";
47        cin>>p[i].priority;
48        p[i].pid = i+1;
49        burst_remaining[i] = p[i].burst_time;
50        cout<<endl;
51    }
52
53    int current_time = 0;
54    int completed = 0;
55    int prev = 0;
56
57    while(completed != n) {
58        int idx = -1;
59        int mx = -1;
60        for(int i = 0; i < n; i++) {
61            if(p[i].arrival_time <= current_time && is_completed[i] == 0) {
62                if(p[i].priority > 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<<endl<<endl;
117
118    cout<<"#P\t"<<"AT\t"<<"BT\t"<<"PRI\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"RT\t"<<"\n"<<endl;
119
120    for(int i = 0; i < n; i++) {
121        cout<<p[i].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\t"<<p[i].priority<<"\t"<<p[i].start_time<<"\t"<<p[i].completion_time<<"\t"<<p[i].turnaround_time<<"\t"<<p[i].waiting_time<<"\t"<<p[i].response_time<<"\t"<<"\n"<<endl;
122    }
123    cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;
124    cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;
125    cout<<"Average Response Time = "<<avg_response_time<<endl;
126    cout<<"CPU Utilization = "<<cpu_utilisation<<"%"<<endl;
127    cout<<"Throughput = "<<throughput<<" process/unit time"<<endl;
128
129
130}
131
132/*
133
134AT - Arrival Time of the process
135BT - Burst time of the process
136ST - Start time of the process
137CT - Completion time of the process
138TAT - Turnaround time of the process
139WT - Waiting time of the process
140RT - Response time of the process
141
142Formulas used:
143
144TAT = CT - AT
145WT = TAT - BT
146RT = ST - AT
147
148*/
149
150}
151
152
queries leading to this page
non preemptive priority scheduling programpriority preemptive scheduling examplec program for preemptive priority schedulingpreemptive priority scheduling lower order code in cpriority scheduling algorithm program non preemptivepriority scheduling preemptive program in cc program to demonstrate the concept of preemptive priority scheduling algorithmpreemptive priority scheduling program in cnon pre emptive priority scheduling program in cprogram of non preemptive priority sheduling in cpriority preemptive scheduling c 2b 2bpriority scheduling preemptive algorithmpreemptive priority scheduling programpreemptive priority scheduling questionspreemptive priority scheduling implementation in cnon preemptive priority cpu scheduling algorithm in cpreemptive priority scheduling algorithm program in cpriority scheduling preemptivepreemptive priority cpu scheduling program in cwrite a c program to demonstrate priority scheduling 28preemptive with respect to arrival time 29to implement preemptive priority scheduling algorithm preemptive priority scheduling c 2b 2bnon preemptive priority scheduling c 2b 2balgorithm preemptive priority schedulingto implement preemptive priority scheduling algorithm c 2b 2bpreemptive priority scheduling algorithmto implement preemptive priority scheduling algorithm cnon preemptive priority scheduling program in cpriority scheduling in preemptive methodpreemptive priority scheduling algorithm example in ospreemptive priority scheduling algo in cpreemptive priority scheduling ascending order in cpreemptive priority scheduling algorithm in c using structureepriority cpu scheduling program in javaalgorithm for c program of preemptive priority schedulingpriority scheduling preemptive c 2b 2bc program to implement priority scheduling algorithmpriority preemptive scheduling program in cpriority non preemptive scheduling program in c 2b 2b with outputpriority non preemptive scheduling program in cpreemptive priority scheduling program finderpreemptive priority scheduling algorithm in cnon preemptive priority scheduling program in c programpreemptive priority schedulingpreemptive priority scheduling examplesjf preemptive scheduling program in c 2b 2bpreemptive priority scheduling c programpreemptive priority based schedulingin priority preemptive schedulingnon preemptive priority scheduling in cpreemptive priority scheduling dvntges preemptive priority based scheduling algorithmpreemptive priority scheduling in operating systempriority scheduling program in c non preamptivec program for non preemptive priority schedulingpreemptive priority scheduling algorithm in c using structurepreemptive priority scheduling program in cppwrite a program in c to implement non preemptive priority schedulingpreemptive scheduling c 2b 2bnon preemptive priority scheduling codepriority scheduling non preemptive c 2b 2bpreemptive priority scheduling in cpriority scheduling non preemptive in cpreemptive priority scheduling cpppre emptive priority scheduling algorithm in cpriority preemptive scheduling programpreemptive priority scheduling implementation in c