Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Contiguous Allocation?
2.1.
Advantages
2.2.
Disadvantages
3.
Frequently Asked Questions
3.1.
What are the different methods of file allocation in the operating system?
3.2.
What is the difference between contiguous allocation and non-contiguous file allocation methods?
3.3.
What is the fragmentation in OS?
4.
Conclusion
Last Updated: Mar 27, 2024

Contiguous Allocation

Author Pakhi Garg
0 upvote
Operating Systems

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.

File Allocation ethods

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.

Contiguous Allocation

 

The secondary memory, i.e. the hard disk, is divided into disk blocks or sectors. 

For example: 

Illustration Image

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. 

Illustration Image

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

Illustration Image

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: 

Illustration Image

Now, we will discuss the advantages and disadvantages of the contiguous file allocation method.

“Must Recommended Topic, Internal and External Fragmentation“

Advantages

  1. This method is easy to implement.
  2. There is less overhead.
  3. We get excellent read performance. We just need the starting disk block from the directory to find our file.
  4. Sequential access and random access to the file blocks are possible.

Disadvantages

  1. 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.
  2. 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.

Frequently Asked Questions

What are the different methods of file allocation in the operating system?

Ans. There are two methods of file allocation in the operating system. These are-

  1. Contiguous allocation
  2. Non-contiguous allocation

Non-contiguous allocation is further divided into two methods -

  1. Linked list allocation
  2. Indexed allocation

 

What is the difference between contiguous allocation and non-contiguous file allocation methods?

S.No Contiguous allocation Non-contiguous allocation
1. Contiguous allocation allocates consecutive blocks of the disk to the file blocks. Non-contiguous allocation allocates random blocks of the disk to the file blocks based on the disk block availability. 
2. This method executes faster. This method executes slower.
3.  This method suffers both internal and external fragmentation. This method overcomes both internal and external fragmentation.
4. There is less overhead. There is more overhead.

 

What is the fragmentation in OS?

Fragmentation is an unwanted problem in the operating system that occurs when processes are loaded and unloaded from memory, causing free memory space to be fragmented. 

Conclusion

In this article, we went through the contiguous file allocation method. We also discussed the advantages and disadvantages of this method.

Reader, don’t stop here. Start your learning journey in the operating system with Coding Ninjas by taking the OS course.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning!

Live masterclass