The Indexed File Allocation method stores files in memory blocks, where each block is assigned a specific address. Each block's address within a file is stored in a dedicated index block. These index blocks are used to locate the memory blocks that hold the file in the file allocation system.
One of the approaches to file allocation in the operating system is the indexing file allocation method. The concept of "non-contiguous memory allocation" encompasses three distinct techniques: the Linked File Allocation, the Indexed File Allocation, and the Contiguous Memory Allocation.
Why do we utilize the operating system's Indexed File Allocation method?
The Linked File Allocation method effectively addresses the challenges of external fragmentation and file growth that were present in Contiguous Memory Allocation. Unlike Contiguous Memory Allocation, Linked File Allocation does not support direct access. To overcome this limitation, Indexed File Allocation is introduced. This allocation strategy optimizes memory usage by eliminating external fragmentation, similar to Contiguous Memory Allocation, while also enabling direct access. Additionally, it enhances the efficiency of locating file blocks during searches.
Algorithm for the Indexed File Allocation Program:
STEP 1: Run the application.
STEP 2: To learn how many files there are.
STEP 3: Get each file's memory requirements
STEP 4: Randomly select locations to assign the RAM to the file.
STEP 5: Check to see if the chosen venue is free.
Set the flag to 1 when the space is allocated, and to 0 when it is vacant.
STEP 7: Display the filename, size, and assigned block.
STEP 8: In case there is a necessity to retain additional files, proceed with collecting the required data.
STEP 9: If yes, then go to STEP 2.
STEP 10: If no, Stop the program.
Indexed File Allocation Program in C:
Let's consider a program to execute the indexed file allocation method in the C programming language.
#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