File System in OS
File system is part of OS. It maintains the files at hard disk. A file is a collection of sequence records in which are stored at Hard disk. Sequence of record means, the sequence of bits, bytes, functions and lines or records. When OS creates different files then it also generates the code to understand these file structure.
File Structure and its types
A File Structure must be according to format that the OS can understand. Different files and their structures are
i. Text file: It is a sequence of characters which are organized into lines.
ii. Source file: It is a sequence of procedures and functions.
iii. Object file: It is a sequence of bytes organized into blocks that are understandable by the machine.
Some operating systems support less number of file structures like UNIX and MS-DOS.
File Types in MS DOS and UNIX
MS-DOS and UNIX OS has the following types of files.
i. Ordinary files: These files contain the user information and these information’s are in the form of text, databases or executable program. User can apply the following operations on such files
- Remove the entire file.
ii. Directory files: Directory files contains list of file names and other information related to these files which are stored in the directory.
iii. Special files: Special files contain the information’s about physical devices like disks, printers, networks, terminals etc.
Special files are further divided into two types
i. Character special files: The data in character special files is handled character by character as in case of printers or terminals.
ii. Block special files: The data in block special files is handled in blocks as in the case of Hard disk.
Attributes of the File
i. Name: Each file has a specific name to recognize this file in the file system. Two or more files with the same name cannot present in one directory.
ii. Identifier: Along with the name, Every File has its own extension which indicates the type of the file. Let suppose, in the filename “xyz.txt,” the xyz is the filename and .txt is the file extension. Other file extensions may be like that
|Image||.jpg, .png, .jpeg|
iii. Type: Files are categorized into different types such as text, audio, video, database, system, executable etc. files.
iv. Location: As we know, the secondary storage is divided into large numbers of sectors. In the same way, file is also divided into several blocks. The size of file-block is equal to sector size of secondary memory, so that file can easily accommodate on secondary storage. Each sector on secondary memory is considered as a location, to store the blocks of the files.
v. Size: Size of the file, is the capacity of file which it require to accommodate on secondary storage.
vi. Protection: A protection on file is a permission which is applied by admin for different group of Users to access the system.
vii. Time and Date: Every file contains the time and its date on which the file is created and last modified.
Operations on the File
File system in OS performs several operations on the file. All common operation on file are.
i. Create: In file system, creating a file is the basic task on the file. Different types of files are created for different purpose as,
- Word processors are used to create a word file
- Image editors are used to create the image files.
- Text editors are used to create a text file.
ii. Write: The OS use the Pointers to find the required position in the file from where the data needs to be written.
iii. Read: Files can be opened in three ways
A Read pointer is used to read the required file.
iv. Re-position: According to the user’s requirement, Moving pointers forward or backward in file the file system is called re-positioning. As in repositioning OS is trying to find the actual address of file so It is also known as seeking.
v. Delete: Deleting the file means delete the file along with its all attributes. Allocated space to the file is also becomes available and now it can be allocated to any other file.
vi. Truncate: Truncating the file means, deleting the file without deleting its attributes. After Truncating the file, the attributes of the file is still present in the storage location.
File Access Methods
The way in which the records of a file are accessed is called file access mechanisms. There are several ways to access files
- Sequential access
- Direct/Random access
- Indexed sequential access
i. Sequential access
Most of the files such as text files, audio files, video files, etc are need to be accessed sequentially because in sequential access method, the operating system reads the file word by word. As if there are 100 numbers of instructions and OS wants to access the last block then OS has to access first 1 to 99 blocks to get last block (100) in file system.
And in files like text, audio, video, OS require very next word after accessing the previous. So, sequential access is method is used.
Compilers also use this access method. Tape drive is a common example of sequential access.
ii. Direct/Random access
Random access is a method in which the records are accessed directly. In random access method there is no need to access the data sequentially. It is mostly used where the filtered information’s are required i.e. database. As in database mostly the filtered records are accessed so, sequential access will be slow procedure in such cases.
As if there are 100 numbers of blocks and OS wants to access the last one then OS can directly access it, without to access first 1 to 99 blocks in file system.
iii. Indexed sequential access
This method is built up on base of sequential access. In index sequential access, a file can be sorted on any sector/block at secondary memory. Then an index block is created having all pointers (gives the actual address) to files. Each file has its own index block which is also stored in a sector of secondary memory, to access any particular block from the file.
If the file size is too large then number of indexes for each block of file is also increased. Due to which indexes of whole file cannot be stored in the single sector of secondary storage. In such cases, multilevel indexing comes to occur.
In indexed accessing, searching in the large database is very fast and easy to implement. But it requires some extra space in the memory to store the index value.
Note: Some modern system use the method of direct access and indexed access but sequential access is also used in some case where the need is to access the data sequentially i.e. compilers.
What is a directory?
Directory contains the list of related files and exists at secondary storage (HD). Some or the entire attributes of file also stored in the directory.
A hard disk can be divided into the number of partitions of different sizes. The partitions are also known as volumes. Like Volume C, Volume D etc.
Each Volume or Directory must have at least one directory to list the number of files in it.
Directory contains number of entries. Each entry represent a file with it’s attributes as shown in the following diagram
Data inside the directory is also known as metadata of directory.
Operations on Directory
- File Creation
- Search for the file
- File deletion
- Renaming the file
- Traversing Files
- Listing of files