Methods of File Allocation
For the purpose of quick access and effective utilization of disk
space we use three methods
Contiguous allocation
Linked allocation (Chained)
Indexed allocation
Contiguous Allocation
2
Single set of blocks is allocated to a file at the time of creation.
Each file occupies a set of contiguous blocks on the disk
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 allocate for this file.
Start pointes to 1st block
In file
Length is the number
of blocks in file
Advantages and disadvantages
4
Advantages
Accessing a file that has been allocated contiguously is easy.
Access is simple for both sequential and random methods
disadvantages
Contiguous allocation has some problems
One difficulty is to finding a space for a new file.
First Fit and Best Fit are the most common strategies used
to select a free hole from the set of available holes
External fragmentation.
When a file is allocated or deleted free space
is occur.it become a problem when the largest
contiguous chunk is insufficient for a request.
It can be overcome by compaction :compacts all
free space in to one contiguous block.
Another problem with contiguous allocation is determining how much
space is needed for a file.
In some cases ,this determination may be fairly simple.in
general,however,the size of an output file may be difficult to estimate.
If we allocate too little space to a file ,we may find that the file cannot
be extended.
In a best fit allocation strategy ,the space on both sides of the file may
be in use.
To minimize these drawbacks ,some OS use a modified contiguous
allocation scheme.
Here, contiguous chunk of space allocated initially;then,if that amount
not enough,another chunk of contiguous space,known as extent is
added.
An extent is a contiguous block of disks. Extents are allocated for file
allocation. A file consists of one or more extents
Linked/Chained Allocation
9
Linked allocation solves all problems of contiguous
allocation.
Each file is a linked list of disk blocks: blocks may be
scattered anywhere on the disk.
Each block contains a pointer to the next block in the
chain
There is a link between one to another blocks, that is
why it is said to be linked allocation.
The directory contains a pointer to the first and last
blocks of the file.
10
10
16
-1
25
Advantages and disadvantages
There is no external fragmentation with linked allocation, and any free
block on the free-space list can be used to satisfy a request.
there is no need to declare the size of a file when that file is created. A file
can continue to grow as long as there are free blocks. Consequently, it is
never necessary to compact disk space.
11
The major problem is that it can be used effectively for only 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 sometimes a disk
seek.
Consequently, it is inefficient to support a direct-access capability for
linked allocation files.
12
Another disadvantage to linked allocation 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 pointer, rather than for information.
The usual solution to this problem is to collect blocks into multiples, called
clusters, and to allocate the clusters rather than blocks. For instance, the file
system define a cluster as 4 blocks and operate on the disk in only cluster
units. Pointers then use a much smaller percentage of the file's disk space
.The cost of this approach an increase in internal fragmentation.
13
another problem is reliability.
Since the files are linked together by pointers scattered all over the disk,
consider what would happen if a pointer were lost or damaged.
Partial solutions are to use doubly linked lists or to store the file name
and relative block number in each block;
14
File allocation table (FAT).
15
An important variation, on the linked allocation method is the use of a file
allocation table (FAT).
This simple but efficient method of disk-space allocation is used by the MSDOS and OS/2 operating systems.
A section of disk at the beginning of each-partition is set aside to contain the
table.
The table has one entry for each disk block, and is indexed by block number.
The directory entry contains the block number of the first block of the
file.
The table entry indexed by that block number then contains the block
number of the next block in the file.
This chain continues until the last block, which has a special end-offile value as the table entry.
16
File-Allocation Table
17
Unused blocks are indicated by a 0 table value.
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.
The 0 is then replaced with the end-of file value.
18
Indexed Allocation
19
Non-contiguous
Linked allocation solves the external-fragmentation and size-declaration
problems of contiguous allocation.
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 need to be retrieved in order
Indexed allocation solves this problem by bringing all thepointers together
into one location: the 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 contains the address of the index block.
Advantage of this approach is Allocation supports direct access, without
suffering from external fragmentation
To read the ith block, we use the pointer in the ith index-block entry to find
and read the desired block.
The file allocation table contains block number for the index
When the file is created, all pointers in the index block are set to nil.
20
21
Problems with index allocation
The pointer overhead of the index block is generally
greater than the pointer overhead of linked
allocation.
If the index block is too small, however, it will not be
able to hold enough pointers for a large file.
22
Mechanism to handle the size of index block
1. Linked scheme. An index block is normally one disk block. Thus,
itcan be read and written directly by itself. To allow for large files, we
may link together several index blocks.(no limit on size)
23
2. Multilevel index.
A variant of the linked representation is to use 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 that block to find the desired data
block.
Increase number of levels based on maximum file size desired.
Maximum size of file is bounded
24
Indexed File - Linked Scheme
file block
Index block
link
link
Indexed Allocation - Multilevel index
2nd level Index
Index block
link
link
Free Space Management
27
Since there is only a limited amount of disk space, it
is necessary to reuse the space
from deleted files for new files, if possible.
To keep track of freedisk space, the OS maintains a
free-space list.
The free-space list records all
free disk blocks-those not allocated to some file or
directory.
To create a file,we search the free-space list for the
required amount of space, and allocate that space to
the new file. This space is then removed from the
free-space list.
When a file is deleted, its disk space is added to the
free-space list.
28
29
Free space implementation methods are
Bit map method
Linked list method
Grouping
Counting
Bit map/Bit vector method
30
one bit for each block
to indicate if it is used or free
if block is free, bit is 1
If block is allocated, bit is o
For example consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17,
18, 25, 26, and 27 are free, and the rest of the blocks are allocated.
The free space bitmap wouldbe
001111001111110001100000011100000 ..
31
The main advantage of this approach is that it is relatively simple and
efficient to find the first free block or n consecutive free blocks on the
disk.
The calculation of the block number is
(number of bits per word) x (number of 0-value words) + offset of first 1
bit
32
disadvantages
It may require a special hardware support to do bit
operationsi.e finding out which bit is 1 and which bit
is 0
bit vectors are inefficient unless the entire vector is
kept in main memory (and is written to disk
occasionally for recovery needs).
Linked list method
33
Another approach is to link together all the free disk blocks, keeping
apointer to the first free block in a special location on the disk and
caching it in memory.
This first block contains a pointer to the next free disk block ,and so on
Blocks are allocated from the beginning
34
Advantages
operating system simply needs a free block so that it can allocate that
block to a file, so the first block in the free list is used.
No disk fragmentation
Disadvantages
to traverse the list, we must read each block, which requires substantial
I/Otime
Pointers are also used diskspace
35
Grouping
36
modification of free-list approach an indexed approach
Store the address of n free blocks in the first free block.
The first n-1 of these blocks are actually free .the last block contains the
addresses of another n free blocks, and so on.
Advantage: the addresses of a large number of free blocks can be
quickly
found
Counting
37
several contiguous blocks may be allocated or freed simultaneously ,
particularly space is allocated with the contiguous
allocation algorithm or through clustering
Rather than keeping a list of n free disk addresses ,we can keep the
address of the first free block and number n of free contiguous
blocks that follow the first block
Each entry in the free space list then consist of a disk address and a
count.