-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrtf.c
More file actions
139 lines (115 loc) · 3.33 KB
/
Copy pathsrtf.c
File metadata and controls
139 lines (115 loc) · 3.33 KB
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
//this is the code for shortest remaining time first algorithm
#include<stdio.h>
#include<limits.h>
typedef struct{
int name;
int at;
int bt;
int ct;
int tat;
int wt;
}Process;
int findShortestJob(Process processes[],int n , int currentTime , int rt[]){
int shortestJob = -1;
int minBurstTime = INT_MAX;
for(int i =0;i<n;i++){
if(processes[i].at<=currentTime&& rt[i]<=minBurstTime && rt[i]>0){
minBurstTime = rt[i];
shortestJob = i;
}
}
return shortestJob;
}
void calculateTime(Process processes[],int n){
int remainingTime[n];
int completed = 0 , shortestJob;
int currentTime = 0;
int shortestJobs[50];
for(int i=0;i<n;i++){
remainingTime[i]=processes[i].bt;
}
int i=0;
while(completed<n){
shortestJob = findShortestJob(processes, n , currentTime , remainingTime);
printf("Executing P%d\n",shortestJob);
shortestJobs[i++] = shortestJob;
if(shortestJob==-1){
currentTime++;
continue;
}
remainingTime[shortestJob]--;
currentTime++;
if(remainingTime[shortestJob]==0){
processes[shortestJob].ct = currentTime;
processes[shortestJob].tat = processes[shortestJob].ct - processes[shortestJob].at;
processes[shortestJob].wt = processes[shortestJob].tat - processes[shortestJob].bt;
completed++;
}
}
}
int main(){
int noOfProcess;
int i;
printf("Enter number of process:\n");
scanf("%d",&noOfProcess);
Process processes[noOfProcess];
printf("Enter the name , arrival time and burst time of the processes:\n");
for(i=0;i<noOfProcess;i++){
scanf("%d %d %d",&processes[i].name,&processes[i].at,&processes[i].bt);
}
calculateTime(processes,noOfProcess);
//finding average tat and wt of the processes
int totalWT = 0,totalTT = 0;
float avgWaitingTime,avgTurnaroundTime;
for(i=0;i<noOfProcess;i++){
totalWT+=processes[i].wt;
totalTT+=processes[i].tat;
}
avgWaitingTime = (float)totalWT/noOfProcess;
avgTurnaroundTime = (float)totalTT/noOfProcess;
printf("NAME \t AT \t BT \t CT \t TAT \t WT \n");
for(i=0;i<noOfProcess;i++){
printf("%d \t %d \t %d \t %d \t %d \t %d \n",processes[i].name,processes[i].at,processes[i].bt,processes[i].ct,processes[i].tat,processes[i].wt);
}
printf("Average Waiting Time : %f\n",avgWaitingTime);
printf("Average TurnAroundTime : %f\n",avgTurnaroundTime);
return 0;
}
/*SAMPLE OUTPUT
Enter number of process:
5
Enter the name , arrival time and burst time of the processes:
1 0 8
2 1 1
3 2 3
4 3 2
5 4 6
Executing P0
Executing P1
Executing P2
Executing P3
Executing P3
Executing P2
Executing P2
Executing P4
Executing P4
Executing P4
Executing P4
Executing P4
Executing P4
Executing P0
Executing P0
Executing P0
Executing P0
Executing P0
Executing P0
Executing P0
NAME AT BT CT TAT WT
1 0 8 20 20 12
2 1 1 2 1 0
3 2 3 7 5 2
4 3 2 5 2 0
5 4 6 13 9 3
Average Waiting Time : 3.400000
Average TurnAroundTime : 7.400000
*/