File Allocation Strategies Simulation in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BLOCKS 10
// Define the structure for Sequential Allocation
typedef struct {
int startBlock;
int totalBlocks;
} Sequential;
// Define the structure for Indexed Allocation
typedef struct {
int *indexBlocks;
int numIndexBlocks;
} Indexed;
// Define the structure for Linked Allocation
typedef struct {
int *nextBlock;
int totalBlocks;
} Linked;
// Function to display the Sequential Allocation strategy
void sequentialAllocation(Sequential *file) {
printf("Sequential Allocation:\n");
printf("Start Block: %d, Total Blocks: %d\n", file->startBlock, file->totalBlocks);
// Function to display the Indexed Allocation strategy
void indexedAllocation(Indexed *file) {
printf("Indexed Allocation:\n");
printf("Index Blocks: ");
for (int i = 0; i < file->numIndexBlocks; i++) {
printf("%d ", file->indexBlocks[i]);
printf("\n");
// Function to display the Linked Allocation strategy
void linkedAllocation(Linked *file) {
printf("Linked Allocation:\n");
printf("Block Pointers: ");
for (int i = 0; i < file->totalBlocks; i++) {
printf("%d ", file->nextBlock[i]);
printf("\n");
}
// Simulate file allocation with sequential allocation strategy
void allocateSequential(Sequential *file) {
printf("Enter the start block and the total number of blocks for sequential allocation:\n");
scanf("%d %d", &file->startBlock, &file->totalBlocks);
sequentialAllocation(file);
// Simulate file allocation with indexed allocation strategy
void allocateIndexed(Indexed *file) {
int numBlocks;
printf("Enter the number of index blocks for indexed allocation:\n");
scanf("%d", &numBlocks);
file->indexBlocks = (int *)malloc(numBlocks * sizeof(int));
file->numIndexBlocks = numBlocks;
printf("Enter the index blocks:\n");
for (int i = 0; i < numBlocks; i++) {
scanf("%d", &file->indexBlocks[i]);
indexedAllocation(file);
// Simulate file allocation with linked allocation strategy
void allocateLinked(Linked *file) {
int totalBlocks;
printf("Enter the total number of blocks for linked allocation:\n");
scanf("%d", &totalBlocks);
file->nextBlock = (int *)malloc(totalBlocks * sizeof(int));
file->totalBlocks = totalBlocks;
printf("Enter the block pointers:\n");
for (int i = 0; i < totalBlocks; i++) {
scanf("%d", &file->nextBlock[i]);
linkedAllocation(file);
int main() {
int choice;
Sequential sequentialFile;
Indexed indexedFile;
Linked linkedFile;
while (1) {
printf("\nFile Allocation Strategies Simulation\n");
printf("1. Sequential Allocation\n");
printf("2. Indexed Allocation\n");
printf("3. Linked Allocation\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
allocateSequential(&sequentialFile);
break;
case 2:
allocateIndexed(&indexedFile);
break;
case 3:
allocateLinked(&linkedFile);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
return 0;