Linked file allocation program in C

In this article, we will discuss the linked file allocation program in C. But before discussing its implementation, we need to know about linked file allocation in C with its algorithm.

The method of non-contiguous memory allocation is known as linked file allocation. It contains the file in a series of randomly chosen memory blocks, with each block containing the address or reference to the memory block after it, much like a linked list. Each file keeps track of its first memory block within the directory, from which the remaining portions of the file may be accessed.

One operating system's file management method is called Linked File Allocation. Non-contiguous memory allocation includes the Linked File Allocation, Indexed File Allocation, and Contingent Memory Allocation procedures as its other methods.

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 is not subject to external fragmentation since it may store files in any available space if the amount of blocks is larger than or equal to the file size that must be stored.

Furthermore, in cases where we require additional space for a file or wish to add more data, the linked file allocation approach allocates a new empty block to store this data. As a result, it can handle the two issues that the contiguous allocation of memory faces.

Algorithm

STEP 1: Open the application.

STEP 2: Generate data on the number of files.

STEP 3: Give the files randomized places.

STEP 4: Determine whether or not the chosen site is available.

STEP 5: If the location is available, set the flag to 0. When a space is assigned, set the flag to 1.

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

STEP 7: Gather information on whether additional files must be saved.

STEP 8: If true, go to STEP 2.

STEP 9: End the program if not.

Program for implementation

Filename: FileAllocation.c

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.