Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views22 pages

Os Unit-V Notes Part - II

Uploaded by

abarshmxaske
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)
4 views22 pages

Os Unit-V Notes Part - II

Uploaded by

abarshmxaske
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
You are on page 1/ 22

UNIT-V

FILE
 A file is a collection of related information that is recorded on secondary
storage (such as magnetic disks, magnetic tapes and optical disks).

 The information in a file is defined by its creator.

 A file has a certain defined structure, which depends on its type.

FILE ATTRIBUTES OR COMPONENTS OF FILE

Figure: File Attribute in Windows


Figure: File Attribute in Linux

A file's attributes vary from one operating system to another but typically
consist of these:
 Name: This attribute indicate the name of the file. The user can
recognize file by their file attribute name. The symbolic file name is
the only information kept in human readable form.
 Identifier: This unique tag, usually a number, identifies the file within
the file system. It is the non-human-readable name for the file.
 Type: This information is needed for systems that support different
types of files. Some examples of file
types are .png, .gif, .jpg, .wav, .mp3, and .au.
 Location/Position: This information is a pointer to a device and to the
location of the file on that device. Example: Path or any reference.
 Size: The current size of the file (in bytes, words, or blocks) and
possibly the maximum allowed size are included in this attribute.
 Protection: Access-control information determines who can do reading,
writing, executing, and so on.
 Time, date, and user identification: This information may be kept for
creation, last modification, and last use. These data can be useful for
protection, security, and usage monitoring.
FILE OPERATIONS

 A file is an abstract data type.


 File operation is a term that implements the action taken for the file.
 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.
 Following are the files operations:

(1) Creating a file:

 Creation of file allow users to create the file on the secondary storage.
 Two steps are necessary to create a file:
 First, space in the file system must be found for the file.
 Second, an entry for the new file must be made in the directory.

(2)Writing a file:

 In order to write the content in file, we need to check whether the file
is created or not.
 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
file's location.
 The system must keep a write pointer to the location in the file where
the next write is to take place.
 The write pointer must be updated whenever a write occurs.

(3)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.
 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.
 Once the read has taken place, the read pointer is updated.
 Because a process is usually either reading from or writing to a file, the
current operation location can be kept as a per-process current file
position pointer.
 Both the read and write operations use this same pointer, saving space
and reducing system complexity.

(4) Repositioning within a file:

 The directory is searched for the appropriate entry, and the


current-file-position pointer is repositioned to a given value.
 Repositioning within a file need not involve any actual I/0.
 This file operation is also known as a file seek.

(5) 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, so
that it can be reused by other files, and erase the directory entry.
 In this operation, file contents and logical structure is deleted.

(6) Truncating a file:

 In this operation, the contents is deleted whereas the structure remains


as it is.
 If user want to reuse that file structure it can be used again.
 The user may want to erase the contents of a file but keep its attributes.
 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.

Other common operations include:


 Appending new information to the end of an existing file
 Renaming an existing file.
 Copying: we can create a copy of a file, or copy the file to another device.

OPEN FILE
 Most of the file operations mentioned involve searching the directory for
the entry associated with the named file.
 To avoid this constant searching, many systems require that an open ()
system call be made before a file is first used actively.
 The operating system keeps a small table, called the open-file table,
containing information about all open files.
 When a file operation is requested, the file is specified via an index into
this table, so no searching is required.
 When the file is no longer being actively used, it is closed by the process,
and the operating system removes its entry from the open-file table.
 Several pieces of information are associated with an open file.
 File pointer - records the current position in the file, for the next read
or write access.
 File-open count - How many times has the current file been opened
( simultaneously by different processes ) and not yet closed? When
this counter reaches zero the file can be removed from the table.
 Disk location of the file.
 Access rights: Some systems provide support for file locking.
 A shared lock is for reading only.
 A exclusive lock is for writing as well as reading.
 An advisory lock is informational only, and not enforced, where
the participating processes need to follow/obey a locking protocol
 A mandatory lock is enforced. If a lock is n1.andatory, then once
a process acquires an exclusive lock, the operating system will
prevent any other process from accessing the locked file.
 UNIX used advisory locks, and Windows uses mandatory locks.

FILE TYPES
 File types 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 character (.).
 In this way, the user and the operating system can tell from the name
alone what the type of a file is.
 Examples: resume.doc, Server.java, and ReaderThread.c
 The system uses the extension to indicate the type of the file and the type
of operations that can be done on that file.

Following are the File types:

1. A text file is a sequence of characters organized into lines. Text files are
regular files that contain information stored character format and are
readable by the user. A plain text file format contains unformatted text
and no font or page settings etc. can be applied. Example: .txt
2. A source file is a sequence of procedures and functions. Source file is
easily understand by human. Example: .c, .cpp, .java

3. An object file is a sequence of bytes organized into blocks that are


understandable by the machine. A computer program which has been
translated into machine language by a compiler and assembler, but not
yet linked into an executable program. Example: .obj, .o

4. An executable file is a series of code sections that the loader can bring
into memory and execute. Unlike a data file, an executable file cannot
be read because it has been compiled. The system can execute directly
when the user clicks the file icon. Example: .BAT, .COM, .EXE, and .BIN.
5. Word Processor: It provide some flexible way than text file type. A word
processing file contains user information in plain text or rich text format.
a rich text file format allows formatting options such as setting fonts type,
styles (bold, italic, underline, etc.), page margins, headings, bullets and
numbers, and several other formatting features. Example: .doc, .rtf, .wps

6. Batch File: Batch file is a way to run multiple commands in sequence. It


consists of a series of commands to be executed by the command-line
interpreter, stored in a plain text file. Example: .bat, .sh

7. Binary files: Binary files are regular files that contain information readable
by the computer. Binary files might be executable files that instruct the
system to accomplish a job. Commands and programs are stored in
executable, binary files.

8. Directory files: A directory is a file that acts as a folder for other files.
A directory can also contain other directories (subdirectories); a directory
that contains another directory is called the parent directory of the
directory it contains. It contain list of file names and other information
related to these files. Directory files contain information that the system
needs to access all types of files, but directory files do not contain the
actual file data. As a result, directories occupy less space than a regular
file. Each entry contains the name of the file and the file's index node
reference number (i-node number).

9. Library file: In programming, a library is a collection of precompiled


routines that a program can use. The routines, sometimes called modules,
are stored in object format. Libraries are particularly useful for storing
frequently used routines because you do not need to explicitly link them
to every program that uses them. Example: .lib

10. Archive file: It is a compressed file. An archive file is a file that is


composed of one or more computer files along with metadata. Archive
files are used to collect multiple data files together into a single file for
easier portability and storage, or simply to compress files to use less
storage space. Example:.arc, .zip, .rar

11. Multimedia file: Any file other than one containing only text. A file that
is capable of holding multimedia elements (text, images, audio, video and
animations). Example:.swf, .wav, .mp3, .mp4, .mpg, .wmv, and .avi.

12. DB File: DB is a database file format for Windows. DB files store data
information, usually stored in a series of tables, table fields, and field data
values. The information is then stored and organized according to the data
model, the most common model structure being the relational model.
FILE ACCESS METHOD

 Files stores information.


 When it is used, this information must be accessed and read into computer
memory.
 The way that files are accessed and read into memory is determined by
Access methods.
 Usually a single access method is supported by systems while there are
OS's that support multiple access methods.
 There are several ways to access files −
1. Sequential access
2. Direct/Random access
3. Indexed sequential access

1. Sequential Access Method


 The simplest access method is sequential access.
 A sequential access is that in which the records are accessed in a sequence,
i.e., the information in the file is processed in order, one record after the
other.
 Example: Compilers usually access files in this fashion.
 Read command cause a pointer to be moved ahead by one.
 Write command allocate space for the record and move the pointer to the
new end of file.
 A sequential access file emulates / follows magnetic tape operation,
and generally supports a few operations:
 read next - read a record and advance (move) a file pointer
to the next position.
 write next - write a record and advance (move) a file pointer
to the next position.
 Rewind - Moving back to the earlier location.
 Skip n - skip n records
Figure- Sequential-access file
2. Direct Access Method
 Also called as Random Access or Relative Access Method.
 This method is useful for disks.
 The file is viewed as a numbered sequence of blocks or records.
 There are no restrictions on which blocks are read/written, it can be done
in any order.
 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.

 Operations supported include:


 read n - read record number n.
 write n - write record number n.
 jump to record n - could be 0 or the end of file.

3. Indexed Access Method


 This mechanism is built up on base of sequential access.
 It uses an Index to control the pointer while accessing files.
 An index is created for each file which contains pointers to various blocks.
 The index, like an index of a book, contains pointers to the various blocks.
 To find a record in the file, we first search the index and then use the
pointer to access the file directly and to find the desired record.
 Index is searched sequentially and its pointer is used to access the file
directly.

Figure - Example of index and relative files


FILE SYSTEM
 File system is used to control how data is stored and retrieved.
 Without a file system, information placed in a storage medium would be
one large body of data with no way to tell where one piece of information
stops and the next begins.
 The file system consists of two distinct parts:
1. A collection of files, each storing related data.
2. A directory structure, which organizes and provides information
about all the files in the system. Example: Phone Directory

DIRECTORY STRUCTURE

 There are typically thousand, millions, and even billions of files within
a computer.
 Files are stored on random-access storage devices, including hard disks,
optical disks, and solid state (memory-based) disks.
Figure - A typical file-system organization
 A storage device can be used in its entirety for a file system.
 It can also be subdivided for finer-grained control. For example, a disk
can be into quarters, and each quarter can hold a file system.
 Partitions are also known as slices or mini disks. A file system can be
created on each of these parts of the disk.
 Any entity containing a file system is generally known as a volume.
 Each volume that contains a file system must also contain information
about the files in the system. This information is kept in entries in a device
directory or volume table of contents.

 The device directory (more commonly known simply as that directory)


records information -such as name, location, size, and type-for all files
on that volume.
 Above Figure shows a typical file-system organization.

STORAGE STRUCTURE
 A disk can be used in its entirety for a file system.
 Alternatively a physical disk can be broken up into multiple partitions,
slices, or mini-disks, each of which becomes a virtual disk and can have
its own file system. ( or be used for raw storage, swap space, etc. )
 Or, multiple physical disks can be combined into one volume, i.e. a larger
virtual disk, with its own file system spanning the physical disks.

DIRECTORY OVERVIEW
 Directory operations to be supported include:
 Search for a file - We need to be able to search a directory structure
to find the entry for a particular file.
 Create a file - New files need to be created and added to the directory.
 Delete a file - When a file is no longer needed, we want to be able
to remove it from the directory or erase from the directory.
 List a directory -We need to be able to list the files in a directory and
the contents of the directory entry for each file in the list
 Rename a file - Because the name of a file represents its contents to
its users, we must be able to change the name when the contents or
use of the file changes. Renaming a file may also allow its position
within the directory structure to be changed.
 Traverse the file system - We may wish to access every directory and
every file within a directory structure. For reliability, it is a good idea
to save the contents and structure of the entire file system at regular
intervals. Often, we do this by copying all files to magnetic tape. This
technique provides a backup copy in case of system failure. In addition,
if a file is no longer in use, the file can be copied to tape and the
disk space of that file released for reuse by another file.
Advantages of maintaining directories are:

 Efficiency: A file can be located more quickly.


 Naming: It becomes convenient for users as two users can have same
name for different files or may have different name for same file.
 Grouping: Logical grouping of files can be done by properties e.g. all java
programs, all games etc.
SCHEMES FOR DEFINING THE LOGICAL STRUCTURE OF A
DIRECTORY
OR
CLASSIFICATION OF DIRECTORY SYSTEM

1. Single-Level Directory

 The simplest directory structure is the single-level directory.


 All files are contained in the same directory, which is easy to support and
understand
 In this a single directory is maintained for all the users.
 In this type of directory system, there is a root directory which has all
files.
 It has a simple architecture and there are no sub directories.
 Advantage :
 It is easy to find a file in the directory.
 Easy to implement.
 This type of directory system is used in cameras and phones.

Figure - Single-level directory


 Disdavatages:
 Naming problem: Users cannot have same name for two files i.e. all
files in the directory must have unique name.
 Grouping problem: Users cannot group files according to their need.
 High Overhead: It lead to high overhead on single directory due to
increase in the size of directory containing files. Keeping track of so
many files is a daunting (difficult) task.
 Cannot support Scalability: Because of single level it cannot
accommodate more files in single directory.

2. Two-Level Directory

 Path name:Due to two levels there is a path name for every file to locate
that file.
 Now,we can have same file name for different user.
 Searching is efficient in this method.
 A single-level directory often leads to confusion of file names among
different users.
 The standard solution is to create a separate directory for each user is
called as User Level Directory or User File Directory (UFD).
 Each user gets their own directory space.
 File names only need to be unique within a given user's directory.
 The root of a tree is Master File Directory (MFD).
 The files are the leaves of the tree.
 A master file directory is used to keep track of each users directory, and
must be maintained when users are added to or removed from the system.
 A separate directory is generally needed for system ( executable ) files.
 Systems may or may not allow users to access other directories besides
their own
 If access to other directories is allowed, then provision must be made
to specify the directory being accessed.
 If access is denied, then special consideration must be made for users
to run programs located in system directories.

Figure- Two-level directory structure.

Advantages:
 It solves the naming problem because all user can give same name to their
directory.
 This system is best and effective for searching.
 It supports for isolation(separation) of users.

Disadvantages:
 Users cannot group files according to their need.

3. Tree-Structured Directories
 Directory is maintained in the form of a tree.
 Searching is efficient and also there is grouping capability.
 Users may create their own sub directories.
 Each user / process has the concept of a current directory from which all
( relative ) searches take place.
 Files may be accessed using either absolute path names or relative path
names.
Absolute Path Relative Path

Relative to the root of the tree Relative to the current directory

Begins at root and follows a path down Defines a path from current
to the specified file. directory.

EXAMPLE: C:\Windows\calc.exe EXAMPLE: calc.exe

Internet URL absolute path: Internet URL non absolute path


https://www.computerhope.com/oh.ht (relative path): oh.htm
m

 Directories are stored the same as any other file in the system, except
there is a bit that identifies them as directories, and they have some
special structure that the OS understands.
 Special system calls are used to create and delete directories.
 Deletions if directory is empty, its entry in the directory that contains it
can simply deleted.
 If it is not empty : One of the Two approaches can be taken :-
a)User must delete all the files in the directory.
b)If any sub directories exist, same procedure must be applied.
 The UNIX - rm remove directory command is used.
 MS dos will not delete a directory unless it is empty.
Figure- Tree-structured directory structure
Advantages:
 It allows user to create sub directories.

Disadvantages:
 Deletion of file is somewhat difficult.
 A path to a file in a tree - structured directory can be longer than a path
in two -level directory.

4. Acyclic-Graph Directories

 When the same files need to be accessed in more than one place in the
directory structure ( e.g. because they are being shared by more than one
user / process ), it can be useful to provide an acyclic-graph structure.
( Note the directed arcs from parent to child. )
 In all previous approach there is a need of sharing of file among various
user directory.
 If user want to access or share file by another user directory so the
problem arises.
 To overcome this problem acyclic graph is introduced.
 Consider two programmers or project developers are working on same
project they often required to access the same file.
 Here both programmer are equally responsible for the project, both want
the sub-directories to be in their own directories.
 An acyclic graph allows directories to have shared subdirectories and files.
 The same file is shared by two users.

Figure- Acyclic-graph directory structure

 With a shared file, only one actual file exist. So, any changes made by
one person are immediately visible to the other.
 UNIX provides two types of links for implementing the acyclic-graph
structure.
 A Hard Link ( usually just called a link ) involves multiple directory
entries that both refer to the same file. Hard links are only valid for
ordinary files in the same file system.
 A Symbolic Link or Soft Link, that involves a special file, containing
information about where
5. General Graph Directory
 Allow cycles. Example: A is subdirectory of B and B is subdirectory of A
 If cycles are allowed in the graphs, then several problems can arise.
 Search algorithms can go into infinite loops.
 How do we guarantee no cycles?
 Allow only links to file not subdirectories
 Every time a new link is added use a cycle detection algorithm to
determine whether it is OK to find the linked file. Symbolic links may
be used to link directories and/or files in other file systems, as well as
ordinary files in the current file system.

Figure: General graph directory

Advantages:
 Team Work
 In organization if files are required mostly(frequently) then this approach
is good.

Disadvantages:
1. Deletion of file: If one project member delete a file then other do not know
the file has been deleted so then the problem arrives.
2. Multiple absolute path names: There is a possibility to have multiple path
name. If user want to traverse the file then problem occur.

You might also like