5.4 Allocation Methods
5.4 Allocation Methods
The direct-access nature of disks gives us flexibility in the implementation of files. In almost every
case, many files are stored on the same disk. The main problem is how to allocate space to these
files so that disk space is utilized effectively and files can be accessed quickly. Three major
methods of allocating disk space are in wide use:
contiguous
linked
indexed
Each method has advantages and disadvantages. Although some systems support all three, it is more
common for a system to use one method for all files within a file-system type.
Contiguous Allocation
Contiguous allocation requires that each file occupy a set of contiguous blocks on the disk. Disk
addresses define a linear ordering on the disk. With this ordering, assuming that only one job is
accessing the disk, accessing block b + 1 after block b normally requires no head movement.
When head movement is needed (from the last sector of one
cylinder to the first sector of the next cylinder), the head
need only move from one track to the next. Thus, the
number of disk seeks required for accessing contiguously
allocated files is minimal, as is seek time when a seek is
finally needed. Contiguous allocation of a file is defined by
the disk address and length (in block units) of the first block.
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 length of the area
allocated for this file. Accessing a file that has been allocated contiguously is easy. For sequential
access, the file system remembers the disk address of the last block referenced and, when necessary,
reads the next block.
Accessing a file that has been allocated contiguously is easy. For sequential access, the file system
remembers the disk address of the last block referenced and, when necessary, reads the next block.
For direct access to block i of a file that starts at block b, we can immediately access block b + i.
Thus, both sequential and direct access can be supported by contiguous allocation.
Contiguous allocation has some problems, however. One difficulty is finding space for a new file.
The system chosen to manage free space determines how this task is accomplished.
As files are allocated and deleted, the free disk space is broken into little pieces. External
fragmentation exists whenever free space is broken into chunks. It becomes a problem when the
largest contiguous chunk is insufficient for a request; storage is fragmented into a number of holes,
none of which is large enough to store the data. Depending on the total amount of disk storage and
the average file size, external fragmentation may be a minor or a major problem.
Another problem with contiguous allocation is determining how much space is needed for a file.
When the file is created, the total amount of space it will need must be found and allocated. How
does the creator (program or person) know the size of the file to be created?
Linked Allocation
Linked allocation solves all problems of contiguous allocation. With linked allocation, each file is a
linked list of disk blocks; the disk blocks may be scattered anywhere on the disk. The directory
contains a pointer to the first and last blocks of the file.
For example, a file of five blocks might start at block 9 and
continue at block 16, then block 1, then block 10, and
finally block 25. Each block contains a pointer to the next
block. These pointers are not made available to the user.
Thus, if each block is 512 bytes in size, and a disk address
(the pointer) requires 4 bytes, then the user sees blocks of
508 bytes.
To create a new file, we simply create a new entry in the
directory. With linked allocation, each directory entry has a
pointer to the first disk block of the file. This pointer is
initialized to null (the end-of-list pointer value) to signify an
empty file. The size field is also set to 0.
A write to the file causes the free-space management system to find a free block, and this new block
is written to and is linked to the end of the file.
To read a file, we simply read blocks by following the pointers from block to block.
There is no external fragmentation with linked allocation, and any free block on the free-space list
can be used to satisfy a request. The size of a file need not be declared when the file is created. A
file can continue to grow as long as free blocks are available.
Linked allocation does have disadvantages, however. The major problem is that it can be used
effectively only for sequential-access files. 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. Each access to a pointer
requires a disk read, and some require a disk seek. Consequently, it is inefficient to support a direct-
access capability for linked-allocation files.
Another disadvantage is the space required for the pointers. If a pointer requires 4 bytes out of a
512-byte block, then 0.78 percent of the disk is being used for pointers, rather than for information.
Each file requires slightly more space than it would otherwise.
Indexed Allocation
Linked allocation solves the external-fragmentation
and size-declaration problems of contiguous allocation.
However, in the absence of a FAT, linked allocation
cannot support efficient direct access, since the
pointers to the blocks are scattered with the blocks
themselves all over the disk and must be retrieved in
order. Indexed allocation solves this problem by
bringing all the pointers together into one location: the
index block.
Each file has its own index block, which is an array of disk-block addresses. The i th entry in the
index block points to the ith block of the file. The directory contains the address of the index block.
To find and read the ith block, we use the pointer in the ith index-block entry.
Indexed allocation supports direct access, without suffering from external fragmentation, because
any free block on the disk can satisfy a request for more space. Indexed allocation does suffer from
wasted space, however. The pointer overhead of the index block is generally greater than the pointer
overhead of linked allocation.