OS Unit 4
OS Unit 4
1 2
By Pushpendra Singh Chundawat
Cont..
1 3
By Pushpendra Singh Chundawat
Cont..
1 4
By Pushpendra Singh Chundawat
Cont..
What is File System?
A file is a collection of correlated information which is recorded on secondary or non-
volatile storage like magnetic disks, optical disks, and tapes. It is a method of data
collection that is used as a medium for giving input and receiving output from that
program.
In general, a file is a sequence of bits, bytes, or records whose meaning is defined by the
file creator and user. Every File has a logical location where they are located for storage
and retrieval.
Files are stored on disk or other storage and do not disappear when a user logs off.
Files have names and are associated with access permission that permits controlled
sharing.
Files could be arranged or more complex structures to reflect the relationship between
them.
Protection. This attribute assigns and controls the access rights of reading, writing, and
executing the file.
Time, date and security: It is used for protection, security, and also used for monitoring
Field:
This element stores a single value, which can be static or variable length.
DATABASE:
Collection of related data is called a database. Relationships among elements of data
are explicit.
FILES:
Files is the collection of similar record which is treated as a single entity.
RECORD:
A Record type is a complex data type that allows the programmer to create a new data
type with the desired column structure. Its groups one or more columns to form a new
data type. These columns will have their own names and data type.
Sequential Access
In this type of file access method, records are accessed in a certain pre-defined
sequence. In the sequential access method, information stored in the file is also
processed one by one. Most compilers access files using this access method.
Random Access
The random access method is also called direct random access. This method
allow accessing the record directly. Each record has its own address on which
can be directly accessed for reading and writing.
Sequential Access
Contiguous Allocation
In this method,
Every file users a contiguous address space on memory.
Here, the OS assigns disk address is in linear order.
In the contiguous allocation method, external fragmentation is the biggest issue.
Linked Allocation
In this method,
Every file includes a list of links.
The directory contains a link or pointer in the first block of a file.
With this method, there is no external fragmentation
This File allocation method is used for sequential access files.
This method is not ideal for a direct access file.
Indexed Allocation
In this method,
Directory comprises the addresses of index blocks of the specific files.
An index block is created, having all the pointers for specific files.
All files should have individual index blocks to store the addresses for disk
space.
1 20
By Pushpendra Singh Chundawat
Cont..
Operation performed on directory are:
Search for a file
Create a file
Delete a file
List a directory
Rename a file
Traverse the file system
Naming: It becomes convenient for users as two users can have same name for
different files or may have different name for same file.
Grouping: Logical grouping of files can be done by properties e.g. all java programs,
all games etc.
1 21
By Pushpendra Singh Chundawat
Structures of Directory in Operating System
A directory is a container that is used to contain folders and file. It organizes files and
folders into a hierarchical manner.
Naming problem: Users cannot have same name for two files.
Grouping problem: Users cannot group files according to their need.
1 23
By Pushpendra Singh Chundawat
Cont..
Single-level directory –
Single level directory is simplest directory structure.In it all files are contained in same
directory which make it easy to support and understand.A single level directory has a
significant limitation, however, when the number of files increases or when the system
has more than one user. Since all the files are in the same directory, they must have the
unique name . if two users call their dataset test, then the unique name rule violated.
Advantages:
Since it is a single directory, so its implementation is very easy.
If the files are smaller in size, searching will become faster.
The operations like file creation, searching, deletion, updating are very easy in such a
directory structure.
Disadvantages:
There may chance of name collision because two files can not have the same name.
Searching will become time taking if the directory is large.
In this can not group the same type of files together.
1 24
By Pushpendra Singh Chundawat
Cont..
TWO-LEVEL DIRECTORY
In this separate directories for each user is maintained.
Path name: Due to two levels there is a path name for every file to locate that file.
Now, we can have same file name for different user.
Searching is efficient in this method.
1 25
By Pushpendra Singh Chundawat
Cont..
As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each
user.In the two-level directory structure, each user has there own user files directory
(UFD). The UFDs has similar structures, but each lists only the files of a single user.
system’s master file directory (MFD) is searches whenever a new user id=s logged in.
The MFD is indexed by username or account number, and each entry points to the UFD
for that user.
1 26
By Pushpendra Singh Chundawat
Cont..
Advantages:
We can give full path like /User-name/directory-name/.
Different users can have same directory as well as file name.
Searching of files become more easy due to path name and user-grouping.
Disadvantages:
A user is not allowed to share files with other users.
Still it not very scalable, two files of the same type cannot be grouped together in the
same user.
1 27
By Pushpendra Singh Chundawat
Cont..
TREE-STRUCTURED DIRECTORY :
1 28
By Pushpendra Singh Chundawat
Cont..
TREE-STRUCTURED DIRECTORY :
Once we have seen a two-level directory as a tree of height 2, the natural generalization
is to extend the directory structure to a tree of arbitrary height.
This generalization allows the user to create there own subdirectories and to organize
on their files accordingly.
1 29
By Pushpendra Singh Chundawat
Cont..
TREE-STRUCTURED DIRECTORY :
A tree structure is the most common directory structure. The tree has a root directory,
and every file in the system have a unique path.
Advantages:
Very generalize, since full path name can be given.
Very scalable, the probability of name collision is less.
Searching becomes very easy, we can use both absolute path as well as relative.
Disadvantages:
Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
We can not share files.
It is inefficient, because accessing a file may go under multiple directories.
1 30
By Pushpendra Singh Chundawat
Acyclic graph directory –
An acyclic graph is a graph with no cycle and allows to share
subdirectories and files. The same file or subdirectories may
be in two different directories. It is a natural generalization of
the tree-structured directory.It is used in the situation like
when two programmers are working on a joint project and
they need to access files.
It is the point to note that shared file is not the same as copy
file . If any programmer makes some changes in the
subdirectory it will reflect in both subdirectories.
1 31
By Pushpendra Singh Chundawat
Acyclic graph directory –
Advantages:
We can share files.
Searching is easy due to different-different paths.
Disadvantages:
We share the files via linking, in case of deleting it may
create the problem,
If the link is softlink then after deleting the file we left with a
dangling pointer.
In case of hardlink, to delete a file we have to delete all the
reference associated with it.
1 32
By Pushpendra Singh Chundawat
General graph directory structure –
In general graph directory structure, cycles
are allowed within a directory structure where
multiple directories can be derived from more
than one parent directory.
Advantages:
It allows cycles.
It is more flexible than other directories
structure.
Disadvantages:
It is more costly than others.
It needs garbage collection.
1 33
By Pushpendra Singh Chundawat
FILE ALLOCATION METHODS
The allocation methods define how the files are stored in the disk blocks. There are
three main disk space or file allocation methods.
Contiguous Allocation
Linked Allocation
Indexed Allocation
All the three methods have their own advantages and disadvantages as discussed
1 34
By Pushpendra Singh Chundawat
FILE ALLOCATION METHODS
1 35
By Pushpendra Singh Chundawat
Cont..
The directory entry for a file with contiguous allocation contains
Address of starting block
Length of the allocated portion.
Advantages:
Both the Sequential and Direct Accesses are supported by this.
For direct access, the address of the kth block of the file which starts at block b can easily
be obtained as (b+k).
This is extremely fast since the number of seeks are minimal because of contiguous
allocation of file blocks.
External fragmentation will occur, making it difficult to find contiguous blocks of space of
sufficient length.
Also, with pre-allocation, it is necessary to declare the size of the file at the time of
creation.
Disadvantages:
Because the file blocks are distributed randomly on the disk, a large number of seeks are
needed to access every block individually. This makes linked allocation slower.
It does not support random or direct access. We can not directly access the blocks of a file.
A block k of a file can be accessed by traversing k blocks sequentially (sequential access )
from the starting block of the file via block pointers.
Pointers required in the linked allocation incur some extra overhead.
Internal fragmentation exists in last disk block of file.
There is an overhead of maintaining the pointer in every disk block.
If the pointer of any disk block is lost, the file will be truncated.
It supports only the sequencial access of files.
By Pushpendra Singh Chundawat 1 39
Indexed Allocation
It addresses many of the problems of contiguous
and chained allocation. In this case, the file
allocation table contains a separate one-level
index for each file.
Disadvantages:
The pointer overhead for indexed allocation is greater than linked allocation.
For very small files, say files that expand only 2-3 blocks, the indexed allocation would
keep one entire block (index block) for the pointers which is inefficient in terms of memory
utilization. However, in linked allocation we lose the space of only 1 pointer per block.
Linked scheme: This scheme links two or more index blocks together for holding the
pointers. Every index block would then contain a pointer or the address to the next index
block.
Multilevel index: In this policy, a first level index block is used to point to the second
level index blocks which inturn points to the disk blocks occupied by the file. This can be
extended to 3 or more levels depending on the maximum file size.
Combined Scheme: In this scheme, a special block called the Inode (information
Node) contains all the information about the file such as the name, size, authority, etc
and the remaining space of Inode is used to store the Disk Block addresses which
contain the actual file
Bit Tables : This method uses a vector containing one bit for each block on the disk. Each
entry for a 0 corresponds to a free block and each 1 corresponds to a block in use.
For example: 00011010111100110001In this vector every bit correspond to a particular
block and 0 implies that, that particular block is free and 1 implies that the block is already
occupied. A bit table has the advantage that it is relatively easy to find one or a contiguous
group of free blocks.
Advantages
a bit table works well with any of the file allocation methods.
Another advantage is that it is as small as possible.