OS Chap 2
OS Chap 2
MANAGEMENT
• A file is a named collection of related information that is recorded on
secondary storage such as magnetic disks, magnetic tapes and optical
disks.
• • In general, a file is a sequence of bits, bytes, lines or records whose
meaning is defined by the file's creator and user.
Attributes of a File
• Name : It is the only information which is in human-readable form.
• Identifier: The file is identified by a unique tag(number) within file
system.
• Type: It is needed for systems that support different types of files.
• Location: Pointer to file location on device.
• Size: The current size of the file.
• Protection : This controls and assigns the power of reading, writing,
executing.
• Time, date, and user identification: This is the data for protection,
security, and usage monitoring.
File Operations
• Creating a file: Two steps are necessary to create a file. First, space in the file system
must be found for the file. Second, an entry for the new file must be made in the
directory.
• Writing a file: To write a file, we make a system call specifying both the name of the
file and the information to be written to the file. Given the name of the file, the system
searches the directory to find the file's location. The system must keep a write pointer to
the location in the file where the next write is to take place. The write pointer must be
updated whenever a write occurs.
• Reading a file: To read from a file, we use a system call that specifies the name of the
file and where (in memory) the next block of the file should be put. Again, the directory
is searched for the associated entry, and the system needs to keep a read pointer to the
location in the file where the next read is to take place. Once the read has taken place,
the read pointer is updated.
• Repositioning within a file: The directory is searched for the appropriate entry, and
the current-file-position pointer is repositioned to a given value. Repositioning within a
file need not involve any actual I/O. This file operation is also known as a file seek.
• Deleting a file. To delete a file, we search the directory for the named
file. Having found the associated directory entry, we release all file
space, so that it can be reused other files, and erase the directory
entry
• Protection: Access-control information determines who can do
reading, writing, executing, and so on.
• Truncating a file: The user may want to erase the contents of a file
but keep its attributes. Rather than forcing the user to delete the file
and then recreate it, this function allows all attributes to remain
unchanged—except for file length—but lets the tile be reset to length
zero and its file space released.
Common operations
Open
Close
Append
Rename
Copy
Access Methods
• Types:
1.Single-level directory
2.Two-level directory
3.Tree-structured directory
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 files are smaller in size, searching will 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 cannot have
the same name.
• Searching will become time taking if directory will large.
• In this cannot group the same type of files together
Two-level directory
• As, a single level directory often leads to confusion of files names among
different users hence 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 MFD is indexed by username or account number, and each entry
points to the UFD for that user
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.
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 their own subdirectories
and to organize on their files accordingly.
• 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 cannot share files.
• It is inefficient, because accessing a file may go under multiple
directories
Allocation Methods
• Whenever a hard disk is formatted, a system has many
small areas called blocks or sectors that are used to store
any kind of file.
• File allocation methods are different ways by which the
operating system stores information in memory blocks, thus
allowing the hard drive to be utilized effectively and the file
to be accessed.
• Below are the types of file allocation methods in the
Operating System.
1.Contiguous File allocation
2.Linked File Allocation
3.Indexed File Allocation
Contiguous Allocation
• If the blocks are allocated to the file in such a way that all the logical blocks
of the file get the contiguous physical block in the hard disk then such
allocation scheme is known as contiguous allocation
Advantages
• It is very easy to implement.
• There is a minimum amount of seek time.
• The disk head movement is minimum.
• Memory access is faster.
• It supports sequential as well as direct access.
Disadvantages
• At the time of creation, the file size must be initialized.
• As it is pre-initialized, the size cannot increase.
Linked Allocation
• This method does not suffer from external fragmentation. This makes it relatively
better in terms of memory utilization.
• 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.
Indexed File Allocation