Indexed File Allocation Program in C

In this article, we will discuss the indexed file allocation program in C.

What does the operating system mean by "Indexed File Allocation"?

The Indexed File Allocation keeps the file in memory blocks; each memory block has an address, and each file block's address is kept in a separate index block. The file allocation system is directed to the memory blocks containing the file by these index blocks.

One of the file allocation methods in the operating system is the indexing file allocation. The term "non-contiguous memory allocation" refers to three different methods: 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 external fragmentation and file growth issues that the Contiguous Memory Allocation had are resolved by the Linked File Allocation. However, the contiguous memory allocation permitted direct access, which the linked file allocation is unable to support. The Indexed File Allocation fixes this issue. It makes optimal use of memory spaces because it does not suffer from external fragmentation like contiguous memory allocation and may enable direct access. It also speeds up the search of file blocks.

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: Choose random locations to allocate the RAM to the file.

STEP 5: Check to see if the chosen venue is free.

STEP 6: Set the flag to 1 if the space is reserved, and to 0 if it is available.

STEP 7: Print the file name, size, and allocated block.

STEP 8: If more files need to be kept, to gather information.

STEP 9: If yes, then go to STEP 2.

STEP 10: If no, Stop the program.

Indexed File Allocation Program in C:

Let's take a program to implement the indexed file allocation in C.

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