Linked File Allocation Program In C

One technique used in file management by an operating system is referred to as Linked File Allocation. Linked File Allocation is part of non-contiguous memory allocation, which also encompasses Indexed File Allocation and Contiguous Memory Allocation as alternative approaches.

Why does the operating system implement the Linked File Allocation method?

A non-contiguous allocation of memory strategy is called linked file allocation . When allocating contiguous memory, external fragmentation and directories are two issues that cannot be developed after being declared. The Linked File Allocation approach addresses these issues.

The Linked File Allocation method avoids external fragmentation by allowing files to be saved in any free space when the number of blocks is sufficient to accommodate the file size.

Moreover, when there is a need for extra storage space to accommodate a file or when there is a desire to append more information, the linked file allocation method assigns a fresh vacant block to hold this additional data. This capability allows it to effectively address the challenges encountered by the contiguous memory allocation technique.

Algorithm

STEP 1: Open the application.

STEP 2: Generate data on the number of files.

STEP 3: Give the files randomized places.

STEP 4: Check the availability of the selected website.

When the position is accessible, change the flag value to 0. Once a location is allocated, update the flag to 1.

STEP 6: Display the name of the file, its size, and the assigned block.

STEP 7: Collect data to determine if there is a need to save any extra files.

STEP 8: If true, go to STEP 2 .

STEP 9: End the program if not.

Program for implementation

Filename: FileAllocation.c

Example

#include <stdio.h>

#include <stdlib.h>

void recursiveParts(int pagesAllocation[]){

    int s, length, k, c, j;

    printf("Enter the beginning block's index as well as length: ");

    scanf("%d%d", &s, &length);

    k = length;

    if (pagesAllocation[s] == 0){

        for (j = s; j < (s + k); j++){

            if (pagesAllocation[j] == 0){

                pagesAllocation[j] = 1;

                printf("%d------>%d\n", j, pagesAllocation[j]);

            }

            else {

                printf("The block %d has already been allocated \n", j);

                k++;

            }

        }

    }

    else

        printf("The block %d has already been allocated \n", s);

    printf("Do you want to add more files? \n");

    printf("Enter 1 for continue, Enter 0 for No: ");

    scanf("%d", &c);

    if (c==1)

        recursiveParts(pagesAllocation);

    else

        exit(0);

    return;

}



int main(){

    int pagesAllocation[50], p1, a1;

    for (int i = 0; i < 50; i++)

        pagesAllocation[i] = 0;

    printf("Enter the quantity of provided blocks: ");

    scanf("%d", &p1);

    printf("Enter the number of alloted blocks ");

    for (int i = 0; i < p1; i++){

        scanf("%d", &a1);

        pagesAllocation[a1] = 1;

    }



    recursiveParts(pagesAllocation);



    return 0;

}

Output:

Output

Enter the quantity of provided blocks: 3

Enter the number of alloted blocks 4

2

4

Enter the beginning block's index as well as length: 5

6

5------>1

6------>1

7------>1

8------>1

9------>1

10------>1

Do you want to add more files? 

Enter 1 for continue, Enter 0 for No: 1

Enter the beginning block's index as well as length: 7 2

The block 7 has already been allocated 

Do you want to add more files? 

Enter 1 for continue, Enter 0 for No: 0

Explanation

  • In this example, the program constructs an array pagesAllocation[50] to represent the accessible memory blocks. All blocks are initially set to 0 , indicating that they are unallocated.
  • The program requires the user to enter the number of already assigned blocks (p1) , followed by the indices of these allotted blocks. It indicates these allocated blocks by changing their pagesAllocation array values to 1 .
  • The recursiveParts function is responsible for allocating extra memory blocks. It accepts an array pagesAllocation as input and performs the following steps:
  • It requests to allocate the user's starting block index (s) and the memory block ( length ) measurement.
  • It determines whether the beginning block (s) has already been assigned. If not, it begins allocating memory blocks from s .
  • If the starting block has not previously been assigned, it iterates across length blocks beginning with s . It examines each block to see if it has already been assigned. If not, it allocates it by changing pagesAllocation[j] to 1 and generating an allocation notification. If a block has already been allocated, it produces a message noting this and increases k (the allocation duration) to prevent overwriting allocated blocks.
  • In the main function:
  • It starts the pagesAllocation array with every block set to 0 .
  • It prompts the user to provide the number of blocks previously allocated (p1) and the indexes of those blocks. It designates these blocks as reallocated by setting pagesAllocation[a1] to 1.
  • After that, it invokes the recursiveParts method to allow users to set up additional memory blocks as required.
  • The program continues until the user chooses not to allocate additional files by typing 0 .

Input Required

This code uses input(). Please provide values below: