-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16_SSTF_DiskScheduling.c
More file actions
74 lines (56 loc) · 1.34 KB
/
16_SSTF_DiskScheduling.c
File metadata and controls
74 lines (56 loc) · 1.34 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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<limits.h>
int process[50];
int head = 0;
int noOfProcess = 0;
int totalDistance = 0;
int completed = 0;
bool iscompleted[50];
int getProcess( ) {
int min = INT_MAX;
int id = -1;
for( int i = 0 ; i < noOfProcess ; i++ ) {
if( iscompleted[i] == false ) {
if( abs(head - process[i]) < min) {
min = abs(head - process[i]);
id = i;
}
}
}
return id;
}
void SSTF_Algo() {
int completed = 0;
int id = -1;
while( completed != noOfProcess ) {
id = getProcess( );
totalDistance = totalDistance + abs( head - process[id] );
head = process[id];
iscompleted[id] = true;
completed++;
}
}
void input() {
printf("\nEnter no. of Process : ");
scanf("%d" , &noOfProcess);
printf("\nEnter Process : ");
for( int i = 0 ; i < noOfProcess ; i++ ) {
scanf("%d" , &process[i]);
}
printf("\nEnter Head Position : ");
scanf("%d" , &head);
for( int i = 0 ; i < noOfProcess ; i++ ) {
iscompleted[i] = false;
}
}
void output() {
printf("\nTotal Distance covered : %d" , totalDistance);
}
int main ( ) {
input( );
SSTF_Algo( );
output( );
return 0;
}