-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRTF_scheduling.c
148 lines (122 loc) · 3.22 KB
/
SRTF_scheduling.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
141
142
143
144
145
146
147
148
// C program to implement Shortest Remaining Time First
// (SRTF) or Preemptive Shortest Job First (SJF)
// SRTF is a preemptive algorithm, which means that the
// currently running process can be interrupted if a
// new process arrives with a shorter burst time. It is
// a dynamic algorithm and has a low average waiting time.
// Time complexity is O(n) and space complexity is O(n).
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define FALSE 0
#define TRUE 1
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
// Function prototypes
void FindWaitingTime(struct Process *,int,int *);
void FindTurnAroundTime(struct Process *,int,int *,int *);
void FindAvgTime(struct Process *, int);
// Function to find the waiting time for all
// processes
void FindWaitingTime(struct Process proc[], int n,int wt[])
{
int rt[n];
// Copy the burst time into rt[]
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
int check = FALSE;
// Process until all processes are
// completed
while (complete != n) {
// Find process with minimum
// remaining time among the
// processes that arrive until the
// current time`
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = TRUE;
}
}
if (check == FALSE) {
t++;
continue;
}
// Reduce remaining time by one
rt[shortest]--;
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// If a process gets completely
// executed
if (rt[shortest] == 0) {
// Increment complete
complete++;
check = FALSE;
// Find finish time of current
// process
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate turn around time
void FindTurnAroundTime(struct Process proc[], int n,
int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
// Function to calculate average time
void FindAvgTime(struct Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
// Function to find waiting time of all
// processes
FindWaitingTime(proc, n, wt);
// Function to find turn around time for
// all processes
FindTurnAroundTime(proc, n, wt, tat);
// Display processes along with all
// details
printf("P\t\tBT\t\tWT\t\tTAT\t\t\n");
// Calculate total waiting time and
// total turnaround time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("%i", proc[i].pid);
printf("\t\t %i",proc[i].bt);
printf("\t\t %i",wt[i]);
printf("\t\t %i \n", tat[i]);
}
printf("\nAverage waiting time = %f", (float)total_wt / (float)n);
printf("\nAverage turn around time = %f",(float)total_tat / (float)n);
}
// Driver code
int main()
{
struct Process proc[5] = { { 1, 6, 2 }, { 2, 2, 5 },
{ 3, 8, 1 }, { 4, 3, 0}, {5, 4, 4} };
int n = sizeof(proc) / sizeof(proc[0]);
FindAvgTime(proc, n);
return 0;
}