0% found this document useful (0 votes)
10 views20 pages

Lecture-Files and Directories

The document discusses the fundamentals of file systems, including the definition of a file, operations on files, and various methods of file storage such as contiguous allocation, linked-list allocation, and i-nodes. It also covers the implementation of directories, the management of file attributes, and the concept of shared files in operating systems. Key points include the advantages and disadvantages of different allocation methods and the importance of directory entries in locating files on disk.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Lecture-Files and Directories

The document discusses the fundamentals of file systems, including the definition of a file, operations on files, and various methods of file storage such as contiguous allocation, linked-list allocation, and i-nodes. It also covers the implementation of directories, the management of file attributes, and the concept of shared files in operating systems. Key points include the advantages and disadvantages of different allocation methods and the importance of directory entries in locating files on disk.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Operating Systems

BCS303

Lecture: Files and Directories

Balaji Vijaykumar B.E.,(NIE), M.Tech.,(SJCE)

Assistant Professor, Department of CS&E-AIML


The National Institute of Engineering, Mysuru

(Former Research & Development Engineer @


Philips R&D)
(Former Software Engineer @ Accenture)
Files Systems
What is a File?
A file can be called persistent sequential (structured) data repository. The idea is that files can be used to store data for long
periods of time, and specifically, for longer than the runtimes of the processes that create them. Thus one process can create a
file, and another process may re-access the file using its name.
The most important operations on files are reading and writing.
In a nutshell, the main ones are
• Open : gain access to a file.
• Close: relinquish access to a file.
• Read: read data from a file, usually from the current position.
• Write: write data to a file, usually at the current position.
• Append: add data at the end of a file.
• Seek: move to a specific position in a file.
• Rewind: return to the beginning of the file.
• Set attributes: e.g. to change the access permissions.
• Rename: change the name of the file.
Files System Layout

• File systems are stored on disks.


• Disks can be divided up into one or more partitions, with independent file systems on each partition.
• Sector 0 of the disk is called the MBR (Master Boot Record) and is used to boot the computer.
• The end of the MBR contains the partition table.
• This table gives the starting and ending addresses of each partition.
• This table gives the starting and ending addresses of each partition.
• One of the partitions in the table is marked as active.
• When the computer is booted, the BIOS reads in and executes the MBR.
Files System Layout Cont…
• The first thing the MBR program does is locate the active partition, read in its first block, called the boot block, and
execute it.

• The program in the boot block loads the operating system contained in that partition.

• For uniformity, every partition starts with a boot block, even if it does not contain a bootable operating system.

• The layout of a disk partition varies a lot from file system to file system.

• File system will contain some of the items shown in Fig.

• Superblock contains all the key parameters about the file system and is read into memory when the computer is booted
or the file system is first touched.
• Typical information in the superblock includes a
 Magic number to identify the file-system type,
 The number of blocks in the file system, and
 Other key administrative information.
FILE-SYSTEM IMPLEMENTATION
 Users are concerned with how
 Files are named,
 What operations are allowed on them,
 What the directory tree looks like, and similar interface issues.
 Implementers are interested in how
 Files and directories are stored,
 How disk space is managed, and
 How to make everything work efficiently and reliably.

• Most important issue in implementing file storage is keeping track of which disk blocks go with
which file.

Various methods are used in different operating systems. some methods are discussed are as follows:
1. Contiguous Allocation
2. Linked-List Allocation
3. Linked-List Allocation Using a Table in Memory
4. I-nodes
Contiguous Allocation

Fig. Contiguous allocation of disk space for seven files

• The simplest allocation scheme is to store each file as a contiguous run of disk blocks.
• On a disk with 1-KB blocks, a 50-KB file would be allocated 50 consecutive blocks and with 2-KB
blocks, it would be allocated 25 consecutive blocks.
• Fig. shows an example of contiguous storage allocation.
Contiguous Allocation
Contiguous disk-space allocation has two significant advantages.
1. It is simple to implement because keeping track of where a file’s blocks are, is
reduced to remembering two numbers:
• The disk address of the first block and
• The number of blocks in the file.
2. Read performance is excellent because the entire file can be read from the disk in
a single operation.
• Only one seek is needed (to the first block).
• After that, no more seeks or rotational delays are needed, so data come in at the
full bandwidth of the disk.
Contiguous Allocation
Cont..
 Contiguous allocation also has a very serious drawback:
 Over the course of time, the disk becomes fragmented as shown in Fig.
 Initially, fragmentation won’t be a problem, but as the disk fills, the holes are to be used which is going to
be time consuming in keep tracking of the size of the hole

Fig. The state of the disk after files D and F have been removed.
Linked-List Allocation
 The second method for storing files is to keep each one as a linked list of disk blocks, as shown in Fig.
Linked-List Allocation
Cont…
The first word of each block is used as a pointer to the next one.
The rest of the block is for data.
Every disk block can be used in this method.
No space is lost to disk fragmentation
It is sufficient for the directory entry to merely store the disk address of the
first block.
The rest can be found starting there.
Drawback is random access is extremely slow, and also
Amount of data storage in a block is no longer a power of two because the
pointer takes up a few bytes
Linked-List Allocation Using a Table in
Memory
 Disadvantages of the linked-list allocation can be eliminated by taking
the pointer word from each disk block and putting it in a table in
memory

 Fig. shows what the table looks like for the example of linked list Fig.
File A uses disk blocks 4, 7, 2, 10, and 12, in that order, and file B uses disk
blocks 6, 3, 11, and 14, in that order.
 Using the table of Fig. we can start with block 4 and follow the chain all
the way to the end.
 The same can be done starting with block 6.
 Both chains are terminated with a special marker (e.g., −1) that is not a
valid block number.
 Such a table in main memory is called a FAT (File Allocation Table).

Fig. Linked-list allocation using a file-allocation table in main memory


Linked-List Allocation Using a Table in
Memory Cont….
With this organization, the entire block is available for data.
Random access is much easier, but the chain must still be followed to
find a given offset within the file
The chain is entirely in memory, so it can be followed without making
any disk references.
Disadvantage of this method is that the entire table must be in memory
all the time to make it work.
I-nodes
 Last method for keeping track of which blocks belong to which file is to associate with each file a
data structure called an i-node (index-node), which lists the attributes and disk addresses of the
file’s blocks.

 Given the i-node, it is then possible to find all the blocks of the
file.
 The big advantage of this scheme over linked files using an in-
memory table is that the i-node need be in memory only when the
corresponding file is open.
 If each i-node occupies n bytes and a maximum of k files may be
open at once, the total memory occupied by the array holding the
i-nodes for the open files is only kn bytes
 Only this much space need be reserved in advance.

Fig. An example i-node


Implementing Directories

 Before a file can be read, it must be opened.


 When a file is opened, the operating system uses the path name supplied by
the user to locate the directory entry on the disk.
 The directory entry provides the information needed to find the disk blocks.
 Depending on the system, this information may be the
 Disk address of the entire file (with contiguous allocation),
 Number of the first block (both linked-list schemes), or
 Number of the i-node.
 The main function of the directory system is to map the ASCII name of the
file onto the information needed to locate the data.
Implementing Directories
Fig.
(a) A simple directory containing
fixed-size entries with the disk
addresses and attributes in the
directory entry.

(b) A directory in which each entry


just refers to an i-node.

 A closely related issue is where the attributes should be stored.


 Every file system maintains various file attributes, such as each file’s owner and creation time, and they must be
stored somewhere.
 One possibility is to store them directly in the directory entry.
 This option is shown in Fig. (a).
 For Systems that use i-nodes, another possibility for storing the attributes is in the i-nodes, rather than in the
directory entries.
 The directory entry will be shorter: just a file name and an i-node number shown in Fig. (b).
Implementing Directories Cont..

 In DOS filename length is up to 8 characters, and an optional extension upto 3 characters.


 In UNIX Version 7, file names were 1–14 characters, including any extensions
 All modern operating systems support longer, variable-length file names.
 How can these be implemented?
 The simplest approach is to set a limit on file-name length to 255 characters, and then use one
of the designs shown in fig. with 255 characters reserved for each file name.
 Approach is simple, but wastes a great deal of directory space, since few files have such long
names.
Implementing Directories Cont..
 An alternate method is shown in fig.

Fig. Two ways of handling long


file names in a directory.
(a) In-line. (b) In a heap.
Shared Files
 When several users are working together on a project, they often need to share files.
As a result, it is often convenient for a shared file to appear simultaneously in different directories belonging to different
users.
 Fig. shows the file system with one of C’s files is now present in one of B’s directories as well.
 The connection between B’s directory and the shared file is called a link
 The file system itself is now a Directed Acyclic Graph, or DAG, rather than a tree.

Fig. File system containing a shared file.


Shared Files
Problems with shared files:
Changes made by one user will not be available to other.
Solution for the above is:
1. Disk blocks are not listed in directories, but in a little data structure associated with
the file itself.
The directories would then point just to the little data structure (Used in Inix)
2. B links to one of C’s files by having the system create a new file, of type LINK,
and entering that file in B’s directory
The new file contains just the path name of the file to which it is linked.
When B reads from the linked file, the operating system sees that the file being
read from is of type LINK, looks up the name of the file, and reads that file.
This approach is called symbolic linking
Shared Files

These two methods has drawbacks has shown in fig.


Fig. (a) Situation prior to linking. (b) After the link is created. (c) After the original owner removes the file.

You might also like