-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcfs.c
110 lines (93 loc) · 1.83 KB
/
fcfs.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
#include <stdio.h>
#include <math.h>
void displayHeader(int max)
{
int i, count = 0;
printf("\n ");
for(i=0; i<=max; i++)
{
if(i == 0 || i%10 == 0 || i == max)
{
printf("%d",i);
if(i > 99)
count = 2;
else if(i > 9)
count = 1;
else
count = 0;
}
else
{
if(count == 2)
count--;
else if(count == 1)
count--;
else
printf(" ");
}
}
printf("\n<");
for(i=0; i<=max; i++)
{
printf("-");
}
printf(">\n");
}
void point(int p)
{
int i;
printf("\n ");
for(i=0; i<p; i++)
printf(" ");
printf("+ [%d]",p);
printf("\n\n");
}
void line(int max)
{
int i;
printf("|");
for(i=0; i<=max; i++)
printf("-");
printf("|\n");
}
int displayList(int head, int n, int string[n], int dir, int total, int max)
{
int i;
point(head);
line(max);
for(i=0; i<n; i++)
{
total = total + fabs(head - string[i]);
head = string[i];
point(string[i]);
line(max);
}
return total;
}
int main()
{
int n, i, max, dir, head, total = 0;
printf("\n\n\t\t\t\t\t\t\tFCFS\n\n");
printf("\n Enter the total number of disk blocks: ");
scanf("%d",&max);
printf("\n Enter the total number of requests: ");
scanf("%d",&n);
int string[n];
printf("\n Enter the disk request string (%d values)(range 0 - %d): ",n,max);
for(i=0; i<n; i++)
{
scanf("%d",&string[i]);
if(string[i] < 0)
{
printf("\n !-- Request cannot be negative --!\n");
return -1;
}
}
printf("\n Enter the current head position: ");
scanf("%d",&head);
printf("\n\n\t\t\t\t\t\tList of requests\n\n");
displayHeader(max);
total = displayList(head,n,string,dir,total,max);
printf("\n\n The total Head moment:- %d units\n\n",total);
return 0;
}