Linked File Allocation Program in C and C++ 

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

 

What is Linked File Allocation in the Operating System?

Linked File Allocation is a Non-contiguous memory allocation method where the file is stored in random memory blocks and each block contains the pointer (or address) of the next memory block as in a linked list. The starting memory block of each file is maintained in a directory and the rest of the file can be traced from that starting block.

 

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

 

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

The Linked File Allocation is a non-contiguous memory allocation method. In the Contiguous memory allocation, we have faced two problems – external fragmentation and files cannot be extended once declared. These problems are dealt with in the Linked File Allocation method. 

As the Linked File Allocation can store at any random space if the size of the block is greater or equal to the size of the file to be stored, and hence no external fragmentation occurs in the Linked File Allocation. 

Also, if we have to add more information to the file or extend the file then linked file allocation will allot it an empty block to store the data and the address of this block will be stored into the previous block. Therefore it can deal with both the problems faced by the contiguous memory allocation.

 

Linked File Allocation Program Algorithm:

STEP 1: Start the program.

STEP 2: Gather information about the number of files.

STEP 3: Allocate random locations to the files.

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

STEP 5: If the location is free set the flag=0 a location is allocated set the flag = 1.

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

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

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

STEP 9: If no, Stop the program.

 

Linked File Allocation Program in C:

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

void recursivePart(int pages[]){
    int st, len, k, c, j;
    printf("Enter the index of the starting block and its length: ");
    scanf("%d%d", &st, &len);
    k = len;
    if (pages[st] == 0){
        for (j = st; j < (st + k); j++){
            if (pages[j] == 0){
                pages[j] = 1;
                printf("%d------>%d\n", j, pages[j]);
            }
            else {
                printf("The block %d is already allocated \n", j);
                k++;
            }
        }
    }
    else
        printf("The block %d is already allocated \n", st);
    printf("Do you want to enter more files? \n");
    printf("Enter 1 for Yes, Enter 0 for No: ");
    scanf("%d", &c);
    if (c==1)
        recursivePart(pages);
    else
        exit(0);
    return;
}

int main(){
    int pages[50], p, a;

    for (int i = 0; i < 50; i++)
        pages[i] = 0;
    printf("Enter the number of blocks already allocated: ");
    scanf("%d", &p);
    printf("Enter the blocks already allocated: ");
    for (int i = 0; i < p; i++){
        scanf("%d", &a);
        pages[a] = 1;
    }

    recursivePart(pages);
    getch();
    return 0;
}

 

 

Linked File Allocation Program in C++:

#include <iostream>
#include <conio.h>
using namespace std;

void recursivePart(int pages[]){
    int st, len;
    cout << "Enter the index of the starting block and its length: ";
    cin >> st >> len;
    int k = len;
    if (pages[st] == 0){
        for (int j = st; j < (st + k); j++){
            if (pages[j] == 0){
                pages[j] = 1;
                cout << j << "------>" << pages[j] << endl;
            }
            else {
                cout << "The block "<< j << " is already allocated" << endl;
                k++;
            }
        }
    }
    else
        cout <<"The block " << st << " is already allocated" << endl;
    cout << "Do you want to enter more files?" << endl;
    cout << "Enter 1 for Yes, Enter 0 for No: ";
    int c;
    cin >> c;
    if (c==1)
        recursivePart(pages);
    else
        exit(0);
    return;
}

int main(){
    int pages[50], p, a;

    for (int i = 0; i < 50; i++)
        pages[i] = 0;
    cout << "Enter the number of blocks already allocated: ";
    cin >> p;
    cout << "Enter the blocks already allocated: ";
    for (int i = 0; i < p; i++){
        cin >> a;
        pages[a] = 1;
    }

    recursivePart(pages);
    getch();
    return 0;
}

 

 

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

Linked File Allocation Program in C and C++ Output

 

The Linked File Allocation program provides efficient use of memory by avoiding external fragmentation. But the Linked Allocation Program does not have the direct access method as the Indexed File Allocation Program in C and C++ has, also see how the Sequential File Allocation Program in C and C++ is different from the rest of File Allocation Methods.

 

1 thought on “Linked File Allocation Program in C and C++ ”

  1. Thanks for another great post. Where else could anyone get that type of info in such an ideal way of writing? I have a presentation next week, and I’m on the look for such info.

    Reply

Leave a Comment