Introduction
Generally, files are stored in secondary storage devices such as hard disks. So to manage the space of hard disks efficiently, the operating system uses file allocation methods.
The file system divides the file logically into multiple blocks. These blocks now need to be stored in a secondary storage device.
The hard disk contains multiple physical blocks in which the data reside. The file system will now decide where to store these blocks of a file in the sectors of the hard disk using file allocation methods.
In this article, we will study the Contiguous Allocation method in detail:
What is Contiguous Allocation?
The contiguous allocation method is the first and the most basic file allocation method. For understanding contiguous allocation, we will consider file A. This file A is divided into five blocks named B1, B2, B3, B4, and B5.
The secondary memory, i.e. the hard disk, is divided into disk blocks or sectors.
For example:
In contiguous allocation, the blocks of file A will be placed contiguously (one after another) in the memory. The operating system will find five empty contiguous blocks in the memory and allocate them to file A. For if the starting disk block of file A is 0 and there are five blocks for file A, then five disk blocks contiguous to 0 will be allocated to file A, i.e., disk block 0, disk block 1, disk block 2, disk block 3 and disk block 4.
Now you must be thinking that we have decided the disk blocks to be allotted to the blocks of the file but what if we want this file in future? How will the operating system find the file for us?
Well, the operating system maintains a directory in which it stores the name of the file, its starting block, and its length. All these values are initialized to nil. The operating system finds the required contiguous empty blocks in the hard disk and allots them to the file block as soon as a file is saved. The directory structure for file A will be-
Directory
Similarly, if we consider two more files, files B and C which are divided into four blocks. Let us assume the starting disk block for file B is 5 and for file C is 10. If we store these files on the hard disk using the contiguous allocation method, then these will be stored like-
File B will get the contiguous disk blocks 5, 6, 7, and 8. File C will get the contiguous disk blocks 10, 11, 12 and 13 as shown below:
Now, we will discuss the advantages and disadvantages of the contiguous file allocation method.
“Must Recommended Topic, Internal and External Fragmentation“
Advantages
- This method is easy to implement.
- There is less overhead.
- We get excellent read performance. We just need the starting disk block from the directory to find our file.
- Sequential access and random access to the file blocks are possible.
Disadvantages
- This method suffers external fragmentation. The disk space is broken into chunks since the files are allocated and deleted. In the above example, the hard disk has four empty disk blocks, blocks 9, 14, and 15. But these blocks are not contiguous, and hence the space is wasted.
- It may be challenging to grow a file. If we want to grow the size of file A, we can’t since the next immediate contiguous block to file A is disk block 5, which is allocated to file B.
You can also read about the Multilevel Feedback Queue Scheduling.