Os Unit 5
Os Unit 5
File System: The Concept of a File- file attributes, file operations, file types, file structures; Access
Methods- sequential access, direct access, other access methods, Directory Structure single level
directory, two level directories, tree structured directory, general graph directory; File Sharing-
multiple users, remote file systems, Protection- types of access, access control.
Implementing File System: File System Structure. File System Implementation- overview, partitions
and mounting, virtual file systems, Allocation Methods- contiguous allocation, linked allocation,
indexed allocation; Free-Space Management- linked list, grouping, counting.
Disk Scheduling: FCFS Scheduling, SSTF Scheduling, SCAN Scheduling, C-SCAN scheduling,
LOOK Scheduling
FILE SYSTEM: FILE CONCEPT
A file is a collection of similar records. The data can’t be written on to the secondary
storage unless they are within a file. Files represent both the program and the data. Data can be
numeric, alphanumeric, alphabetic or binary. Many different types of information can be stored on
a file ---Source program, object programs, executable programs, numeric data, payroll recorder,
graphic images, sound recordings and so on.
A file has a certain defined structures according to its type:
1 Text file:-Text file is a sequence of characters organized in to lines.
2 Object file:-Object file is a sequence of bytes organized in to blocks understandable by the
systems linker.
3 Executable file:-Executable file is a series of code section that the loader can bring in to
memory and execute.
4 Source File:-Source file is a sequence of subroutine and function, each of which are further
organized as declaration followed by executable statements.
File Attributes:-File attributes varies from one OS to other. The common file attributes are:
1 Name:-The symbolic file name is the only information kept in human readable form.
2 Identifier:-The unique tag, usually a number, identifies the file within the file system. It is the
non- readable name for a file.
3 Type:-This information is needed for those, whose system that supports different types.
4 Location:-This information is a pointer to a device and to the location of the file on that
device.
5 Size:-The current size of the file and possibly the maximum allowed size are included in this
attribute.
6 Protection:-Access control information determines who can do reading, writing, execute and
so on.
7 Time, data and User Identification:-This information must be kept for creation, lastmodification
and last use. These data are useful for protection, security and usage monitoring. File Operations:-
File is an abstract data type. To define a file we need to consider the operation that can be
performed on the file.
Basic operations of files are:
1. Creating a file:-Two steps are necessary to create a file. First space in the file system for file is
found. Second an entry for the new file must be made in the directory. The directory entry records
the name of the file and the location in the file system.
2. Writing a file:-System call is mainly used for writing in to the file. System call specify the name
of the file and the information i.e., to be written on to the file. Given the name the system search
the entire directory for the file. The system must keep a write pointer to the location in thefile
where the next write to be taken place.
3. Reading a file:-To read a file system call is used. It requires the name of the file and the memory
1|P a g
Unit – VI Operating Systems
address. Again the directory is searched for the associated directory and system must maintain a
read pointer to the location in the file where next read is to take place.
4. Delete a file:-System will search for the directory for which file to be deleted. If entry is found
it releases all free space. That free space can be reused by another file.
5. Truncating a file:-User may want to erase the contents of the file but keep its attributes. Rather
than forcing the user to delete a file and then recreate it, truncation allows all attributes to remain
unchanged except for file length
6. Repositioning within a file:-The directory is searched for appropriate entry and the current file
position is set to a given value. Repositioning within a file does not need to involve actual i/o. The
file operation is also known as file seeks.
In addition to this basis 6 operations the other two operations include appending new
information to the end of the file and renaming the existing file. These primitives can be combined
to perform other two operations. Most of the file operation involves searching the entire directory
for the entry associated with the file. To avoid this OS keeps a small table containing information
about an open file (the open table). When a file operation is requested, the file is specified via
index in to this table. So searching is not required. Several piece of information are associated with
an open file:
File pointer:-on systems that does not include offset an a part of the read and write system
calls, the system must track the last read-write location as current file position pointer. This
pointer is unique to each process operating on a file.
File open count:-As the files are closed, the OS must reuse its open file table entries, or it
could run out of space in the table. Because multiple processes may open a file, the system
must wait for the last file to close before removing the open file table entry. The counter
tracks the number of copies of open and closes and reaches zero to last close.
Disk location of the file:-The information needed to locate the file on the disk is kept in
memory to avoid having to read it from the disk for each operation.
Access rights:-Each process opens a file in an access mode. This information is stored on
per-process table the
In addition to this basis 6 operations the other two operations include appending new
information to the end of the file and renaming the existing file. These primitives can be combined
to perform other two operations. Most of the file operation involves searching the entire directory
for the entry associated with the file.
To avoid this OS keeps a small table containing information about an open file (the open
table). When a file operation is requested, the file is specified via index in to this table. So searching
is not required.
File Types
When we design a file system—indeed, an entire operating system—we always consider whether the operating
system should recognize and support file types. A common technique for implementing file types is to include
the type as part of the file name. The name is split into two parts—a name and an extension, usually separated
by a period as shown in Figure 1. In this way, the user and the operating system can tell from the name alone
what the type of a file is.
2|P a g
Unit – VI Operating Systems
File Structure
File types also can be used to indicate the internal structure of the file. Source and object files have structures
that match the expectations of the programs that read them. Further, certain files must conform to a required
structure that is understood by the operating system. For example, the operating system requires that an
executable file have a specific structure so that it can determine where in memory to load the file and what the
location of the first instruction is.
This point brings us to one of the disadvantages of having the operating system support multiple file structures:
it makes the operating system large and cumbersome. If the operating system defines five different file
structures, it needs to contain the code to support these file structures. In addition, it may be necessary to define
every file as one of the file types supported by the operating system.
2. ACCESS METHODS
The information in the file can be accessed in several ways. Different file access methods are:
1. Sequential Access: Sequential access is the simplest access method. Information in the
file is processed in order, one record after another. Editors and compilers access the files in this
fashion. Normally read and write operations are done on the files. A read operation reads the next
portion of the file and automatically advances a file pointer, which track next i/o location. Write
operation appends to the end of the file and such a file can be next to the beginning. Sequential
access depends on a tape model of a file as shown in Figure 2.
Eg:-User may need block 13, then read block 99 then write block 12. For searching the records
in large amount of information with immediate result, the direct access method is suitable. Not
all OS support sequential and direct access. Few OS use sequential access and some OS uses
direct access. It is easy to simulate sequential access on a direct access but the reverse is
extremely inefficient. Simulation of sequential access on a direct-access file is shown in figure 3.
3. DIRECTORY STRUCTURE
The files systems can be very large. Some systems stores millions of files on the disk. To
manage all this data we need to organize them.
This organization is done in two parts:
1. Disks are split in to one or more partition also known as minidisks.
2. Each partition contains information about files within it. This information is kept in
entries in a device directory or volume table of contents. The device directory or simple directory
records information as name, location, size, type for all files on the partition. The directory can be
viewed as a symbol table that translates the file names in to the directory entries. The directory
itself can be organized in many ways.
When considering a particular directory structure, we need to keep in mind the operations
that are to be performed on a directory.
Search for a file:-Directory structure is searched for finding particular file in the directory.
Files have symbolic name and similar name may indicate a relationship between files, we
may want to be able to find all the files whose name match a particular pattern.
Create a file:-New files can be created and added to the directory.
Delete a file:-when a file is no longer needed, we can remove it from the directory.
List a directory:-We need to be able to list the files in directory and the contents of the
directory entry for each file in the list.
Rename a file:-Name of the file must be changeable when the contents or use of the file is
changed. Renaming allows the position within the directory structure to be changed.
Traverse the file:-it is always good to keep the backup copy of the file so that or it can be
used when the system gets fail or when the file system is not in use.
I. Single-level directory: This is the simplest directory structure. All the files are contained in
the same directory which is easy to support and understand as shown in Figure 5.
5|P a g
Unit – VI Operating Systems
To create a file for a user, OS searches only those users UFD to ascertain whether anotherfile of that
name exists.
To delete a file checks in the local UFD so that accidentally delete another user’s file
with the same name
6|P a g
Unit – VI Operating Systems
Path name can be of two types: a. Absolute path name:-Begins at the root and follows a
path down to the specified file, giving the directory names on the path. b. Relative path name:-
Defines a path from the current directory. One important policy in this structure is low to handle
the deletion of a directory.
a. If a directory is empty, its entry can simply be deleted.
b. If a directory is not empty, one of the two approaches can be used.
i. In MS-DOS, the directory is not deleted until it becomes empty.
ii. In UNIX, RM command is used with some options for deleting directory.
IV. Acyclic graph directories: It allows directories to have shared subdirectories and files as
shown in Figure 8. Same file or directory may be in two different directories. A graph with no
cycles is a generalization of the tree structure subdirectories scheme. Shared files and
subdirectories can be implemented by using links. A link is a pointer to another file or a
subdirectory. A link is implemented as absolute or relative path. An acyclic graph directory
structure is more flexible then is a simple tree structure but sometimes it is more complex.
8|P a g
Unit – VI Operating Systems
The file allocation table entry for each file indicates the address of starting block and the
length of the area allocated for this file. Contiguous allocation is the best from the point of view of
individual sequential file. It is easy to retrieve a single block. Multiple blocks can be broughtin
one at a time to improve I/O performance for sequential processing. Sequential and direct access
can be supported by contiguous allocation. Contiguous allocation algorithm suffers from external
fragmentation. Depending on the amount of disk storage the external fragmentation can be a major
or minor problem. Compaction is used to solve the problem of external fragmentation. The
following figure shows the contiguous allocation of space after compaction. The original disk was
then freed completely creating one large contiguous space. If the file is n blocks long and starts at
location b, then it occupies blocks b, b+1, b+2… ........................................................ b+n
Advantages:
Supports variable size problem.
Easy to retrieve single block.
Accessing a file is easy.
It provides good performance.
Disadvantage:
Pre-allocation is required.
It suffers from external fragmentation.
2. Linked Allocation: It solves the problem of contiguous allocation. This allocation is on the
basis of an individual block. Each block contains a pointer to the next block in the chain as shown
in Figure 10. The disk block can be scattered anywhere on the disk. The directory contains a pointer
to the first andthe last blocks of the file.
9|P a g
Unit – VI Operating Systems
Free-Space Management
Since storage space is limited, we need to reuse the space from deleted files for new files, if possible. (Write-
once optical disks allow only one write to any given sector, and thus reuse is not physically possible.) To keep
track of free disk space, the system maintains a free-space list. The free-space list records all free device
blocks— those not allocated to some file or directory
1 Bit Vector
Frequently, the free-space list is implemented as a bitmap or bit vector. Each block is represented by 1 bit. If
the block is free, the bit is 1; if the block is allocated, the bit is 0.
For example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 are free and
the rest of the blocks are allocated. The free-space bitmap would be 001111001111110001100000011100000
.
The main advantage of this approach is its relative simplicity and its efficiency in finding the first free block
or n consecutive free blocks on the disk.
2 Linked List
Another approach to free-space management is to link together all the free blocks, keeping a pointer to the first
free block in a special location in the file system and caching it in memory. This first block contains a pointer
to the next free block, and so on. Recall our earlier example in bit vector, in which blocks 2, 3, 4, 5, 8, 9, 10,
11, 12, 13, 17, 18, 25, 26, and 27 were free and the rest of the blocks were allocated. In this situation, we would
keep a pointer to block 2 as the first free block. Block 2 would contain a pointer to block 3, which would point
to block 4, which would point to block 5, which would point to block 8, and so on as shown in following figure
12.
13 | P a g
Unit – VI Operating Systems
Fig: SCAN disk scheduling
Let’s take a example 98, 183, 37, 122, 14, 124, 65, 67. Before applying scan to schedule the
requests on cylinders we need to know the direction of head movement in addition to the head’s
current position (53).
If the disk arm is moving towards 0, the head will service 37 and then 14. At cylinder 0,
the arm will reverse and will move toward the other end of the disk, servicing the requests at 65,
67, 98, 122, 124, and 183. If a request arrives in the queue just in front of the head, it will be
serviced almost immediately; a request arriving just behind the head will have to wait until the arm
moves to end of the disk, reverses direction, and comes back.
iv. C-SCAN Scheduling:
Circular SCAN (C-SCAN) scheduling is a variant of SCAN designed to provide a more
uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk to other,
servicing requests along the way. When the head reaches the other end, however it immediately
returns to the beginning of the disk, without servicing any requests on the return trip. The C- SCAN
scheduling algorithm essentially treats the cylinders as a circular list that wraps around from the
final cylinder to the first one.
14 | P a g
Unit – VI Operating Systems
15 | P a g