-
Notifications
You must be signed in to change notification settings - Fork 1
/
fitcheck.c
129 lines (125 loc) · 3.47 KB
/
fitcheck.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
#include <stdio.h>
void firstfit(int blocksize[], int m, int processsize[], int n)
{
int allocation[n];
int blocksizecopy[m];
for (int i = 0; i < m; i++)
blocksizecopy[i] = blocksize[i];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (blocksizecopy[j] >= processsize[i])
{
allocation[i] = j;
blocksizecopy[j] -= processsize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf(" %d \t\t %d \t\t", i + 1, processsize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
void bestfit(int blocksize[], int m, int processsize[], int n)
{
int allocation[n];
int blocksizecopy[m];
for (int i = 0; i < m; i++)
blocksizecopy[i] = blocksize[i];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++)
{
int best = -1;
for (int j = 0; j < m; j++)
{
if (blocksizecopy[j] >= processsize[i])
{
if (best == -1)
best = j;
else if (blocksizecopy[best] > blocksizecopy[j])
best = j;
}
}
if (best != -1)
{
allocation[i] = best;
blocksizecopy[best] -= processsize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf(" %d \t\t %d \t\t", i + 1, processsize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
void worstfit(int blocksize[], int m, int processsize[], int n)
{
int allocation[n];
int blocksizecopy[m];
for (int i = 0; i < m; i++)
blocksizecopy[i] = blocksize[i];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++)
{
int worst = -1;
for (int j = 0; j < m; j++)
{
if (blocksizecopy[j] >= processsize[i])
{
if (worst == -1)
worst = j;
else if (blocksizecopy[worst] < blocksizecopy[j])
worst = j;
}
}
if (worst != -1)
{
allocation[i] = worst;
blocksizecopy[worst] -= processsize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf(" %d \t\t %d \t\t", i + 1, processsize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int blocksize[10], processsize[10], m, n;
printf("Enter no. of blocks: ");
scanf("%d", &m);
printf("Enter size of each block: ");
for (int i = 0; i < m; i++)
scanf("%d", &blocksize[i]);
printf("Enter no. of processes: ");
scanf("%d", &n);
printf("Enter size of each process: ");
for (int i = 0; i < n; i++)
scanf("%d", &processsize[i]);
printf("\nFirst Fit\n");
firstfit(blocksize, m, processsize, n);
printf("\nBest Fit\n");
bestfit(blocksize, m, processsize, n);
printf("\nWorst Fit\n");
worstfit(blocksize, m, processsize, n);
return 0;
}