Unit 6 (22516)
Unit 6 (22516)
Unit 6 (22516)
Prepared by:
Mrs. Kousar Ayub A.
Lecturer(Selection Grade)
Computer Engg. Dept
M. H. Saboo Siddik Polytechnic
1
Learning Outcomes
2
File
• A file is a named collection of related information that is recorded on
secondary storage such as magnetic disks, magnetic tapes and optical
disks.
• 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 location of file then 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.
System needs to keep a read pointer to location in the file where the next read
is to take place. Once the read has taken place, the read pointer is updated. 5
File Operations
• Repositioning within a file: The directory is searched for the appropriate entry,
and the current file position is set to a given value. Repositioning within a file
does not need to involve any actual I/O. This file operation is also known as a
file seeks.
• 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 and erase
the directory entry.
• Truncating a file: Instead of deleting a file and then recreate it, this function
allows all attributes to remain unchanged but for the file to be reset to length
zero. User wants to erase the contents of the file.
7
Serial File
• The least complicated form of file
organization is the serial file or
pile. Data are collected in the
order in which they arrive.
9
Sequential File Access
• The simplest access method is sequential access. Information in the file is
processed in order, one record after the other.
• The bulk of the operations on a file are reads and writes. The read and write
operations on the sequential file are done in sequential order.
• A read operation reads the next portion of the file and automatically advances a
file pointer, which tracks the I/O location. Similarly a write appends to the end of
the file and advances to the end of the newly written material.
• Such a file can be reset to the beginning and on some systems a program may be
able to skip forward or backward ‘n’ records for some integer ‘n’.
10
Sequential File Access
• Advantages of Sequential File:
• 1. Easy to access the next record.
• 2. Data organization is very simple.
• 3. Absence of data structures.
• 4. They are easily stored on tapes as well as disks.
• 5. Automatic backup copy is created.
• With large files, the index file itself may become too large to be kept
in memory. One solution is then to create an index for the index file.
The primary index file would contain pointers to secondary index
files, which then point to the actual data items.
12
Index Sequential File
• Advantages:
• Variable length records are allowed.
• Indexed sequential file may be updated in sequential or random mode
• Very fast operation
• Disadvantages:
• The major disadvantage of the index sequential file is that as the file grows,
a performance deteriorates rapidly because of overflows and consequently
there arises the need for periodic reorganization. Reorganization is an
expensive process and the file becomes unavailable during reorganization.
• When a new record is added to the main file, all of the index files must be
updated.
• Consumes large memory space for maintaining index files.
13
Direct Access
• For direct access the file is viewed as a number sequence of blocks of
records. A block is generally a fixed length quantity, defined by operating
system. A block may be a byte 512 words, 1024 bytes or some other
quantity, depending upon the system.
• A direct access file allows arbitrary blocks to be read or written. Thus we
may read block 14, then read block 50 and then write block 7. There are
no restrictions on the order of reading or writing for a direct access file.
• Direct access files are of great use for immediate access to large
amounts of information. When a query concerning a particular subject
arrives, we compute which block contains the answer and then read the
block directly to provide the desired information. Not all the operating
system support both sequential and direct access of files. Some systems
allow only sequential file access, others allow only direct access.
14
Direct Access
16
Hashing
• When they switched to the next user, the current contents of user
memory were written out to a backing store (a disk or drum) and the
memory of the next user was read in. This scheme is called Swapping.
18
Swapping
19
Allocation methods
• From the user’s point of view, a file is an abstract data type. It
can be created, opened, written, read, closed and deleted
without any real concern for its implementation. The
implementation of a file is a problem for the operating system.
• The main problem is how to allocate space to these files so that
disk space is effectively utilized and files can be quickly accessed.
• Three major methods of allocating disk space are in wide use:
1. Contiguous
2. Linked
3. Indexed
• Each method has its advantages and disadvantages.
20
Contiguous Allocation
• The contiguous allocation method requires each file to occupy a set of
contiguous addresses on the disk. Disk addresses define a linear ordering on the
disk. Contiguous allocation of a file is defined by the disk address of the first
block and its length. If the file is ‘n’ blocks long and starts at location ‘b’, then it
occupies blocks b, b+1, b+2, - - - - - b+n-1. The directory entry for each file
indicates the address of the starting block and the length of the area allocated
for this file.
• Contiguous allocation supports both sequential and direct access.
• For direct access to block ‘i’ of a file, which starts at block ‘b’, we can
immediately access block b+i. The difficulty with contiguous allocation is finding
space for a new file.
• For direct access to block ‘i’ of a file, which starts at block ‘b’, we can
immediately access block b+i.
• The difficulty with contiguous allocation is finding space for a new file.
• If file to be created are ‘n’ blocks long, we must search free space list for ‘n’ free
contiguous blocks.
21
Contiguous Allocation
22
Contiguous Allocation
• Advantages of Contiguous File Allocation Method:
• 1. Supports both sequential and direct access methods.
• 2. Contiguous allocation is the best form of allocation for sequential files. Multiple
blocks can be
• brought in at a time to improve I/O performance for sequential processing.
• 3. It is also easy to retrieve a single block from a file. For example, if a file starts at block
‘n’ and the ith block of the file is wanted, its location on secondary storage is simply n +
i.
• 4. Reading all blocks belonging to each file is very fast.
• 5. Provides good performance.
25
Linked File Allocation (Chained File
Allocation)
Advantages of Linked File Allocation Method:
1. Any free blocks can be added to a chain.
2. There is no external fragmentation.
3. Best suited for sequential files that are to be processed sequentially.
4. No need to know the size of the file in advance.
5. No need to compact or relock files.
26
Indexed File Allocation
• Linked allocation solves the external fragmentation and size declaration
problems of contiguous allocation. However linked allocation cannot
support direct access, since the blocks are scattered all over the disk.Mostly
pointers to blocks are scattered all over the disk.
• Indexed allocation solves this problem by bringing all of the pointers
together into one location the Index Block.
• Each file has its own index block, which is an array of disk block addresses.
The ith entry in the index block points to the ith block of the file. The
directory contains the address of the index block.
• To read the ith block we use pointer in ith index block entry to find and read
the desired block. When the file is created, all pointers in the index block are
set to nil. When the ith block is first written a lock is removed from the free
space list and its address is put in the ith index block entry.
27
Indexed File Allocation
• Indexed allocation supports direct access, without
suffering from external fragmentation. Indexed
allocation does suffer from wasted space. The
pointer overhead of index block is worse than
pointer over head of linked allocation. Assume we
have a file of only one or two blocks with linked
allocation we only lose the space of one pointer per
block. With indexed allocation an index block must
be allocated even if only one or two pointers will be
non-nil.
28
Indexed File Allocation
29
Indexed File Allocation
Advantages of Indexed File Allocation Method:
1. Does not suffer from external fragmentation.
2. Support both sequential and direct access to the file.
3. No need for user to know size of the file in advance.
4. Entire block is available for data as no space is occupied by pointers.
30
Single Level Directory Structure
• It is the simplest form of directory system is having one directory
containing all the files. Sometimes, it is called the root directory.
• In single level directory structure, the entire files are contained in the
same directory. So unique name must be assigned to each file of the
directory
• The world’s first supercomputer, the CDC 6600, had only a single
directory for all files, even though it was used by many users at once.
31
Single Level Directory Structure
32
Two Level Directory Structure
• The structure of two level directory structure is divided into two levels of
directories namely, a master directory and user directories. The user directories
are the sub-directories of the master directory.
• The user directory represents a list of files of a specific user. In this directory
structure, each users has its private directory known as User File Directory
(UFD).
34
Hierarchical Directory Structure (Tree
Structure)
• The two level hierarchies eliminate name conflicts
among users but are not satisfactory for users with a
large number of files. We needed general hierarchy
i.e., a tree of directories.
• The tree structured directory structure, allows users
to create their own subdirectory and to organize
their files accordingly. A subdirectory contains a set
of files or subdirectories. A directory is simply
another file, but it is treated in a special way.
35
Hierarchical Directory Structure (Tree
Structure)
36
Disk management in Linux
37
Disk Management in Windows OS
38
Measurement of speed of the disk
• Transfer Rate is the rate at which the data moves from disk to the
computer.
• Random Access Time is the sum of the seek time and rotational latency.
• The seek time is the time for the disk arm to move the head to the
required cylinder containing the desired track.
• The rotational latency is the additional time for the disk to rotate the
desired sector to the disk head.
40