-
Notifications
You must be signed in to change notification settings - Fork 1
/
05_3_LRTF_CPUSchudling.c
140 lines (122 loc) · 4.38 KB
/
05_3_LRTF_CPUSchudling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<limits.h>
// Structure
typedef struct process {
int processId;
int arrivalTime;
int burstTime;
int remainingTime;
int startTime;
int completedTime;
int turnaroundTime;
int waitingTime;
int responseTime;
bool isCompleted;
};
// Variables
int n = 0, cpuTime = 0, completed = 0, firstProcess = -1;
struct process p[100];
float sumWaitingTime = 0.0, sumTurnaroundTime = 0.0, sumResponseTime = 0.0, sumBurstTime = 0.0;
float averageWaitingTime = 0.0, averageTurnaroundTime = 0.0, averageResponseTime = 0.0;
float cpuUtilization = 0.0;
float throughput = 0.0;
// Function Declaration & Body
void input(){
printf("\nEnter Number of Process : ");
scanf("%d", &n);
printf("\nEnter Process Arrival Time & Burst Time (Space Seprated) -->");
for( int i = 0; i < n; i++) {
if( i == 0 ) printf("\n\t1'st Process : ");
else if( i == 1 ) printf("\t2'nd Process : ");
else if( i == 2 ) printf("\t3'rd Process : ");
else printf("\t%d'th Process : ", i+1);
scanf("%d%d", &p[i].arrivalTime, &p[i].burstTime);
p[i].processId = i;
p[i].isCompleted = false;
p[i].remainingTime = p[i].burstTime;
}
}
int getprocess() {
int id = -1;
int max = INT_MIN;
for( int i = 0 ; i < n ; i++ ) {
if( p[i].arrivalTime <= cpuTime && p[i].isCompleted == false ) {
if( p[i].remainingTime > max ) {
max = p[i].remainingTime;
id = p[i].processId;
}
if( p[i].remainingTime == max ) {
if( p[i].arrivalTime < p[id].arrivalTime ) {
max = p[i].remainingTime;
id = p[i].processId;
} else if( p[i].arrivalTime == p[id].arrivalTime ) {
if( p[i].processId < id ) {
max = p[i].remainingTime;
id = p[i].processId;
}
}
}
}
}
return id;
}
void LRTF_Algo() {
while( completed < n ) {
int id = getprocess();
if( id == -1 ) {
cpuTime++;
} else {
if( firstProcess == -1 && completed == 0 ) {
firstProcess = id;
}
if( p[id].remainingTime == p[id].burstTime ) {
p[id].startTime = cpuTime;
}
cpuTime++;
p[id].remainingTime--;
if( p[id].remainingTime == 0 ) {
p[id].completedTime = cpuTime;
p[id].turnaroundTime = p[id].completedTime - p[id].arrivalTime;
p[id].waitingTime = p[id].turnaroundTime - p[id].burstTime;
p[id].responseTime = p[id].startTime - p[id].arrivalTime;
p[id].isCompleted = true;
completed++;
}
}
}
}
void calclate(){
for( int i = 0 ; i < n ; i++ ) {
sumBurstTime += (float)p[i].burstTime;
sumResponseTime += (float)p[i].responseTime;
sumTurnaroundTime += (float)p[i].turnaroundTime;
sumWaitingTime += (float)p[i].waitingTime;
}
averageResponseTime = sumResponseTime/(float)n;
averageTurnaroundTime = sumTurnaroundTime/(float)n;
averageWaitingTime = sumWaitingTime/(float)n;
throughput = (float) n/( cpuTime - p[firstProcess].arrivalTime );
cpuUtilization = (100 * sumBurstTime)/(float)cpuTime;
}
void output(){
printf("\nNumber of Process : %d\n", n);
printf("Process Table -->");
printf("\n ID AT BT ST CT TAT WT RT COMPLETE");
for( int i = 0; i < n; i++) {
printf("\n %7d %7d %7d %7d %7d %7d %7d %7d %11s", p[i].processId, p[i].arrivalTime, p[i].burstTime, p[i].startTime, p[i].completedTime, p[i].turnaroundTime, p[i].waitingTime, p[i].responseTime, p[i].isCompleted ? "TRUE" : "FAlSE" );
}
printf("\nAverage Turnaround Time : %f", averageTurnaroundTime);
printf("\nAverage Waiting Time : %f", averageWaitingTime);
printf("\nAverage Response Time : %f", averageResponseTime);
printf("\nThroughPut : %f", throughput);
printf("\nCPU Utilization : %f\n", cpuUtilization);
}
// Function Call
int main() {
input();
LRTF_Algo();
calclate();
output();
}