Sequential File Allocation Program in C and C++ 

Sequential File Allocation Program in C and C++ as well as the algorithm of the Sequential File Allocation program to help you understand better. Also, sequential file allocation is also known as Contiguous File Allocation.

 

What is Sequential File Allocation in the Operating System?

In the Sequential File Allocation method, the file is divided into smaller chunks and these chunks are then allocated memory blocks in the main memory. These smaller file chunks are stored one after another in a contiguous manner, this makes the file searching easier for the file allocation system.

 

The Contiguous(Sequential) File Allocation is one of the File Allocation Methods in the Operating System. The Other File Allocation Method is the Non-contiguous File Allocation which also has two types – first is the Linked File Allocation and the second is the Indexed File Allocation. 

 

Why do we use the Sequential File Allocation method in the operating system?

The Sequential File Allocation or Contiguous File Allocation Method has an easy memory access advantage over the other two file allocation methods. In the contiguous File Allocation, the file is stored in sequential memory blocks and they are next to each other. So, when we have to search some files we look into the directory (directory has the starting block address of each file) and reach the starting block where the file starts and from there we will just read the next blocks in order to access the complete file. This access method also allows us to directly access the blocks of the memory as we can calculate easily where our required information is located.

 

Sequential File Allocation Program Algorithm:

STEP 1: Start the program.

STEP 2: Gather information about the number of files.

STEP 3: Gather the memory requirement of each file.

STEP 4: Allocate the memory to the file in a sequential manner. 

STEP 5: Select any random location from the available location. 

STEP 6: Check if the location that is selected is free or not.

STEP 7: If the location is allocated set the flag = 1.

STEP 8: Print the file number, length, and the block allocated.

STEP 9: Gather information if more files have to be stored.

STEP 10: If yes, then go to STEP 2.

STEP 11: If no, Stop the program.

 

Sequential File Allocation Program in C:

/* Program to simulate sequential file allocation strategy */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void recurse(int files[]){
    int flag = 0, startBlock, len, j, k, ch;
    printf("Enter the starting block and the length of the files: ");
    scanf("%d%d", &startBlock, &len);
    for (j=startBlock; j<(startBlock+len); j++){
        if (files[j] == 0)
            flag++;
    }
    if(len == flag){
        for (int k=startBlock; k<(startBlock+len); k++){
            if (files[k] == 0){
                files[k] = 1;
                printf("%d\t%d\n", k, files[k]);
            }
        }
        if (k != (startBlock+len-1))
            printf("The file is allocated to the disk\n");
    }
    else
        printf("The file is not allocated to the disk\n");

    printf("Do you want to enter more files?\n");
    printf("Press 1 for YES, 0 for NO: ");
    scanf("%d", &ch);
    if (ch == 1)
        recurse(files);
    else
        exit(0);
    return;
}

int main()
{
int files[50];
for(int i=0;i<50;i++)
files[i]=0;
printf("Files Allocated are :\n");

recurse(files);
getch();
return 0;
}

 

 

Sequential File Allocation Program in C++:

/* Program to simulate sequential file allocation strategy */
#include <iostream>
#include <conio.h>
using namespace std;

void recurse(int files[]){
    int flag = 0, startBlock, len, k;
    cout << "Enter the starting block and the length of the files: ";
    cin >> startBlock >> len;
    for (int j=startBlock; j<(startBlock+len); j++){
        if (files[j] == 0)
            flag++;
    }
    if(len == flag){
        for (int k=startBlock; k<(startBlock+len); k++){
            if (files[k] == 0){
                files[k] = 1;
                cout << k <<"\t" << files[k] << endl;
            }
        }
        if (k != (startBlock+len-1))
            cout << "The file is allocated to the disk" << endl;
    }
    else
        cout << "The file is not allocated to the disk" << endl;

    cout << "Do you want to enter more files?" << endl;
    int ch;
    cout << "Press 1 for YES, 0 for NO: ";
    cin >> ch;
    if (ch == 1)
        recurse(files);
    else
        exit(0);
    return;
}

int main()
{
int files[50];
for(int i=0;i<50;i++)
files[i]=0;
cout << "Files Allocated are :" << endl;

recurse(files);
getch();
return 0;
}

 

The output of the Sequential File Allocation Program in C and C++:

Sequential File Allocation Program in C Output

 

The Sequential File Allocation program provides you faster access to the memory but at the same time causes external fragmentation. The Linked File Allocation program in C and C++ deals with the external fragmentation but does not have a direct access facility as Sequential File Allocation has. But the Indexed File Allocation Program in C and C++ has both the direct access facility and can avoid any external fragmentation. 

 

Leave a Comment