Operating System VVFGC, Tumkur
Chapter-5
File System
Definition:
A file is a collection of related information which is stored on secondary storage device.
(Or) A file is a collection of records, where each record contains some fields, where each field
represents some data.
Attributes of the File
1. Name
This attribute stores the name of the file. One directory cannot have two files with the same
name.
2. Identifier
Along with the name, Each File has its own unique identification number. This will not be
repeated.
3. Type
In a File System, the Files are classified in different types such as video files, audio files, text
files, executable files, etc.
4. Location
In the File System, there are several locations on which, the files can be stored. Each file carries
its location as its attribute.
5. Size
The Size of the File is one of its most important attribute. By size of the file, we can count
number of bytes acquired by the file in the memory.
1
Operating System VVFGC, Tumkur
Initially the size of the file is 0 bytes. Based on the data the file size will be increased.
6. Protection
The Admin of the computer may want the different protections for the different files. Therefore
each file carries its own set of permissions to the different group of Users.
There are 3 main protections of file they are read®, write(w) and execute(x).
7. Time and Date
Every file carries a time stamp which contains the time and date on which the file is creation and
last modified.
Operations on files
1. Create operation:
This operation is used to create a file in the file system. It is the most widely used operation
performed on the file system.
This file system allocates space to the file. Here uses create () system call.
2. Open operation:
Once the file is created, it must be opened before performing the file processing operations.
When the user wants to open a file, it provides a file name to open the particular file in the file
system. Here uses open() system call.
3. Write operation:
This operation is used to write the information into a file.
A system call write() is issued that specifies the name of the file and the length of the data has to
be written to the file.
2
Operating System VVFGC, Tumkur
Whenever the file length is increased by specified value and the file pointer is repositioned after
the last byte written.
4. Read operation:
This operation reads the contents from a file.
A Read pointer is maintained by the OS, pointing to the position up to which the data has been
read.
5. Re-position or Seek operation:
The seek system call re-positions the file pointers from the current position to a specific place in
the file i.e. forward or backward depending upon the user's requirement.
6. Delete operation:
Deleting the file means will not only delete all the data stored inside the file it is also used to
delete the content, so that disk space occupied by it is freed.
Hence the space will be provided to another file.
7. Truncate operation:
Truncating is simply deleting the file content but not its structure / attributes. Size of the file is 0.
8. Close operation:
When the processing of the file is complete, it should be closed so that all the changes made
permanent and all the resources occupied should be released.
9. Append operation:
This operation adds data to the end of the file.
10. Rename operation:
3
Operating System VVFGC, Tumkur
This operation is used to rename the existing file.
File types
File type Usual extension Function
Executable .exe, .com, .bin Ready to run machine language program
Object .obj, .o After Compiling done this file is generated.
Source Code .C,. java, .asm, etc. Source code in various languages
Commands to the command interpreter (UNIX /
Batch .bat, .sh
LINUX)
Text .txt, .rtf Textual data, documents
Word
.doc , .docx Various word processor formats
Processor
Archive .arc, .zip, .tar Related files grouped into one compressed file
.mpeg, .mov, .mp4,
Multimedia For containing audio/video information
.mp3, .avi
Markup .xml, .html, .tex It is the textual data and documents
Library .lib, .dll It contains libraries files for programmers
It is a format for printing or viewing an ASCII
Print or View .gif, .pdf,. jpg
or binary file.
File Access Methods
There are three ways to access a file into a computer system:
o Sequential-Access,
o Direct Access,
4
Operating System VVFGC, Tumkur
o Index sequential Method.
Sequential Access:
It is the simplest access method. Information in the file is processed in order, one record after the
other.
For example a file contains 100 records, in order to access 75 th record the search begins
from 1st record. It cannot directly jump to the 75th record.
Operations are:
Read next: read the content in that record first and automatically advance a file pointer, which
keeps track I/O location.
Write next- writing content into the file to and advance a file pointer.
Rewind: it indicates current pointer position move back to the beginning of the file.
Direct Access:
Another method is direct access method also known as relative access method.
o The direct access is based on the disk model of a file since disk allows random access to
any file block.
o For direct access, the file is viewed as a numbered sequence of block or record. Thus, we
may read block 14 then block 59, and then we can write block 17. There is no restriction
on the order of reading and writing for a direct access file.
Operations are:
Read next: read CP
CP= CP+1
Write next- : write CP
CP= CP+1
CP = 0 : it indicates current pointer position move back to the beginning of the file.
Indexed Access
If a file can be sorted on any of the filed then an index can be assigned to a group of certain records.
A particular record can be accessed by its index like primary, secondary indexes etc. The index is
nothing but the address of a record in the file.
5
Operating System VVFGC, Tumkur
For example Primary index pointer is pointing to secondary index, secondary index pointer is
pointing to the third index like so on.
In index accessing, searching in a large database became very quick and easy but we need to have
some extra space in the memory to store the index value.
Directory Structure
A directory is a container that is used to contain list of folders and files. It organizes files and
folders in a hierarchical manner.
Single Level Directory
The simplest method is to have one big list of all the files on the disk.
The entire system will contain only one directory which is supposed to mention all the files present
in the file system.
The directory contains one entry per each file present on the file system.
Since all the files are in the same directory, they must have a unique name.
Advantages
1. Implementation is very simple.
2. If the sizes of the files are very small then the searching becomes faster.
3. File creation, searching, deletion is very simple since we have only one directory.
6
Operating System VVFGC, Tumkur
Disadvantages
1. We cannot have two files with the same name.
2. Protection cannot be implemented for multiple users.
3. There are no ways to group same kind of files.
Two-level directory:
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 their own user files directory (UFD).
The UFDs have similar structures, but each lists only the files of a single user.
System’s master file directory (MFD) is searched whenever a new user id is correct.
The main advantage is there can be more than two files with same name, and would be very
helpful if there are multiple users.
As there is advantage of security, there is also disadvantage that the user cannot share the file
with the other users.
Tree Structure/ Hierarchical Structure:
In Tree structured directory system, any directory entry can either be a file or sub directory.
7
Operating System VVFGC, Tumkur
Tree structured directory system overcomes the drawbacks of two level directory system. The
similar kind of files can now be grouped in one directory.
A user do not have access to the root directory data and cannot modify it. And, even in this
directory the user do not have access to other user’s directories.
Acyclic Graph Structure:
As we have seen the above three directory structures, where none of them have the capability to
access one file from multiple directories.
The file or the subdirectory could be accessed through the directory it was present in, but not
from the other directory.
This problem is solved in acyclic graph directory structure, where a file in one directory can be
accessed from multiple directories.
8
Operating System VVFGC, Tumkur
Where a file is shared between multiple users. If any user makes a change, it would be reflected
to both the users.
Advantages:
Sharing of files and directories is allowed between multiple users.
Searching becomes too easy.
File Protection
The files which have direct access of the any user must need of protection. The files which are not
accessible to other users doesn’t require any kind of protection.
There are 3 types of files. They are
Ordinary files
Directory files
Special/ device files
Each file can have 3 types of access permissions. They are
Read (r) = 4
Write (w) = 2
Execute (x) = 1
9
Operating System VVFGC, Tumkur
Each file can have 3 types of users. They are
File Owner (U)
Group Owner (G)
Others (O)
Changing the file permission:
“chmod” Command:
The “mode” helps in setting new permissions that have to be applied to files or directories.
Examples:
- r-x --x --x 1 root filename 67504 Jun 17 12:01 path
d rwx rwx rwx 2 root directory name 400 Sep 3 13:37 path
Symbolic and Octal mode.
1) Symbolic mode
If we talk about symbolic mode, we can say that it is the most common method used for
specifying fir permissions. In this we have to make a combination of letters and operators to set
or tell what to do with permissions.
The following operators can be used with the symbolic mode:
Operators Definition
`+` Add permissions
`-` Remove permissions
Examples of Using the Symbolic mode:
Read, write and execute permissions to the file owner:
10
Operating System VVFGC, Tumkur
chmod u+rwx [file_name]
Remove write permission for the group and others:
chmod go-w [file_name]
Read and write for Owner, and Read-only for the group and other:
chmod u+rw,go+r [file_name]
2) Octal mode
It is also a method for specifying permissions. In this method we specify permission using three-
digit number. Where..
First digit specify the permission for Owner.
Second digit specify the permission for Group.
Third digit specify the permission for Others.
Example of Using the Octal mode:
Suppose if we to give read and write permission to the file Owner. Read, write and executable
permission to the Group. Read-only permission to the other.
Syntax:
chmod 674 [file_name]
Here.
6 represent permission of file Owner which are (rw).
7 represent permission of Group which are (rwx).
4 represent permission of Others which is (r).
File Allocation Methods
11
Operating System VVFGC, Tumkur
Contiguous Allocation:
If the blocks are allocated to the file in Continuous fashion in the hard disk then such allocation
scheme is known as contiguous allocation.
There are three files in the directory. The starting block and the length of each file are mentioned in
the File Allocation Table.
Advantages
1. It is simple to implement.
2. We will get Excellent read performance.
Disadvantages
1. External Fragmentation because even though sufficient memory block are appeared in disk
but will not allocate it to file.
2. It may be difficult to have a file grow.
Linked List Allocation:
Linked List allocation solves all problems of contiguous allocation. In linked list allocation, each
file is considered as the linked list of disk blocks. However, the disks blocks allocated to a
particular file need not to be contiguous on the disk.
12
Operating System VVFGC, Tumkur
Each disk block allocated to a file contains a pointer which points to the next disk block allocated to
the same file.
Advantages
1. There is no external fragmentation with linked allocation.
2. Any free block can be utilized in order to satisfy the file block requests.
3. File can continue to grow as long as the free blocks are available.
Disadvantages
1. Random Access is not provided.
2. Pointers require some space in the disk blocks.
3. Need to traverse each block.
Indexed Allocation Scheme:
Instead of maintaining a file allocation table of all the disk pointers, Indexed allocation scheme
stores all the disk pointers in one of the blocks called as indexed block. Indexed block doesn't hold
the file data, but it holds the pointers to all the disk blocks allocated to that particular file.
13
Operating System VVFGC, Tumkur
Advantages
1. Supports direct access
Disadvantages
1. A bad index block could cause the lost of entire file.
2. Size of a file depends upon the number of pointers, a index block can hold.
3. More pointer overhead.
Free Space Management
A file system is responsible to allocate the free blocks to the file therefore it has to keep track of all
the free blocks present in the disk.
There are mainly four approaches by using which, the free blocks in the disk are managed.
1. Bit Vector / Bit Map:
In this every block is represented by a bit either 0 or 1.
14
Operating System VVFGC, Tumkur
If the block is empty then the bit is 1 otherwise it is 0. Initially all the blocks are empty therefore
each bit in the bit map vector contains 1.
Ex:
1 0 0 1 1
0th 1st 2nd 3rd 4th
In the above Bit map 0th block, 3rd and 4th blocks are empty.
2. Linked List :
In this approach, the free disk blocks are linked together i.e. a free block contains a pointer to
the next free block.
The free space list head points to Block 5 which points to Block 6, the next free block and so on.
The last free block would contain a null pointer indicating the end of free list.
3. Grouping:
This approach stores the address “n-1” free blocks in the first free block.
The first free block stores the address of some, say n free blocks. Out of these n blocks, the first
n-1 blocks are actually free and the last block contains the address of next free n blocks.
15
Operating System VVFGC, Tumkur
An advantage of this approach is that the addresses of a group of free disk blocks can be found
easily.
For example we have 10 free blocks then 1 st free block only stores the address of other free
blocks.
4. Counting :
This approach stores one counter variable which specifies, after that block how many free
contiguous blocks are there.
From the above example total free blocks are 5 but at the first free block counter pointing to 4.
File System Structure
File System provide efficient access to the disk by allowing data to be stored, located and
retrieved in a convenient way.
A file System must be able to store the file, locate the file and retrieve the file.
Most of the Operating Systems use layering approach every layer of the file system is
responsible for some activities. Here it has 6 layers.
16
Operating System VVFGC, Tumkur
Application Programs:
These are developed by user with any extension like C files, java files word documents etc.
Logical File System:
Application programs will be given as an input for the logical file system.
It will check whether the file is present in Directory structure or not. If it present in Directory
structure then it will generates “logical block number for that file”.
File Organization Module:
Logical block number will be as an input for this layer.
It performs mapping or translation scheme in order to find “Physical block number” in which the
file is stored in hard disk.
Basic File System:
Once File organization module decided which physical block the application program needs,
it passes this information to basic file system. The basic file system is responsible for issuing the
commands to I/O control in order to fetch those blocks.
I/O controls contain the codes by using which it can access hard disk. These codes are known as
device drivers. I/O controls are also responsible for handling interrupts.
******************
17