fcfs preemptive scheduling program in c 2b 2b

Solutions on MaxInterview for fcfs preemptive scheduling program in c 2b 2b by the best coders in the world

showing results for - "fcfs preemptive scheduling program in c 2b 2b"
Miguel
05 May 2017
1#include <iostream>
2#include <algorithm> 
3#include <iomanip> 
4using namespace std;
5
6struct 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
17bool compareArrival(process p1, process p2) 
18{ 
19    return p1.arrival_time < p2.arrival_time;
20}
21
22bool compareID(process p1, process p2) 
23{  
24    return p1.pid < p2.pid;
25}
26
27int 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 "<<i+1<<": ";
48        cin>>p[i].arrival_time;
49        cout<<"Enter burst time of process "<<i+1<<": ";
50        cin>>p[i].burst_time;
51        p[i].pid = i+1;
52        cout<<endl;
53    }
54
55    sort(p,p+n,compareArrival);
56
57    for(int i = 0; i < n; i++) {
58        p[i].start_time = (i == 0)?p[i].arrival_time:max(p[i-1].completion_time,p[i].arrival_time);
59        p[i].completion_time = p[i].start_time + p[i].burst_time;
60        p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
61        p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
62        p[i].response_time = p[i].start_time - p[i].arrival_time;
63
64        total_turnaround_time += p[i].turnaround_time;
65        total_waiting_time += p[i].waiting_time;
66        total_response_time += p[i].response_time;
67        total_idle_time += (i == 0)?(p[i].arrival_time):(p[i].start_time - p[i-1].completion_time);
68    }
69
70    avg_turnaround_time = (float) total_turnaround_time / n;
71    avg_waiting_time = (float) total_waiting_time / n;
72    avg_response_time = (float) total_response_time / n;
73    cpu_utilisation = ((p[n-1].completion_time - total_idle_time) / (float) p[n-1].completion_time)*100;
74    throughput = float(n) / (p[n-1].completion_time - p[0].arrival_time);
75
76    sort(p,p+n,compareID);
77
78    cout<<endl;
79    cout<<"#P\t"<<"AT\t"<<"BT\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"RT\t"<<"\n"<<endl;
80
81    for(int i = 0; i < n; i++) {
82        cout<<p[i].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\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;
83    }
84    cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;
85    cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;
86    cout<<"Average Response Time = "<<avg_response_time<<endl;
87    cout<<"CPU Utilization = "<<cpu_utilisation<<"%"<<endl;
88    cout<<"Throughput = "<<throughput<<" process/unit time"<<endl;
89
90
91}
92