0% found this document useful (0 votes)
9 views5 pages

Indexed File Allocation

The document presents a C program for managing indexed file allocation on a disk, allowing users to create, delete, and display files along with their allocated blocks. It utilizes a bit vector to track free and allocated blocks and maintains a directory of files with their respective block allocations. The program includes functions for initializing the bit vector, finding free blocks, and managing user interactions through a menu-driven interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Indexed File Allocation

The document presents a C program for managing indexed file allocation on a disk, allowing users to create, delete, and display files along with their allocated blocks. It utilizes a bit vector to track free and allocated blocks and maintains a directory of files with their respective block allocations. The program includes functions for initializing the bit vector, finding free blocks, and managing user interactions through a menu-driven interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Indexed file allocation

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_FILES 10
#define MAX_BLOCKS 100 // Maximum number of blocks in the disk

typedef struct {
char filename[20];
int numBlocks;
int* blockIndexes; // Array to hold indexes of blocks allocated to this file
int indexBlock; // Index block to store the starting block for the file's data blocks
} DirectoryEntry;

int bitVector[MAX_BLOCKS]; // Bit Vector to track free (0) or allocated (1) blocks
DirectoryEntry directory[MAX_FILES]; // Directory to store file details
int fileCount = 0;

// Function to initialize bit vector with random 0's and 1's


void initializeBitVector(int n) {
for (int i = 0; i < n; i++) {
bitVector[i] = rand() % 2; // Initialize randomly to 0 or 1
}
}

// Function to display the bit vector (Free/Allocated blocks)


void showBitVector(int n) {
for (int i = 0; i < n; i++) {
printf("%d", bitVector[i]);
}
printf("\n");
}

// Function to display the directory (List of files and their block allocations)
void showDirectory() {
if (fileCount == 0) {
printf("No files in the directory.\n");
} else {
printf("Directory List:\n");
printf("Filename\tNumber of Blocks\tIndex Block\tAllocated Blocks\n");
for (int i = 0; i < fileCount; i++) {
printf("%s\t\t%d\t\t%d\t\t", directory[i].filename, directory[i].numBlocks,
directory[i].indexBlock);
for (int j = 0; j < directory[i].numBlocks; j++) {
printf("%d ", directory[i].blockIndexes[j]);
}
printf("\n");
}
}
}

// Function to find free blocks for the file (Non-contiguous allocation)


int findFreeBlocks(int n, int numBlocks) {
int freeBlocksFound = 0; // This will track how many free blocks we've found
for (int i = 0; i < n; i++) {
if (bitVector[i] == 0) { // Block is free
freeBlocksFound++;
if (freeBlocksFound == numBlocks) {
return 0; // Return 0 when sufficient free blocks are found
}
}
}
return -1; // Not enough free blocks
}

// Function to allocate non-contiguous free blocks to a new file


void createNewFile(int n) {
if (fileCount >= MAX_FILES) {
printf("Directory is full. Cannot add more files.\n");
return;
}

char filename[20];
int numBlocks;

printf("Enter filename: ");


scanf("%s", filename);
printf("Enter number of blocks to allocate: ");
scanf("%d", &numBlocks);

if (findFreeBlocks(n, numBlocks) == -1) {


printf("Not enough free blocks available.\n");
return;
}

// Allocate blocks and update bit vector


directory[fileCount].numBlocks = numBlocks;
strcpy(directory[fileCount].filename, filename);
directory[fileCount].blockIndexes = (int*)malloc(numBlocks * sizeof(int));

int blocksAllocated = 0;
for (int i = 0; i < n && blocksAllocated < numBlocks; i++) {
if (bitVector[i] == 0) { // If the block is free
bitVector[i] = 1; // Mark the block as allocated
directory[fileCount].blockIndexes[blocksAllocated++] = i; // Store the block index
}
}

// Set the index block (the first allocated block will be the index block)
directory[fileCount].indexBlock = directory[fileCount].blockIndexes[0];

fileCount++;
printf("File '%s' created successfully with %d blocks.\n", filename, numBlocks);
}

// Function to delete a file and free its allocated blocks


void deleteFile() {
if (fileCount == 0) {
printf("No files to delete.\n");
return;
}

char filename[20];
printf("Enter filename to delete: ");
scanf("%s", filename);

int index = -1;


for (int i = 0; i < fileCount; i++) {
if (strcmp(directory[i].filename, filename) == 0) {
index = i;
break;
}
}

if (index == -1) {
printf("File not found in the directory.\n");
return;
}

// Release the blocks allocated to the file


for (int i = 0; i < directory[index].numBlocks; i++) {
bitVector[directory[index].blockIndexes[i]] = 0; // Mark the blocks as free
}

// Remove the directory entry by shifting remaining files


for (int i = index; i < fileCount - 1; i++) {
directory[i] = directory[i + 1];
}

fileCount--;
printf("File '%s' deleted successfully.\n", filename);
}
// Main function to drive the program with a menu
int main() {
int n;
printf("Enter the number of blocks on disk (max %d): ", MAX_BLOCKS);
scanf("%d", &n);

if (n > MAX_BLOCKS) {
printf("Exceeded maximum block size. Exiting program.\n");
return 1;
}

initializeBitVector(n);

int choice;
while (1) {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Delete File\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory();
break;
case 4:
deleteFile();
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy