-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequential File Allocation.c
More file actions
50 lines (42 loc) · 1.41 KB
/
Sequential File Allocation.c
File metadata and controls
50 lines (42 loc) · 1.41 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
#include <stdio.h>
int main() {
int n, i, j, start[20], length[20], block[50], k, flag;
// Initialize all blocks as free (0 = free, 1 = allocated)
for (i = 0; i < 50; i++) {
block[i] = 0;
}
printf("Enter number of files: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter starting block and length of file %d: ", i + 1);
scanf("%d%d", &start[i], &length[i]);
flag = 0;
for (j = start[i]; j < start[i] + length[i]; j++) {
if (block[j] == 1) { // block already allocated
flag = 1;
break;
}
}
if (flag == 0) {
for (j = start[i]; j < start[i] + length[i]; j++) {
block[j] = 1; // allocate blocks
}
printf("File %d allocated successfully.\n", i + 1);
}
else {
printf("File %d cannot be allocated. Some blocks are already in use.\n", i + 1);
}
}
// Display allocation table
printf("\nAllocation Table:\n");
printf("File No.\tStart\tLength\tBlocks Allocated\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t%d\t", i + 1, start[i], length[i]);
for (j = start[i]; j < start[i] + length[i]; j++) {
if (block[j] == 1)
printf("%d ", j);
}
printf("\n");
}
return 0;
}