1. File Organization
Files are normally stored on the disks. So the main problem is how to allocate space to those files.
So that disk space is utilized effectively and files can be accessed quickly. Three major strategies
of allocating disc space are in wide use. Sequential, indexed and linked.
Sequential Allocation
In this allocation strategy, each file occupies a set of contiguously blocks on the disk. This strategy
is best suited. For sequential files, the file allocation table consists of a single entry for each file.
It shows the filenames, starting block of the file and size of the file. The main problem with this
strategy is, it is difficult to find the contiguous free blocks in the disk and some free blocks could
happen between two files.
Sequential File Allocation 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 count = 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
2. Sample Code
#include <stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : n");
x:count=0;
printf("Enter starting block and length of files:");
scanf("%d%d",&st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%dt%dn",j,f[j]);
}
if(j!=(st+len-1))
printf("The file is allocated to diskn");
}
else
printf("The file is not allocated n");
3. printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Sample Output
Case 1:
Files Allocated are :
Enter starting block and length of files:14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files:14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files:14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)
Case 2:
Files Allocated are :
Enter starting block and length of files:17 4
17 1
18 1
4. 19 1
20 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files:21 3
21 1
22 1
23 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files:19 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files:25 5
25 1
26 1
27 1
28 1
29 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)
5. Indexed File Allocation
The Indexed File Allocation stores the file in the blocks of memory, each block of memory has an
address and the address of every file block is maintained in a separate index block. These index
blocks point the file allocation system to the memory blocks which actually contains the file.
Indexed File Allocation Algorithm
STEP 1: Start the program.
STEP 2: Get information about the number of files.
STEP 3: Get the memory requirement of each file.
STEP 4: Allocate the memory to the file by selecting random locations.
STEP 5: Check if the location that is selected is free or not.
STEP 6: If the location is allocated set the count = 1, and if free set count = 0.
STEP 7: Print the file number, length, and the block allocated.
STEP 8: Gather information if more files have to be stored.
STEP 9: If yes, then go to STEP 2.
STEP 10: If no, Stop the program.
6. Sample Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d
on the disk : n",ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
8. goto x;
else
exit(0);
getch();
}
Sample Output
Case 1:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1 2 3 4
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
7 8
9. Allocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)
Case 2:
Enter the index block: 4
Enter no of blocks needed and no of files for the index 4 on the disk :
3
1 2 3
Allocated
File Indexed
4-------->1 : 1
4-------->2 : 1
4-------->3 : 1
Do you want to enter more file(Yes - 1/No - 0)
10. Linked File Allocation
Linked File Allocation is a Non-contiguous memory allocation method where the file is stored in
random memory blocks and each block contains the pointer (or address) of the next memory block
as in a linked list. The starting memory block of each file is maintained in a directory and the rest
of the file can be traced from that starting block.
Linked File Allocation Algorithm
STEP 1: Start the program.
STEP 2: Gather information about the number of files.
STEP 3: Allocate random locations to the files.
STEP 4: Check if the location that is selected is free or not.
STEP 5: If the location is free set the count=0 a location is allocated set the count = 1.
STEP 6: Print the file number, length, and the block allocated.
STEP 7: Gather information if more files have to be stored.
STEP 8: If yes, then go to STEP 2.
STEP 9: If no, Stop the program.
13. Sample Output
Case 1:
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter more file(Yes - 1/No - 0)
Case 2:
Enter how many blocks already allocated: 6
Enter blocks already allocated: 2 4 6 8 10 12
Enter index starting block and length: 3 10
3-------->1
4 Block is already allocated
5-------->1
6 Block is already allocated
7-------->1
8 Block is already allocated
9-------->1
10 Block is already allocated
11-------->1
12 Block is already allocated
14. 13-------->1
14-------->1
15-------->1
16-------->1
17-------->1
Do you want to enter more file(Yes - 1/No - 0)1
Enter index starting block and length: 5 1
5 starting block is already allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter index starting block and length: 18 2
18-------->1
19-------->1
Do you want to enter more file(Yes - 1/No - 0)