Indexed File Allocation Program in C and C++

Indexed File Allocation Program in C and C++ as well as the algorithm of the Index File Allocation program to help you understand better. 

 

What is Indexed File Allocation in the Operating System?

The Indexed File Allocation stores the file in the blocks of memory, each block of memory has an address and the address of every file block is maintained in a separate index block. These index blocks point the file allocation system to the memory blocks which actually contains the file.

 

The Indexed File Allocation is one of the File Allocation Methods in the Operating SystemThe Indexed File Allocation comes under Non-contiguous memory allocation, the other method in the Non-contiguous memory allocation is the Linked File Allocation, and the last is the Contiguous Memory Allocation.

 

Why do we use the Indexed File Allocation method in the operating system?

The Linked File Allocation solves the problem of external fragmentation and file growth problems that were faced in the Contiguous Memory Allocation. But the Linked File Allocation cannot support the direct access capability that the contiguous memory allocation supported and this problem is solved by the Indexed File Allocation. The Indexed File Allocation can support direct access capability which speeds up the search of file blocks and it does not suffer from external fragmentation as the contiguous memory allocation and hence an efficient use of memory spaces occurs. 

 

Indexed File Allocation Program Algorithm:

STEP 1: Start the program.

STEP 2: Get information about the number of files.

STEP 3: Get the memory requirement of each file.

STEP 4: Allocate the memory to the file by selecting random locations. 

STEP 5: Check if the location that is selected is free or not.

STEP 6: If the location is allocated set the flag = 1, and if free set flag = 0.

STEP 7: Print the file number, length, and the block allocated.

STEP 8: Gather information if more files have to be stored.

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

STEP 10: If no, Stop the program.

 

Indexed File Allocation Program in C:

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

int files[50], indexBlock[50], indBlock, n;
void recurse1();
void recurse2();

void recurse1(){
    printf("Enter the index block: ");
    scanf("%d", &indBlock);
    if (files[indBlock] != 1){
        printf("Enter the number of blocks and the number of files needed for the index %d on the disk: ", indBlock);
        scanf("%d", &n);
    }
    else{
        printf("%d is already allocated\n", indBlock);
        recurse1();
    }
    recurse2();
}

void recurse2(){
    int ch;
    int flag = 0;
    for (int i=0; i<n; i++){
        scanf("%d", &indexBlock[i]);
        if (files[indexBlock[i]] == 0)
            flag++;
    }
    if (flag == n){
        for (int j=0; j<n; j++){
            files[indexBlock[j]] = 1;
        }
        printf("Allocated\n");
        printf("File Indexed\n");
        for (int k=0; k<n; k++){
            printf("%d ------> %d : %d\n", indBlock, indexBlock[k], files[indexBlock[k]]);
        }
    }
    else{
        printf("File in the index is already allocated\n");
        printf("Enter another indexed file\n");
        recurse2();
    }
    printf("Do you want to enter more files?\n");
    printf("Enter 1 for Yes, Enter 0 for No: ");
    scanf("%d", &ch);
    if (ch == 1)
        recurse1();
    else
        exit(0);
    return;
}

int main()
{
    for(int i=0;i<50;i++)
        files[i]=0;

    recurse1();
    return 0;
}

 

Indexed File Allocation Program in C++:

#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

int files[50], indexBlock[50], indBlock, n;
void recurse1();
void recurse2();

void recurse1(){
    cout << "Enter the index block: ";
    cin >> indBlock;
    if (files[indBlock] != 1){
        cout << "Enter the number of blocks and the number of files needed for the index " << indBlock << " on the disk: ";
        cin >> n;
    }
    else{
        cout << indBlock << " is already allocated" << endl;
        recurse1();
    }
    recurse2();
}

void recurse2(){
    int flag = 0;
    for (int i=0; i<n; i++){
        cin >> indexBlock[i];
        if (files[indexBlock[i]] == 0)
            flag++;
    }
    if (flag == n){
        for (int j=0; j<n; j++){
            files[indexBlock[j]] = 1;
        }
        cout << "Allocated" << endl;
        cout << "File Indexed" << endl;
        for (int k=0; k<n; k++){
            cout << indBlock << " ------> " << indexBlock[k] << ": " << files[indexBlock[k]] << endl;
        }
    }
    else{
        cout << "File in the index is already allocated" << endl;
        cout << "Enter another indexed file" << endl;
        recurse2();
    }
    cout << "Do you want to enter more files?" << endl;
    cout << "Enter 1 for Yes, Enter 0 for No: ";
    int ch;
    cin >> ch;
    if (ch == 1)
        recurse1();
    else
        exit(0);
    return;
}

int main()
{
    for(int i=0;i<50;i++)
        files[i]=0;

    recurse1();
    return 0;
}

 

 

The output of the Indexed File Allocation Program in C and C++:

Indexed File Allocation Program in C and C++ Output

 

The Indexed File Allocation program provides you faster access to the memory blocks but it has to devote a complete memory block to store the pointers of other blocks. The Linked File Allocation program in C and C++ is capable of removing the external fragmentation which was faced by the Sequential File Allocation Program in C and C++. But the Linked File Allocation does not support the direct access capability which is supported by the Indexed and Sequential file which makes the memory access faster.

Leave a Comment