File System –Basic concepts
File system is the most visible aspect of an operating system.
It provides the mechanism for on-line storage of and access to both data and
programs of the operating system and all the users of the computer system.
The file system consists of two distinct parts: a collection of files, each Storing
related data, and a directory structure, which organizes and provides information
about all the files in the system.
File systems live on devices
File Concept
A file is a named collection of related information that is recorded on secondary
storage.
From a user’s perspective, a file is the smallest allotment of logical secondary
storage; that is, data cannot be written to secondary storage unless they are within
a file.
Many different types of information may be stored in a file—source or executable
programs, numeric or text data, photos, music, video, and so on.
A text file is a sequence of characters organized into lines (and possibly pages).
A source file is a sequence of functions, each of which is further organized as
declarations followed by executable statements.
An executable file is a series of code sections that the loader can bring into
memory and execute.
File Attributes
A file is named, for the convenience of its human users, and is referred to by its
name.
A name is usually a string of characters, such as example.c.
Some systems differentiate between uppercase and lowercase characters in
names, whereas other systems do not.
A file’s attributes vary from one operating system to another but typically consist of
these:
1. Name:-
o The symbolic file name is the only information kept in human readable
form.
2. Identifier:-
o This unique tag, usually a number, identifies the file within the file
system; it is the non-human-readable name for the file.
3. Type:-
o This information is needed for systems that support different types of
files.
4. Location:-
o This information is a pointer to a device and to the location of the file
on that device.
5. Size:-
o The current size of the file (in bytes, words, or blocks) and possibly
the maximum allowed size are included in this attribute.
6. Protection:-
o Access-control information determines who can do reading, writing,
executing, and so on.
7. Time, date, and user identification:-
o This information may be kept for creation, last modification, and last
use.
o These data can be useful for protection, security, and usage
monitoring.
The information about all files is kept in the directory structure, which also resides
on secondary storage.
Typically, a directory entry consists of the file’s name and its unique identifier.
The identifier in turn locates the other file attributes.
File Operations
A file is an abstract data type.
To define a file properly, we need to consider the operations that can be performed
on files.
The operating system can provide system calls to create, write, read, reposition,
delete, and truncate files.
1. Creating a file:-
o Two steps are necessary to create a file.
o First, space in the file system must be found for the file.
o Second, an entry for the new file must be made in the directory.
2. Writing a file:-
o 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.
o Given the name of the file, the system searches the directory to find the file’s
location.
o The system must keep a write pointer to the location in the file where the
next write is to take place.
o The write pointer must be updated whenever a write occurs.
3. Reading a file:-
o 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.
o 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.
o Once the read has taken place, the read pointer is updated.
o Because a process is usually either reading from or writing to a file, the
current operation location can be kept as a per-process currentfile-position
pointer.
o Both the read and write operations use this same pointer, saving space and
reducing system complexity.
4. Repositioning within a file:-
o The directory is searched for the appropriate entry, and the current-file-
position pointer is repositioned to a given value.
o Repositioning within a file need not involve any actual I/O.
o This file operation is also known as a file seek.
5. Deleting a file:-
o To delete a file, we search the directory for the named file.
o Having found the associated directory entry, we release all file space, so that
it can be reused by other files, and erase the directory entry.
6. Truncating a file:-
o The user may want to erase the contents of a file but keep its attributes.
o 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 file be reset to length zero and its file space released.
These six basic operations comprise the minimal set of required file operations.
Other common operations include appending new information to the end of an
existing file and renaming an existing file.
These primitive operations can then be combined to perform other file operations.
For instance,
o we can create a copy of a file—or copy the file to another I/O device, such as
a printer or a display—by creating a new file and then reading from the old
and writing to the new.
o We also want to have operations that allow a user to get and set the various
attributes of a file.
For example, we may want to have operations that allow a user to determine the
status of a file, such as the file’s length, and to set file attributes, such as the
file’s owner.
Several pieces of information are associated with an open file.
1. File pointer:-
o On systems that do not include a file offset as part of the read() and write()
system calls, the system must track the last read–write location as a current-
file-position pointer.
o This pointer is unique to each process operating on the file and therefore
must be kept separate from the on-disk file attributes.
2. File-open count:-
o As files are closed, the operating system must reuse its open-file table
entries, or it could run out of space in the table.
o Multiple processes may have opened a file, and the system must wait for the
last file to close before removing the open-file table entry.
o The file-open count tracks the number of opens and closes and reaches zero
on the last close.
o The system can then remove the entry.
3. Disk location of the file:-
o Most file operations require the system to modify data within the file.
o The information needed to locate the file on disk is kept in memory so that
the system does not have to read it from disk for each operation.
4. Access rights:-
o Each process opens a file in an access mode.
o This information is stored on the per-process table so the operating system
can allow or deny subsequent I/O requests.
File locks provide functionality similar to reader–writer locks
A shared lock is akin to a reader lock in that several processes can acquire the
lock concurrently.
An exclusive lock behaves like a writer lock; only one process at a time can
acquire such a lock.
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
Examples include [Link], server.c, and [Link].