0% found this document useful (0 votes)
7 views

File System

6f6f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

File System

6f6f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

File System

(It is a data structure that stores data and


information on storage devices, making them easily
retrievable)
File Concept
• File is a collection of related information that is recorded on secondary storage.
• From user’s perspective, a file is contiguous logical address space.
• Types: Files represent
• Data
• numeric
• character
• binary
• Program (both source and object forms)
• Types of information stored in a file – source programs, object programs,
executable programs, numeric data, text, graphic images, records, sound
recordings and so on.
• Contents of a file defined by file’s creator.
File Concept
• A file has a certain defines structure, which depends on its type.
Text file: sequence of characters organised into lines (and possible pages).
Source file: sequence of subroutines and functions, each of which is further
organized as declarations followed by executable statements.
Object file: sequence of bytes organised into blocks understandable by the
system’s linker.
Executable file: series of code (machine code) sections that the loader can bring
into memory and execute.
File Attributes
File attributes are a type of meta-data that describe and may modify how files
and/or directories in a filesystem behave.
Name – only information kept in human-readable form
Identifier – unique tag (number) identifies file within file system
Type – needed for systems that support different types
Location – pointer to file location on device
Size – current file size
Protection – controls who can do reading, writing, executing
Time, date, and user identification – data for protection, security, and usage
monitoring
• Information about files are kept in the directory structure, which is maintained on
the disk.
• Directory entry consists of the file’s name and its unique identifier. The identifier
in turn locates the other attributes of file.
File Operations
• File is an abstract data type
• The OS can provide system calls to perform operations on file.
Operations:
1. Creating file: Steps –
1. Space in the file system must be found for the file (allocate space for file)
2. Entry of the new file must be in directory
2. Writing file: To write a file-
1. Make a system call specifying both the name of the file and the
information to be written to the file.
2. Given the name of file, the system searches the directory to find the file’s
location.
3. System must keep a write pointer to the location of the file where the
next write is to take place. Write pointer updated whenever write occurs.
File Operations
3. Reading a file: To read from a file –
1. Make a system call specifying both the name of the file and where (in
memory) the next block of file should be put.
2. The directory is searched for the file and the system keeps a read pointer
to the location in the file where the next read is to take place.
3. Once the read has taken place the read pointer is updated.
4. Repositioning within file:
• Directory is searched for the entry and current-file-position pointer is
repositioned to a given value.
• This operation is also known as a file seek.
5. Deleting file: To delete a file,
1. Search the directory for the named file.
2. If file found, release all file space, so that it can be reused by other files
and erase the directory entry.
File Operations
6. Truncating file:
• Rather than deleting the file and then recreate it, this function allows all
attributes to remain unchanged – except file length.
• The file size be reset to zero and its file space released.
Other common operations:
1. Appending new information to the end of an existing file.
2. Renaming an existing file
• Most of the file operations involve searching the directory for the entry
associated with the named file.
• To avoid this constant searching, system call open() can be made before a file is
first used.
• The OS keeps a table, called open-file table, containing information about all
open files.
• When a file operation is requested, the file is specified via an index into this table,
so no searching is required.
• When the file is no longer being used, it is closed by the process making system
call close() and the OS removes its entry from the open-file table.
Open Files
• The implementation of the open() and close() operations is more complicated,
when several processes open the file simultaneously.
• For this OS uses two levels of internal open-file tables: Per-process table
System-wide table
Per-process table:
• Tracks all file that a process has open.
• Stores information regarding the use of the file by the process such as current-
file pointer of each file and access rights to the file.
• Each entry in this table in turn points to the system-wide open-file table.
System-wide table:
• Contains process independent information, such as the location of the file in
the disk, access dates and file size.
• Once the file has been opened by one process, the system-wide table includes an
entry for the file.
Open Files
• When another process executes an open () call, a new entry is simply added to the
process’s open-file table pointing to the appropriate entry in the system-wide table.
• Open-file table also has open count associated with each file to indicate how many
processes have the file open.
• Each close() decreases this open count and when this open count reaches zero, the
file is no longer in use and file’s entry is removed from the open-file table.
Information associated with an open file:
File pointer: System track the last read-write location as a current-file-position
pointer. This pointer is unique to each process operating on the file.
File-open count: Tracks the number of opens and closes and reaches zero on the last
close. The system can then remove entry.
Disk location of the file: The information needed to locate the file on disk is kept in
memory so that system does not have to read it from the disk for each operation.
Access rights: Each process opens a file in an access mode.
This information used by OS so that it can allow or deny subsequent I/O operations.
File Types
• Type of file is part of file name. File name split into 2 parts – name and extension,
usually separated by period character.
• System uses extension to indicate the type of file and operations on the file.
Access Methods
• Information stored in the file can be accessed in several ways.
• The way of accessing and manipulating data stored in a file is called access method.
• There are three ways to access a file into a computer system: Sequential-Access,
Direct Access, Index sequential Method.
1. Sequential Access:
• The simplest method and information in the file is processed in order, one record
after the other.
• A read operation – read next – reads the next portion of the file and automatically
advances file pointer.
• Similarly, for the -write next- append to the end of the file and advance to the newly
written material.
• Sequential access file can be reset to the beginning and able to skip forward or
backward n records for n = 1.
• This method is based on a tape model
of a file and works well on sequential
access devices.
Access Methods
2. Direct Access:
• Also known as relative access method.
• A file is made up of fixed-length logical records that allows the program to read
and write record rapidly in no particular order.
• The direct access is based on the disk model of a file since disk allows random
access to any file block.
• 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.
• For this method the file operations must be modified to include the block
number as a parameter. Thus in read n and write n , n is block number, rather
than read next and write next respectively.
• A block number provided by the user to the operating system is normally
a relative block number, the first relative block of the file is 0 and then 1 and so
on.
Access Methods
3. Index Sequential Access:
• It is built on top of direct access method.
• This method construct an index for the file. The index, contains the pointer to the
various blocks.
• To find a record in the file, first search the index, and then by the help of pointer
access the file directly.
•With large files, the index file itself
may become too large to be kept in
memory.
•One solution is to create an index for
the index file.
•The primary index file would contain
pointers to secondary index files,
which would point to the actual data
items.
Comparison of Access Methods
• Comparison of advantages and disadvantages
Sequential Access
Advantages − Simple and easy to implement, suitable for storing large amounts of data,
requires less memory.
Disadvantages − Not efficient for accessing specific data or making changes to the data,
slow for reading or writing data in the middle of the file.
Direct Access
Advantages − Provides fast and efficient access to specific data within the file and
suitable for devices that require fast access to specific data.
Disadvantages − Requires knowledge of the physical layout of the data within the file,
may require special hardware or software to access the data directly, gaps can be left in
the file which can impact performance.
Indexed access
Advantages − Provides fast and efficient access to files by name or attributes, making it
suitable for applications that require searching and retrieving specific files quickly.
Disadvantages − The index must be maintained, which can require additional disk space
and processing time.
Directory Structure
• Directory can be defined as the listing of the related files on the disk.
• A directory can be viewed as a file which contains the meta data of the bunch of
files.
• The directory may store some or the entire file attributes.
• To get the benefit of different file systems on the different operating systems, a
hard disk can be divided into the number of partitions of different sizes. The
partitions are also called volumes or mini disks.
• Each partition must have at least one directory in which, all the files of the
partition can be listed.
• A directory entry is maintained for each file in the directory which stores all the
information related to that file.
Directory Structure
Directory Structure
• Every Directory supports a number of common operations on the file:
1.File Creation: New file need to be created and added to the directory
2.Search for the file: Search a directory structure to find entry for a particular file.
3.File deletion: When a file is no longer needed, it can be deleted from directory.
4.Renaming the file: When the contents or use of the file changes, file name can
be changed in the directory.
5.Traversing File System: Every directory and every file within directory structure
can be accessed.
6.Listing of files: List the files in directory and the contents of the directory entry
for each file in the list.
• There are several logical structures of a directory, these are
1. Single - Level Directory
2. Two - Level Directory
3. Tree - Structured/ Hierarchical – Structured Directory
4. Acyclic-Graph Directory
Single - Level Directory
• The single-level directory is the simplest directory structure.
• In it, all files created by several users, are contained in the same directory(root
directory) which makes it easy to support and understand.
• The users are not allowed to create subdirectories under the root directory.
• 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 a unique name.
• If two users call their dataset test, then the unique name rule violated.
• Even a single user on single level directory may find difficult to remember the
names of all the files as the number of increases.
• Keeping track of so many files is a difficult task.
• There is no way to segregate important files from non-important files.
Single - Level Directory

• 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 have the same name.
• Searching will become time taking if the directory is large.
• This can not group the same type of files together.
Two - Level Directory Structure
• 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 MFD is indexed by username or account number, and each entry points to the
UFD for that user.
Two - Level Directory Structure
Advantages:
• The main advantage is there can be more than two files with same name, and
would be very helpful if there are multiple users.
• A security would be there which would prevent user to access other user’s
files.
• Searching of the files becomes very easy as only one user's list needs to be
traversed.
Disadvantages:
• As there is advantage of security, there is also disadvantage that the user
cannot share the file with the other users.
• Unlike the advantage users can create their own files, users don’t have the
ability to create subdirectories.
Tree – Structured Directory
• Tree directory structure of operating system is most commonly used in personal
computers.
• User can create files and subdirectories too, which was a disadvantage in the
Two-level directory structures.
• This directory structure resembles a real tree upside down, where the root
directory is at the peak. This root contains all the directories for each user.
• The users can create subdirectories and even store files in their 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.
Advantages:
• This directory structure allows subdirectories inside a directory.
• The searching is easier, because in this, we can use both types of paths, which
are the absolute path and relative path.
• File sorting of important and unimportant becomes easier.
• This directory is more scalable than the other two directory structures
explained.
Tree – Structured Directory
• Disadvantages:
• As the user isn’t allowed to access other user’s directory, this prevents the file
sharing among users.
• It is not efficient because, as the user has the capability to make
subdirectories, if the number of subdirectories increase the searching may
become complicated.
• Users cannot modify the root directory data.
• If files do not fit in one, they might have to be fit into other directories.
Acyclic – Graph Directory
• Acyclic directories are a generalization of the Tree directory structure.
• Acyclic directories allow files to have multiple parent directories; this means that
multiple users can access the same file from different paths.
• If any user makes a change, it would be reflected to all the users.
• However, it is important to note that the shared file is not a copy of the same file
in two different directories.
Acyclic – Graph Directory
• It is designed in a way that multiple directories point to a particular directory or
file with the help of links or aliases.
• Link is a pointer to another file or subdirectory.
• There are two types of links : Hard link and soft link
Hard Link :
• A hard link acts as a copy (mirrored) of the selected file. It accesses the data
available in the original file.
• If the earlier selected file is deleted, the hard link to the file will still contain
the data of that file.
Soft Link :
• A soft link (also known as Symbolic link) acts as a pointer or a reference to the
file name. It does not access the data available in the original file.
• If the earlier file is deleted, the soft link will be pointing to a file that does not
exist anymore.
• Thus acyclic directories allow file sharing and make it easier to search for files.
Acyclic – Graph Directory
The deletion of files is complicated in acyclic directories. There are two ways in
which a file can be deleted:
1. Delete the file when any user deletes the file.
• This may lead to dangling pointers.
• The situation may become worse if the deleted space is reallocated to
some new file, and the dangling pointers may now point to an arbitrary
location in the new files.
2. Delete the link to the file.
• This mechanism needs some way of storing the list of links to the file. This
list may become extremely large, so a better method is to keep a count of
the links to the file.
• Whenever a link is deleted, the count is decremented, and when a new link
is created, the count is incremented.
• The file can be deleted when the count becomes zero.
Acyclic – Graph Directory
Advantages :
• The sharing of files is possible.
• Because of different-different paths, searching is easy.
Disadvantages:
• If the files are shared through linking, there may be a problem in the case of
deleting.
• If we are using soft link, then in this case, if the file is deleted then there is
only a dangling pointer which is left.
• If we are using hard link, in this case, when we delete a file, then we also
have to remove all the reference connected with it.

You might also like