Contiguous file allocation
struct file {
char name[50];
int size;
int start_block;
};
struct disk {
int total_blocks;
int *used_blocks;
int *free_blocks;
};
void init_disk(struct disk *d, int total_blocks) {
d->total_blocks = total_blocks;
d->used_blocks = (int *)calloc(total_blocks, sizeof(int));
d->free_blocks = (int *)calloc(total_blocks, sizeof(int));
for (int i = 0; i<total_blocks; i++) {
d->free_blocks[i] = 1;
}
}
struct file *allocate_file(struct disk *d, char *name, int size) {
int start_block = -1;
int contiguous_blocks = 0;
for (int i = 0; i< d->total_blocks; i++) {
if (d->free_blocks[i] == 1) {
if (start_block == -1) {
start_block = i;
}
contiguous_blocks++;
if (contiguous_blocks == size) {
break;
}
} else {
start_block = -1;
contiguous_blocks = 0;
}
}
if (contiguous_blocks != size) {
return NULL;
}
for (int i = start_block; i<start_block + size; i++) {
d->used_blocks[i] = 1;
d->free_blocks[i] = 0;
}
struct file *f = (struct file *)malloc(sizeof(struct file));
strcpy(f->name, name);
f->size = size;
f->start_block = start_block;
return f;
void free_file(struct disk *d, struct file *f)
{
// mark all the blocks, as free
or (int i = f->start_block; i< f->start_block + f->size; i++) {
d->used_blocks[i] = 0 ;
d->free_blocks[i] = 1 ;
}
free(f);
}
int main() {
// initialize the disk with struct
struct disk d;
init_disk(&d, 10);
// allocate space for a respective file
struct file *f1 = allocate_file(&d, "file1", 3);
if (f1 == NULL) {
printf("Could not allocate space for file1\n");
exit(1);
}
// allocate space for other file
struct file *f2 = allocate_file(&d, "file2", 5);
if (f2 == NULL) {
printf("Could not allocate space for file2\n");
exit(1);
}
// free the 1st file
free_file(&d, f1);
// allocate space for a 3rd file
struct file *f3 = allocate_file(&d, "file3", 4);
if (f3 == NULL) {
printf("Could not allocate space for file3\n");
exit(1);
}
// free the 2nd file
free_file(&d, f2);
// free the 3rd file
free_file(&d, f3);
return 0;
}
Indexed file allocation
#include <stdio.h>
#include <stdlib.h>
#define MAX_BLOCKS 100
int files[MAX_BLOCKS];
int blockSize[MAX_BLOCKS];
int numFiles = 0;
void initialize() {
for (int i = 0; i < MAX_BLOCKS; i++) {
files[i] = -1; // Initialize all blocks as empty (-1 represents empty)
}
}
void allocateFile(int fileNumber, int size) {
if (numFiles >= MAX_BLOCKS) {
printf("Disk is full. Cannot allocate more files.\n");
return;
}
if (size <= 0 || size > MAX_BLOCKS) {
printf("Invalid file size.\n");
return;
}
int startBlock = -1;
int consecutiveBlocks = 0;
for (int i = 0; i < MAX_BLOCKS; i++) {
if (files[i] == -1) {
if (consecutiveBlocks == 0) {
startBlock = i;
}
consecutiveBlocks++;
} else {
consecutiveBlocks = 0;
startBlock = -1;
}
if (consecutiveBlocks == size) {
break;
}
}
if (consecutiveBlocks == size) {
for (int i = startBlock; i < startBlock + size; i++) {
files[i] = fileNumber;
blockSize[i] = size;
}
numFiles++;
printf("File %d allocated starting from block %d\n", fileNumber, startBlock);
} else {
printf("Not enough consecutive free blocks to allocate the file.\n");
}
}
void deallocateFile(int fileNumber) {
int blocksFreed = 0;
for (int i = 0; i < MAX_BLOCKS; i++) {
if (files[i] == fileNumber) {
files[i] = -1;
blocksFreed++;
}
}
if (blocksFreed > 0) {
numFiles--;
printf("File %d deallocated. %d blocks freed.\n", fileNumber, blocksFreed);
} else {
printf("File %d not found on the disk.\n", fileNumber);
}
}
void displayDiskStatus() {
printf("\nDisk Status:\n");
for (int i = 0; i < MAX_BLOCKS; i++) {
if (files[i] != -1) {
printf("Block %d: File %d (Size: %d blocks)\n", i, files[i], blockSize[i]);
}
}
}
int main() {
initialize();
while (1) {
printf("\nFile Allocation Menu:\n");
printf("1. Allocate a File\n");
printf("2. Deallocate a File\n");
printf("3. Display Disk Status\n");
printf("4. Exit\n");
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (numFiles >= MAX_BLOCKS) {
printf("Disk is full. Cannot allocate more files.\n");
} else {
int fileNumber, size;
printf("Enter File Number and Size: ");
scanf("%d %d", &fileNumber, &size);
allocateFile(fileNumber, size);
}
break;
case 2:
if (numFiles <= 0) {
printf("No files to deallocate.\n");
} else {
int fileNumber;
printf("Enter File Number to deallocate: ");
scanf("%d", &fileNumber);
deallocateFile(fileNumber);
}
break;
case 3:
displayDiskStatus();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
Runtime cases:
File Allocation Menu:
1. Allocate a File
2. Deallocate a File
3. Display Disk Status
4. Exit
Enter your choice: 1
Enter File Number and Size: 1 5
File 1 allocated starting from block 0
File Allocation Menu:
1. Allocate a File
2. Deallocate a File
3. Display Disk Status
4. Exit
Enter your choice: 1
Enter File Number and Size: 2 3
File 2 allocated starting from block 5
File Allocation Menu:
1. Allocate a File
2. Deallocate a File
3. Display Disk Status
4. Exit
Enter your choice: 3
Disk Status:
Block 0: File 1 (Size: 5 blocks)
Block 5: File 2 (Size: 3 blocks)
File Allocation Menu:
1. Allocate a File
2. Deallocate a File
3. Display Disk Status
4. Exit
Enter your choice: 2
Enter File Number to deallocate: 1
File 1 deallocated. 5 blocks freed.
File Allocation Menu:
1. Allocate a File
2. Deallocate a File
3. Display Disk Status
4. Exit
Enter your choice: 3
Disk Status:
Block 5: File 2 (Size: 3 blocks)
File Allocation Menu:
1. Allocate a File
2. Deallocate a File
3. Display Disk Status
4. Exit
Enter your choice: 4
Linked File allocation
#include <stdio.h>
#include <stdlib.h>
void recursiveParts(int pagesAllocation[]){
int s, length, k, c, j;
printf("Enter the beginning block's index as well as length: ");
scanf("%d%d", &s, &length);
k = length;
if (pagesAllocation[s] == 0){
for (j = s; j < (s + k); j++){
if (pagesAllocation[j] == 0){
pagesAllocation[j] = 1;
printf("%d------>%d\n", j, pagesAllocation[j]);
}
else {
printf("The block %d has already been allocated \n", j);
k++;
}
}
}
else
printf("The block %d has already been allocated \n", s);
printf("Do you want to add more files? \n");
printf("Enter 1 for continue, Enter 0 for No: ");
scanf("%d", &c);
if (c==1)
recursiveParts(pagesAllocation);
else
exit(0);
return;
}
int main(){
int pagesAllocation[50], p1, a1;
for (int i = 0; i < 50; i++)
pagesAllocation[i] = 0;
printf("Enter the quantity of provided blocks: ");
scanf("%d", &p1);
printf("Enter the number of alloted blocks ");
for (int i = 0; i < p1; i++){
scanf("%d", &a1);
pagesAllocation[a1] = 1;
}
recursiveParts(pagesAllocation);
return 0;
}
Enter the quantity of provided blocks: 3
Enter the number of alloted blocks 4
2
4
Enter the beginning block's index as well as length: 5
6
5------>1
6------>1
7------>1
8------>1
9------>1
10------>1
Do you want to add more files?
Enter 1 for continue, Enter 0 for No: 1
Enter the beginning block's index as well as length: 7 2
The block 7 has already been allocated
Do you want to add more files?
Enter 1 for continue, Enter 0 for No: 0
Explanation
o In this example, the program constructs an array pagesAllocation[50] to
represent the accessible memory blocks. All blocks are initially set to 0,
indicating that they are unallocated.
o The program requires the user to enter the number of already assigned
blocks (p1), followed by the indices of these allotted blocks. It indicates these
allocated blocks by changing their pagesAllocation array values to 1.
o The recursiveParts function is responsible for allocating extra memory
blocks. It accepts an array pagesAllocation as input and performs the
following steps:
o It requests to allocate the user's starting block index (s) and the memory
block (length) measurement.
o It determines whether the beginning block (s) has already been assigned. If
not, it begins allocating memory blocks from s.
o If the starting block has not previously been assigned, it iterates across length
blocks beginning with s. It examines each block to see if it has already been
assigned. If not, it allocates it by changing pagesAllocation[j] to 1 and
generating an allocation notification. If a block has already been allocated, it
produces a message noting this and increases k (the allocation duration) to
prevent overwriting allocated blocks.
o In the main function:
o It starts the pagesAllocation array with every block set to 0.
o It prompts the user to provide the number of blocks previously
allocated (p1) and the indexes of those blocks. It designates these blocks as
reallocated by setting pagesAllocation[a1] to 1.
o After that, it invokes the recursiveParts method to allow users to set up
additional memory blocks as required.
o The program continues until the user chooses not to allocate additional files
by typing 0.