Allocation Methods
Allocation Methods
The main problem is how to allocate space to these files so that storage space is utilized effectively and files can be accessed
quickly.
To allocate space to these files three major methods are in wide use: contiguous, linked, and indexed.
An operating system uses one method for all files within a file-system type.
Contiguous Allocation
If the file is n blocks long and starts at location b, then it occupies blocks b, b+1, b+2, ... ,b+n-1.
The directory entry for each file indicates the address of the starting block and the number of blocks allocated for that file.
Advantages:
1) Easy to implement
2) Supports both sequential and direct access to the blocks of file (For direct access to block i of a file that starts at block b,
we can immediately access block b + i ).
Disadvantages:
1) External fragmentation
As files are allocated and deleted, the free disk space is broken into pieces.
External fragmentation exists whenever free space is broken into chunks and when the largest contiguous chunk is
insufficient for a request.
Compaction technique compact all free space into one contiguous space.
When the file is created, the total amount of space it will need must be found and allocated.
If too little space is allocated to a file then the file cannot be extended.
If more space is allocated then some space may be wasted (internal fragmentation).
To minimize these drawbacks, some operating systems use a modified contiguous-allocation scheme.
In this scheme, a contiguous chunk of space is allocated initially; then, if that space is not enough, another chunk of
contiguous space, known as an extent is added.
The directory entry of the file now contains address of the starting block, block count, plus address of first block of the next
extent.
Linked Allocation
With linked allocation, free blocks at any position of the disk can be allocated to a file.
For example, a file of five blocks may start at block 9 and continue at block 16, then block 1, then block 10, and finally block
25.
Each block allocated to the file contains a pointer to the next block allocated to the file.
The directory entry of a file contains a pointer to the first and last blocks of the file.
Advantages:
1) There is no external fragmentation with linked allocation, and any free block on the free-space list can be used to satisfy a
request.
2) The size of a file need not be declared when that file is created. A file can continue to grow as long as free blocks are
available.
Disadvantages:
1) Does not support direct access. To find the ith block of a file, we must start at the beginning of that file and follow the
pointers until we get to the ith block.
One solution to this problem is to collect blocks into multiples, called clusters and to allocate clusters rather than blocks.
Pointers then use a much smaller percentage of the file's disk space.
Cluster mechanism improves disk throughput and decreases the space needed for block allocation and free-list
management.
But, this approach increases internal fragmentation, because more space is wasted when a cluster is partially full than when
a block is partially full.
3) Another problem with linked allocation is reliability. If a block allocated to a file is corrupted then it is not possible to
access the remaining blocks of the file.
This simple but efficient method of disk-space allocation was used by the MS-DOS operating system.
A section of storage at the beginning of the disk is reserved for the FAT.
The FAT has one entry for each block in the disk and is indexed by block number.
Initially, all entries in FAT are filled with 0 to indicate that all blocks in the disk are initially free.
To store a file in the disk, free blocks at any position of the disk can be allocated to the file as in linked allocation technique.
In the directory entry, name of the file and number of the first block allocated to the file is stored.
In the FAT, the entry indexed by the first block number that is allocated to the file contains the number of the next block
allocated to the file.
This chain continues until it reaches the last block, which has a special end-of-file value as the table entry.
Following figure shows how the blocks allocated to the file are linked with FAT.
Allocating a new block to a file is a simple matter of finding the first 0-valued table entry and replacing the previous end-of-
file value with the address of the new block.
Free blocks at any position of the disk can be allocated to a file as in linked allocation method.
The addresses of blocks allocated to the file are stored into another block called index block.
Each file has its own index block, which is an array of disk-block addresses.
The ith entry in the index block points to the ith block of the file.
The directory entry of the file contains the address of the index block.
To access the ith block, we use the pointer in the ith entry of index block.
Advantages:
1) There is no external fragmentation with indexed allocation, and any free block on the free-space list can be used to satisfy
a request.
2) The size of a file need not be declared when that file is created. A file can continue to grow as long as free blocks are
available.
3) Blocks of the file can be accessed directly. ith block of the file can be accessed directly using the pointer in the ith entry of
index block.
4) If one of the blocks allocated to the file is corrupted, still the remaining blocks of the file can be accessed.
Disadvantages:
Consider a file which occupies only one or two blocks. With linked allocation, we lose the space of only one pointer per
block.
With indexed allocation, an entire index block must be allocated, even if only one or two pointers will be non-null.
2) Size of file is limited to number of pointers in the index block.
Linked scheme
If one index block is not enough to store the addresses of blocks allocated to a file then a number of index blocks are
allocated to the file and they are linked together.
The last entry in first index block contains the address of second index block.
This chain is repeated till the last index block of the file.
Multilevel index
This scheme uses a first-level index block to point to a set of second-level index blocks, which in turn point to the file blocks.
To access a block, the operating system uses the first-level index to find a second-level index block and then uses that block
to find the desired data block.
This approach could be continued to a third or fourth level, depending on the file size.
Combined scheme
In this scheme, the first 15 pointers of the index block are stored in the file's inode.
The first 12 of these pointers point to direct blocks; that is, they contain addresses of blocks that contain data of the file.
Thus, the data for small files (of not more than 12 blocks) do not need a separate index block.
The first pointer points to a single indirect block, which is an index block containing not data but the addresses of blocks that
do contain data.
The second pointer points to a double indirect block, which contains the address of a block that contains the addresses of
blocks that contain pointers to the actual data blocks.