Advantages of contiguous file allocation
Greater speed of access:
The entirety of the file remains consolidated in a single location, enhancing retrieval speed compared to scattered storage on the disk. Contiguous allocation is employed to minimize excessive fragmentation of the disk. Fragmentation arises when files are fragmented into small segments and stored in multiple dispersed locations, leading to challenges in locating and accessing the data.
Disadvantages of contiguous file allocation
- A file must be stored in a single block of space per contiguous allocation to prevent space wasting. Additional space is lost if the file takes up less space than is available.
- One restriction is the amount of contiguous disk space that can be used to store files using contiguous allocation. It may prohibit a file from being placed on the disk, even when there is enough space.
Having explored the benefits and drawbacks of contiguous file allocation, it's time to analyze how it is put into practice.
Implementation of contiguous file allocation in C:
Defining the data structures required to store details about files and the disk is the initial phase in implementing a continuous file allocation system.
Two types of structures will be discussed:
- An organizational format used to store the title, dimensions, and location of every file.
- A storage layout design containing details about available and occupied blocks along with the total block count.
The script for specifying these configurations is as outlined below:
struct file {
char name[50];
int size;
int start_block;
};
struct disk {
int total_blocks;
int *used_blocks;
int *free_blocks;
};
The disk needs to undergo initialization by indicating the overall block count and marking each block as available. Memory allocation will employ dynamic methods to assign memory for both the utilized and available block listings.
Here is the code to initialize the disk:
void init_disk(struct disk *d, int total_blocks) {
d->total_blocks = total_blocks;
d->used_blocks = (int *)calloc(total_blocks, sizeof(int));
d->free_blocks = (int *)calloc(total_blocks, sizeof(int));
for (int i = 0; i<total_blocks; i++) {
d->free_blocks[i] = 1;
}
}
Now, we are able to create the function responsible for allocating storage space for a file. This particular function provides a reference to the file's structure once it obtains the file's name and size as parameters.
Here is the code to allocate space for a file:
struct file *allocate_file(struct disk *d, char *name, int size) {
int start_block = -1;
int contiguous_blocks = 0;
for (int i = 0; i< d->total_blocks; i++) {
if (d->free_blocks[i] == 1) {
if (start_block == -1) {
start_block = i;
}
contiguous_blocks++;
if (contiguous_blocks == size) {
break;
}
} else {
start_block = -1;
contiguous_blocks = 0;
}
}
if (contiguous_blocks != size) {
return NULL;
}
for (int i = start_block; i<start_block + size; i++) {
d->used_blocks[i] = 1;
d->free_blocks[i] = 0;
}
struct file *f = (struct file *)malloc(sizeof(struct file));
strcpy(f->name, name);
f->size = size;
f->start_block = start_block;
return f;
}
Now, you can establish the function to release the disk space allocated by a file. This approach involves deallocating the blocks that were previously assigned to the file, based on the file structure.
The code snippet below will free up the storage space occupied by a file:
void free_file(struct disk *d, struct file *f)
{
// mark all the blocks, as free
for (int i = f->start_block; i< f->start_block + f->size; i++) {
d->used_blocks[i] = 0 ;
d->free_blocks[i] = 1 ;
}
free(f);
}
Now that we have established all the required functions, we can proceed to create a main function for testing the program. Below is a sample main function for reference:
int main() {
// initialize the disk with struct
struct disk d;
init_disk(&d, 10);
// allocate space for a respective file
struct file *f1 = allocate_file(&d, "file1", 3);
if (f1 == NULL) {
printf("Could not allocate space for file1\n");
exit(1);
}
// allocate space for other file
struct file *f2 = allocate_file(&d, "file2", 5);
if (f2 == NULL) {
printf("Could not allocate space for file2\n");
exit(1);
}
// free the 1st file
free_file(&d, f1);
// allocate space for a 3rd file
struct file *f3 = allocate_file(&d, "file3", 4);
if (f3 == NULL) {
printf("Could not allocate space for file3\n");
exit(1);
}
// free the 2nd file
free_file(&d, f2);
// free the 3rd file
free_file(&d, f3);
return 0;
}
In this case, we initialize the disk with 10 blocks and allot space for three files. After that, after making room for a third file, we release the remaining files before freeing the first file.
Output:
Here is the output of this program:$ ./contiguous_allocation
As observed, the software generated no results, confirming that all functions operated as expected.
Contiguous file allocation has the potential to deliver superior performance compared to alternative file allocation methods, especially when dealing with sequential access patterns. Reading or writing a file can be executed at a faster pace when its blocks are consecutively placed on the disk, as they are all grouped together in a single contiguous space. Industries that frequently engage in sequential I/O tasks, such as streaming video or music services, could benefit significantly from this approach.
Contiguous file allocation has the potential downside of disk fragmentation, which can occur when continuous blocks of space are not readily available for larger files due to frequent file creation and deletion. This fragmentation can scatter small free spaces throughout the disk, making it difficult to find contiguous space for new files. As a result, the increased movement time of the disk head to access dispersed file fragments can lead to wasted space and reduced performance.
Techniques such as disk defragmentation can be employed to rearrange the files stored on the disk and consolidate the free space into larger blocks, addressing this issue. An alternative file allocation approach, such as linked or indexed allocation, can enhance the management of frequent file creation and deletion, leading to reduced fragmentation. This serves as an alternate solution to the problem.
Fragmentation and the limited capacity for large files are common challenges associated with contiguous file allocation. Allocating contiguous space for very large files can be difficult, particularly when the disk is mostly full with existing files, as it requires identifying and securing contiguous blocks of sufficient size for the new file.
Contiguous file allocation may offer benefits in certain situations, like dealing with a small number of files or when fragmentation is expected to remain low, despite its limitations. To develop and apply file systems in your programs, it's crucial to grasp the unique implementation details and trade-offs of different file allocation algorithms.
It is essential to thoroughly consider the demands and limitations of different file allocation techniques when constructing a file system for an application and choose the one that most adequately meets those needs. Contiguous file allocation can be a valuable addition to your file system design arsenal as it provides a rapid and efficient method for assigning storage space for files on a disk.
Conclusion:
This article discussed the development of a sequential file allocation system in the C programming language. We initialized the storage medium, allocated storage for files, and freed up the occupied storage space when necessary. Additionally, we outlined the data structures used to store information about both files and the storage medium. To aid in comprehending the implementation process, a sample main function and corresponding output were included. Sequential file allocation offers practical benefits such as reducing fragmentation and enhancing disk access speed. However, it also presents limitations like restricting file sizes and challenges in allocating space for large files. Thus, carefully evaluating the pros and cons of different file allocation strategies is essential before deciding on one for a specific application.
Overall, carefully considering the related data structures and algorithms is necessary to develop a contiguous file allocation program in C. However, if used appropriately, this strategy can offer a quick and effective way to allocate file disc space. After reading the sample code and explanations in this blog article, you thought to have a solid idea of how to create a contiguous file allocation program in C and how it functions in practice.