0% found this document useful (0 votes)
599 views15 pages

Unit 5 - File System Interface

File system interface in OS

Uploaded by

benazirrose2704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
599 views15 pages

Unit 5 - File System Interface

File system interface in OS

Uploaded by

benazirrose2704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

III File-System Interface – File concept

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. In general, a file is a sequence of bits, bytes,
lines or records whose meaning is defined by the files creator and user.
File Attributes
Different OS keep track of different file attributes, including:
Name - Some systems give special significance to names, and particularly extensions ( .exe, .txt,
etc. ), and some do not. Some extensions may be of significance to the OS ( .exe ), and others only
to certain applications ( .jpg )
Identifier
Type - Text, executable, other binary, etc.
Location - on the hard drive.
Size Protection
Time & Date
User ID

File Operations

File Creation and Manipulation


File Creation and Manipulation encompasses essential operations within an operating system that
involve creating, modifying, and organizing files and directories. These actions are vital for
managing data efficiently and are integral to the functioning of computer systems.
File Organization and Search
File organization and search are key OS operations for arranging files systematically and swiftly
locating specific data, optimizing file management and user efficiency.

File Operation Description System Calls / APIs

 open() (Linux-like systems)


Creating Files Create a new file for data storage.
 CreateFile() (Windows)

Creating Create a new directory for organizing  mkdir() (Linux systems)


Directories files.  CreateDirectory() (Windows)

Open a file that you already have  open() (Linux systems)


Opening Files
open to read or write from.  CreateFile() (Windows)

 read() (Linux systems)


Reading Files Retrieve data from an open file.
 ReadFile() (Windows)
 write() (Linux systems)
Writing Files Store data in an open file.
 WriteFile() (Windows)

Renaming Files If you want to rename a file or  rename() (Linux systems)


and Directories directory,.  MoveFile() (Windows)

 unlink() (Linux systems)


Deleting Files  remove() (Linux systems)
Remove files or directories.
and Directories  DeleteFile() (Windows)
 RemoveDirectory() (Windows)

File Operation Description System Calls / APIs

Create duplicates of files in another  cp (Linux systems)


Copying Files
location.  CopyFile() (Windows)

Relocate files from one location to  mv (Linux systems)


Moving Files
another.  MoveFile() (Windows)

 find (Linux systems)


Searching for
Locate files based on specific criteria.  FindFirstFile() and FindNextFile()
Files
(Windows)

File Security and Metadata


File Security and Metadata are vital components of file management, encompassing access control and
crucial file information preservation within an operating system. They are essential for data security and
efficient organization.

File Operation Description System Calls / APIs

 chmod (Linux systems)


File Permissions Control access rights to files and directories.
 SetFileSecurity (Windows)

 chown (Linux systems)


File Ownership Assign specific users or groups as file owners.
 SetFileSecurity (Windows)

 stat (Linux systems)


File Metadata Retrieve and manipulate file information.
 GetFileAttributesEx (Windows)

File Compression and Encryption


File Compression and Encryption are essential for optimizing storage and enhancing data security.
Compression reduces file sizes, while encryption safeguards data privacy by making it unreadable without
the correct decryption key.
File Operation Description System Calls / APIs

File Reduce file sizes to save storage  gzip, zip, tar (Linux systems),
Compression space.  Compress-Archive (Windows)

 openssl, gpg (Linux systems)


File Protect data by converting it into an
 Windows provides encryption libraries and
Encryption unreadable format.
APIs for encryption operations.

File Structure
Some files contain an internal structure, be known to the OS.
For the OS to support particular file formats increases the size and complexity of the OS.
UNIX treats all files as sequences of bytes, with no further consideration of the internal structure.
( With the exception of executable binary programs, which it must know how to load and find
the first executable statement, etc. )
Macintosh files have two forks - a resource fork, and a data fork. The resource fork contains
information relating to the UI, such as icons and button images, and can be modified independently
of the data fork, which contains the code or data as appropriate.
A File Structure should be according to a required format that the operating system can understand.
A file has a certain defined structure according to its type. A text file is a sequence of characters
organized into lines. A source file is a sequence of procedures and functions.

An object file is a sequence of bytes organized into blocks that are understandable by the machine.

Files can be structured in several ways in which three common structures are given in this
tutorial with their short description one by one.

Here, as you can see from the above figure, the file is an unstructured sequence of bytes. Therefore,
the OS doesn't care about what is in the file, as all it sees are bytes.
File Structure 2 1 Record

Now, as you can see from the above figure that shows the second structure of a file, where a file is a
sequence of fixed-length records where each with some internal structure. Central to the idea about
a file being a sequence of records is the idea that read operation returns a record and write operation
just appends a record.

Now in the last structure of a file that you can see in the above figure, a file basically consists of a
tree of records, not necessarily all the same length, each containing a key field in a fixed position in
the record. The tree is stored on the field, just to allow the rapid searching for a specific key.
Access Methods
File access mechanism refers to the manner in which the records of a file may be accessed. There
are several ways to access files
Sequential access
Direct/Random access
Indexed sequential access
Sequential access
A sequential access is that in which the records are accessed in some sequence, i.e., the information
in the file is processed in order, one record after the other. This access method is the most primitive
one. Example: Compilers usually access files in this fashion.

Sequential-access file.
Direct/Random access
Random access file organization provides, accessing the records directly.
Each record has its own address on the file with by the help of which it can be directly accessed
for reading or writing.
The records need not be in any sequence within the file and they need not be in adjacent
locations on the storage medium.

Indexed sequential access


This mechanism is built up on base of sequential access.

An index is created for each file which contains pointers to various


blocks. Index is searched sequentially and its pointer is used to access
the file directly.

Example of index and relative files.


DIRECTORY STRUCTURE

A directory is a container that is used to contain folders and files. It organizes files and folders in a
hierarchical manner. In other words, directories are like folders that help organize files on a
computer.
Just like you use folders to keep your papers and documents in order, the operating system uses
directories to keep track of files and where they are stored. Different structures of directories can be
used to organize these files, making it easier to find and manage them.

Eg.

Different Types of Directory in OS

In an operating system, there are different types of directory structures that help organize and manage
files efficiently.
Directories in an OS can be single-level, two-level, or hierarchical.

Each type of directory has its own way of arranging files and directories, offering unique benefits
and features.
These are:
 Single-Level Directory
 Two-Level Directory
 Tree Structure/ Hierarchical Structure
 Acyclic Graph Structure
 General-Graph Directory Structure

1) Single-Level Directory
The single-level directory is the simplest directory structure. In it, all files are contained in the
same directory which makes 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 a unique name.
Advantages
 Since it is a single directory, so its implementation is very easy.
 If the files are smaller in size, searching will become faster.
 The operations like file creation, searching, deletion, updating are very easy in such a
directory structure.

 Logical Organization : Directory structures help to logically organize files and directories
in a hierarchical structure. This provides an easy way to navigate and manage files, making it
easier for users to access the data they need.
 Increased Efficiency: Directory structures can increase the efficiency of the file system by
reducing the time required to search for files. This is because directory structures are optimized for
fast file access, allowing users to quickly locate the file they need.
 Improved Security : Directory structures can provide better security for files by allowing
access to be restricted at the directory level. This helps to prevent unauthorized access to sensitive
data and ensures that important files are protected.
 Facilitates Backup and Recovery : Directory structures make it easier to backup and
recover files in the event of a system failure or data loss. By storing related files in the same
directory, it is easier to locate and backup all the files that need to be protected.
 Scalability: Directory structures are scalable, making it easy to add new directories and
files as needed. This helps to accommodate growth in the system and makes it easier to manage
large amounts of data.
Disadvantages

 There may chance of name collision because two files can have the same name.
 Searching will become time taking if the directory is large.
 This can not group the same type of files together.

2) Two-Level Directory
As we have seen, a single level directory often leads to confusion of files names among different
users. 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 UFDs
have similar structures, but each lists only the files of a single user. System’s master file directory
(MFD) is searched whenever a new user id is created.
Two-Levels Directory Structure

Advantages
 The main advantage is there can be more than two files with same name, and would be very
helpful if there are multiple users.
 A security would be there which would prevent user to access other user’s files.
 Searching of the files becomes very easy in this directory structure.

Disadvantages
 As there is advantage of security, there is also disadvantage that the user cannot share the
file with the other users.
 Unlike the advantage users can create their own files, users don’t have the ability to create
subdirectories.
 Scalability is not possible because one user can’t group the same types of files together.

3) Tree Structure/ Hierarchical Structure


Tree directory structure of operating system is most commonly used in our personal computers.
User can create files and subdirectories too, which was a disadvantage in the previous directory
structures.
This directory structure resembles a real tree upside down, where the root directory is at the peak.
This root contains all the directories for each user. The users can create subdirectories and even store
files in their directory.
A user do not have access to the root directory data and cannot modify it. And, even in this directory
the user do not have access to other user’s directories. The structure of tree directory is given below
which shows how there are files and subdirectories in each user’s directory.
Tree/Hierarchical Directory Structure

Advantages
 This directory structure allows subdirectories inside a directory.
 The searching is easier.
 File sorting of important and unimportant becomes easier.
 This directory is more scalable than the other two directory structures explained.
Disadvantages
 As the user isn’t allowed to access other user’s directory, this prevents the file sharing
among users.
 As the user has the capability to make subdirectories, if the number of subdirectories
increase the searching may become complicated.
 Users cannot modify the root directory data.
 If files do not fit in one, they might have to be fit into other directories.

4) Acyclic Graph Structure


As we have seen the above three directory structures, where none of them have the capability to
access one file from multiple directories. The file or the subdirectory could be accessed through the
directory it was present in, but not from the other directory.
This problem is solved in acyclic graph directory structure, where a file in one directory can be
accessed from multiple directories. In this way, the files could be shared in between the users. It
is designed in a way that multiple directories point to a particular directory or file with the help of
links.
In the below figure, this explanation can be nicely observed, where a file is shared between multiple
users. If any user makes a change, it would be reflected to both the users.

Acyclic Graph Structure

Advantages
 Sharing of files and directories is allowed between multiple users.
 Searching becomes too easy.
 Flexibility is increased as file sharing and editing access is there for multiple users.
Disadvantages
 Because of the complex structure it has, it is difficult to implement this directory structure.
 The user must be very cautious to edit or even deletion of file as the file is accessed by
multiple users.
 If we need to delete the file, then we need to delete all the references of the file inorder to
delete it permanently.
5) General-Graph Directory Structure
Unlike the acyclic-graph directory, which avoids loops, the general-graph directory can have cycles,
meaning a directory can contain paths that loop back to the starting point. This can make navigating
and managing files more complex.

General Graph Directory Structure

In the above image, you can see that a cycle is formed in the User 2 directory. While this structure
offers more flexibility, it is also more complicated to implement.
Advantages of General-Graph Directory
 More flexible than other directory structures.
 Allows cycles, meaning directories can loop back to each other.
Disadvantages of General-Graph Directory
 More expensive to implement compared to other solutions.
 Requires garbage collection to manage and clean up unused files and directories.

Important questions
1. What is a file?(2 marks)
2. List the file attributes.(2 marks)
3. Write in the detail the operations that can be performed on a file (15 marks)
4. Elaborate on the various file structures. ( 7 marks)
5. Write a note on the various file access methods. ( 7 marks)
6. What is a directory?
7. Write a detailed note on the different types of directories in OS.
8. What is a root Directory?
A root directory is a fundamental concept in directory structure of OS. It is a parent directory that
is a top-level directory organised in a hierarchical manner and is represented by a forward slash
“/” in a unix like systems & a backslash in windows.

9. What is a purpose of a directory structure?


The sole purpose of a directory structure is to provide a way to store and oragnise files & data in
an efficient manner so that access and management of all the files can be done by the user in the
simplest and productive manner.
10. What is a sub-directory ?
A sub-directory is a directory inside another directory. Inside a directory structure, a user can create
many directories inside a directory. These are called sub-directories.

11. What are some common directory structures used in Operating System?
Most common directory structures used to store and organise files are – Single-Level, Two-Level,
tree-like directory structures. Tree structure is generally used in modern personal computers.

12. What are most common system calls made while managing files through a directory structure?
Following system calls are majorly used :
 mkdir() : this system call is made when creating a new file through GUI or through
command line
 open() : This call is made when opening a file inside a directory structure
 close() : This call is made when we close a file in a directory structure
 unlink() : This call is made to unlike a file from other directories before deleting it
 rmdir : This call is made to remove a directory from a file system

Objective type questions

 What is a file in the context of an operating system?

 a) A process
 b) A named collection of related information
 c) A folder
 d) A type of memory

 Which of the following is NOT a file attribute?

 a) Name
 b) Size
 c) Color
 d) Location

 What file extension is typically significant to the operating system?

 a) .jpg
 b) .docx
 c) .exe
 d) .pdf

 Which system call is used to create a file in Linux-like systems?

 a) open()
 b) mkdir()
 c) cp
 d) chmod

 What system call is used to create a directory in Windows?

 a) CreateFile()
 b) CreateDirectory()
 c) remove()
 d) stat()

 Which of the following is used to rename a file in Linux?

 a) cp
 b) rename()
 c) move()
 d) unlink()

 What system call is used to delete a file in Linux?

 a) mkdir()
 b) remove()
 c) unlink()
 d) chmod

 Which of the following is used to copy files in Windows?

 a) cp
 b) CopyFile()
 c) mv
 d) RemoveDirectory()

 In Linux, which command is used to search for files based on specific criteria?

 a) cp
 b) find
 c) mv
 d) rm

 What is the role of file permissions in a file system?

 a) To compress the file


 b) To encrypt the file
 c) To control access rights to files
 d) To find the file

 In Linux, which command is used to change file ownership?

 a) stat
 b) chown
 c) chmod
 d) open()

 Which access method processes a file one record after another in sequence?

 a) Sequential access
 b) Random access
 c) Indexed sequential access
 d) Direct access

 What is the benefit of indexed sequential access?


 a) Access records in sequence only
 b) Allows both sequential and direct access using an index
 c) Always stores records in adjacent locations
 d) Requires records to be of the same length

 Which directory structure is the simplest and most basic?

 a) Tree Structure
 b) Two-Level Directory
 c) Single-Level Directory
 d) Acyclic Graph Directory

 Which directory structure allows each user to have their own User Files Directory (UFD)?

 a) Single-Level Directory
 b) Two-Level Directory
 c) Tree Structure
 d) General Graph Directory

 What is the main disadvantage of a Single-Level Directory structure?

 a) It cannot support subdirectories


 b) Files can only be accessed sequentially
 c) Name collisions occur as all files are in the same directory
 d) It is difficult to implement

 Which directory structure allows the creation of subdirectories inside directories?

 a) Single-Level Directory
 b) Tree Structure
 c) Two-Level Directory
 d) Acyclic Graph Directory

 In which directory structure can files or directories be accessed from multiple directories using links?

 a) Single-Level Directory
 b) Two-Level Directory
 c) Acyclic Graph Structure
 d) Tree Structure

 What is a key advantage of the Acyclic Graph Directory structure?

 a) Easier to implement than other structures


 b) Allows file sharing between multiple users
 c) Prevents file sharing
 d) Does not require links

 Which directory structure can have cycles, where directories loop back to each other?

 a) Acyclic Graph Directory


 b) Single-Level Directory
 c) General Graph Directory
 d) Two-Level Directory
 What is the primary purpose of a directory in an operating system?

 a) To store files sequentially


 b) To organize files and folders in a hierarchical manner
 c) To encrypt files
 d) To delete files

 Which command is used to reduce file sizes in Linux?

 a) cp
 b) gzip
 c) stat
 d) find

 What is the key difference between Sequential access and Random access?

 a) Sequential access accesses records in order, while Random access retrieves them directly
 b) Random access can only read files, while Sequential can write
 c) Random access requires an index, while Sequential does not
 d) Sequential access is faster than Random access

 What method of access is built upon Sequential access and uses an index to locate records?

 a) Direct access
 b) Random access
 c) Indexed Sequential access
 d) None of the above

 Which of the following file structures stores records in a tree format, with key fields?

 a) Sequential structure
 b) Fixed-length record structure
 c) Tree of records
 d) Unstructured bytes

 What does the stat system call in Linux retrieve?

 a) File permissions
 b) File ownership
 c) File metadata
 d) File encryption key

 Which file organization method is most commonly used in personal computers?

 a) Single-Level Directory
 b) Tree Structure
 c) Acyclic Graph Directory
 d) Two-Level Directory

 What advantage does a Tree Structure directory have over a Two-Level Directory?

 a) Allows file sharing between users


 b) Supports subdirectories
 c) Allows only one file per user
 d) Prevents file searching

 In the General Graph Directory structure, what is required to manage unused files and directories?

 a) Compression
 b) Encryption
 c) Garbage collection
 d) Sequential access

 Which of the following directory structures is the most scalable?

 a) Single-Level Directory
 b) Tree Structure
 c) Two-Level Directory
 d) General Graph Directory

You might also like