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

OS Unit 5

The document explains various file system operations and functions in Unix-like operating systems, including close, stat, lseek, and ioctl. It also discusses file allocation methods such as contiguous, linked, and indexed allocation, along with their advantages and disadvantages. Additionally, it covers file attributes, operations, and access methods, providing a comprehensive overview of file management in operating systems.

Uploaded by

rakeshreddyt25
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 views21 pages

OS Unit 5

The document explains various file system operations and functions in Unix-like operating systems, including close, stat, lseek, and ioctl. It also discusses file allocation methods such as contiguous, linked, and indexed allocation, along with their advantages and disadvantages. Additionally, it covers file attributes, operations, and access methods, providing a comprehensive overview of file management in operating systems.

Uploaded by

rakeshreddyt25
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/ 21

Unit-V

1.Explain the following:


a) close b)stat
c) lseek d)ioctl

a) close

The close function in Unix-like operating systems is used to close a file descriptor, so that it
no longer refers to any file and may be reused.

Syntax:

int close(int fd);

• fd: The file descriptor to be closed.


• Returns: 0 on success, -1 on failure.

Usage: When a program is done using a file, it should close the file descriptor associated
with that file to free up resources and avoid potential file descriptor leaks.

b) stat

The stat function is used to get the status of a file. It retrieves information about the file
specified by the given pathname.

Syntax:

int stat(const char *path, struct stat *buf);

• path: The path of the file.


• buf: A pointer to a struct stat where the information about the file will be stored.
• Returns: 0 on success, -1 on failure.

Usage: The stat function is used to obtain information such as the file's size, permissions,
last access time, and more.

c) lseek

The lseek function repositions the offset of the open file descriptor.

Syntax:

off_t lseek(int fd, off_t offset, int whence);

• fd: The file descriptor.


• offset: The offset to which the file offset should be set.
• whence: The directive indicating how the offset should be interpreted. Common
values are SEEK_SET (set file offset to offset), SEEK_CUR (set file offset to current
location plus offset), and SEEK_END (set file offset to the size of the file plus
offset).
• Returns: The resulting offset location as measured in bytes from the beginning of the
file, -1 on failure.

Usage: The lseek function is often used for random access within a file, allowing the
program to read or write data at any position.

d) ioctl

The ioctl (input/output control) function performs device-specific input/output operations.

Syntax:

int ioctl(int fd, unsigned long request, ...);

• fd: The file descriptor of the device.


• request: The device-dependent request code.
• ...: A variable number of arguments depending on the request code.
• Returns: Typically 0 on success, -1 on failure.

Usage: The ioctl function is used to control devices. The operations that can be performed
using ioctl are highly device-specific, such as querying device attributes, setting device
parameters, or issuing commands to the device. The request argument indicates the
operation to be performed, and the additional arguments provide any necessary data or
pointers.

2.Explain File Free Space management approaches?


3.Explain about file allocation methods?

1. Contiguous Allocation

Contiguous allocation requires each file to occupy a set of contiguous blocks on the disk.
Disk addresses define a linear ordering on the disk.

Characteristics

• Disk Address and Length: Each file is defined by the disk address and length (in
block units) of the first block. If the file is n blocks long and starts at location b, then
it occupies blocks b, b + 1, b + 2, ..., b + n - 1.
• Directory Entry: The directory entry for each file indicates the address of the starting
block and the length of the area allocated for the file.

Accessing a File

• Sequential Access: For sequential access, the file system remembers the disk address
of the last block referenced and reads the next block when necessary.
• Random Access: For direct access to block i of a file that starts at block b, block b +
i can be immediately accessed.

Drawbacks

• Finding Space for a New File: The system must find a contiguous block of free
space to allocate to the file. Common strategies for selecting a free hole include:
o First Fit: Allocates the first sufficiently large hole found.
o Best Fit: Allocates the smallest hole that is large enough.
• External Fragmentation: As files are allocated and deleted, free space is broken into
small pieces. This fragmentation can become problematic if the largest contiguous
chunk of free space is insufficient for a new file, even though there is enough total
free space scattered around.
Solutions to External Fragmentation

• Compaction: Copy the entire file system to another disk to consolidate free space
into one large contiguous area. Then, copy the files back onto the original disk,
allocating contiguous space from the consolidated free space.

Determining Space Needed for a File

• Initial Allocation: The total amount of space needed must be known and allocated
when the file is created.
• Potential Issues:
o Underestimation: If too little space is allocated, the file cannot be extended.
Solutions include:
▪ Terminating the program with an error message.
▪ Overestimating space needs, which can result in wasted space.
▪ Finding a larger hole, copying the file to the new space, and releasing
the previous space.
o Efficiency: Even if the total space needed is known in advance, preallocation
can be inefficient if the file grows slowly over time.

Modified Contiguous Allocation

To minimize the drawbacks of contiguous allocation, some operating systems use a modified
scheme:

• Initial Allocation: A contiguous chunk of space is allocated initially.


• Extents: If the initial amount of space is not large enough, additional contiguous
chunks of space (known as extents) are added as needed.

This approach aims to balance the need for contiguous space with the flexibility to grow files
without severe fragmentation or frequent copying.

2. Linked Allocation
• Linked allocation solves all problems of contiguous allocation. With linked
allocation, each file is a linked list of disk blocks; the disk blocks may be
scattered anywhere on the disk. The directory contains a pointer to the first and
last blocks of the file. For example, a file of five blocks might start at block 9
and continue at block 16, then block 1, then block 10, and finally block 25.
• To create a new file, we simply create a new entry in the directory. With linked
allocation, each directory entry has a pointer to the first disk block of the file.
This pointer is initialized to null (the end-of-list pointer value) to signify an
empty file. The size field is also set to 0.
• A write to the file causes the free-space management system to find a free block,
and this new block is written to and is linked to the end of the file.
• To read a file, we simply read blocks by following the pointers from block to
block.
Advantages

• No External Fragmentation: Any free block can be used to satisfy a request.


• Dynamic File Growth: The size of a file does not need to be declared when the file is
created. A file can grow as long as free blocks are available, eliminating the need for
disk space compaction.

Disadvantages

• Sequential Access Only: Linked allocation is efficient only for sequential access. To
find the i-th block of a file, we must start at the beginning of the file and follow the
pointers to the i-th block. Each pointer access requires a disk read, and some may
require a disk seek, making direct access inefficient.
• Pointer Overhead: Space is required for storing pointers. If a pointer requires 4 bytes
out of a 512-byte block, 0.78% of the disk is used for pointers rather than data. This
means each file requires slightly more space than it would otherwise.
• Reliability Issues: Files are linked together by pointers scattered across the disk. If a
pointer is lost or damaged, the file may become inaccessible or corrupted.

Variation on Linked Allocation


• An important variation on linked allocation is the use of a file-allocation table
(FAT). This simple but efficient method of disk-space allocation was used by the
MS-DOS operating system.
• A section of disk at the beginning of each volume is set aside to contain the table.
The table hasone entry for each disk block and is indexed by block number.
• The FAT is used in much the same way as a linked list. The directory entry contains
the block number of the first block of the file. The table entry indexed by that block
number contains the block number of the next block in the file. This chain
continues until it reaches the last block, which has a special end-of-file value as the
table entry.
• An unused block is indicated by a table value of 0. Allocating a new block to a file
is a simple matter of finding the first 0-valued table entry and replacing the previous
end-of-file value withthe address of the new block. The 0 is then replaced with the
end-of-file value.

3. Indexed Allocation
• Linked allocation solves the external-fragmentation and size-declaration
problems of contiguous allocation. However, in the absence of a FAT, linked
allocation cannot support efficient direct access, since the pointers to the blocks
are scattered with the blocks themselves all over the disk and must be retrieved
in order.
• Indexed allocation solves this problem by bringing all the pointers together
into one location: the index block.
• Each file has its own index block, which is an array of disk-block addresses.
The ith entry in the index block points to the ith block of the file. The directory
contains the address of the index block.
• To find and read the ith block, we use the pointer in the ith index-block entry.
• When the file is created, all pointers in the index block are set to null. When the
ith block is first written, a block is obtained from the free-space manager, and its
address is put in the ith index-block entry.
• Indexed allocation supports direct access, without suffering from external
fragmentation, because any free block on the disk can satisfy a request for more
space.
Advantages

• Direct Access: Supports efficient direct access by using the index block to locate any
block of the file directly.
• No External Fragmentation: Any free block on the disk can be used to satisfy space
requirements.
• Flexible File Growth: Files can grow dynamically as long as free blocks are
available.

Disadvantages

• Pointer Overhead: The index block requires storage for pointers, which can lead to
wasted space. The pointer overhead is generally greater than in linked allocation.

Mechanisms for Implementing Index Blocks

1. Linked Scheme

• Description: An index block is normally one disk block in size. For large files,
multiple index blocks can be linked together.
• Implementation: The index block contains a header with the file name and a set of
disk-block addresses. The last address in the index block is either null (for small files)
or a pointer to another index block (for large files).

2. Multilevel Index

• Description: Uses multiple levels of index blocks to handle large files.


• Implementation:
o First-Level Index: Points to a set of second-level index blocks.
o Second-Level Index: Points to the actual file data blocks.
o Further Levels: Can extend to third or fourth levels for very large files.
• Example: With 4,096-byte blocks, each index block can store 1,024 four-byte
pointers. Two levels of indexes allow for 1,048,576 data blocks, supporting a file size
of up to 4 GB.

3. Combined Scheme (UNIX-Based File Systems)

• Description: Combines direct, single indirect, double indirect, and triple indirect
pointers in the file's inode.
• Implementation:
o Direct Pointers: The first 12 pointers in the inode point directly to data
blocks, allowing access to small files (up to 48 KB if the block size is 4 KB)
without needing an index block.
o Single Indirect Pointer: Points to an index block containing addresses of data
blocks.
o Double Indirect Pointer: Points to an index block containing addresses of
other index blocks, which in turn point to data blocks.
o Triple Indirect Pointer: Points to an index block containing addresses of
further index blocks, eventually leading to the actual data blocks.

4.Explain file system interface and operations?

The file system interface provides a way for users and applications to interact with the files
stored on a computer. It includes the structures and commands necessary for creating,
managing, and accessing files and directories. Here are the key components and operations of
a file system interface

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 Structure
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.

When operating system defines different file structures, it also contains the code to support
these file structure. Unix, MS-DOS support minimum number of file structure.
File Attributes:
A file's attributes vary from one operating system to another but typically consist of these:

• Name: Name is the symbolic file name and is the only information kept in human
readable form.
• Identifier: This unique tag is a number that identifies the file within the file
system; it is in non-human-readable form of the file.
• Type: This information is needed for systems which support different types of files
or its format.
• Location: This information is a pointer to a device which points to the location of
the file on the device where it is stored.
• Size: The current size of the file (which is in bytes, words, etc.) which possibly the
maximum allowed size gets included in this attribute.
• Protection: Access-control information establishes who can do the reading,
writing, executing, etc.
• Date, Time & user identification: This information might be kept for the creation
of the file, its last modification and last used. These data might be useful for in the
field of protection, security, and monitoring its usage.
File Operations:
A file is an abstract data type. For defining 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. There are six basic file operations within an
Operating system. These are:

• Creating a file: There are two steps necessary for creating a file. First, space in the
file system must be found for the file. We discuss how to allocate space for the file.
Second, an entry for the new file must be made in the directory.
• Writing a file: To write to a file, you make a system call specify about both the
name of the file along with the information to be written to the file.
• Reading a file: To read from a file, you use a system call which specifies the name
of the file and where within memory the next block of the file should be placed.
• Repositioning inside a file: The directory is then searched for the suitable entry,
and the 'current-file-position' pointer is relocating to a given value. Relocating
within a file need not require any actual I/O. This file operation is also termed as
'file seek.'
• Deleting a file: For deleting a file, you have to search the directory for the specific
file. Deleting that file or directory release all file space so that other files can re-use
that space.
• Truncating a file: The user may wish for erasing the contents of a file but keep the
attributes same. Rather than deleting the file and then recreate it, this utility allows
all attributes to remain unchanged — except the file length — and let the user add
or edit the file content.
6.Briefly explain file access methods?
7.what is a file? Discuss its attributes?

A file is a named collection of related information that is recorded on secondary


storage.
(or)
A file is the smallest allotment of logical secondary storage.
(or)
A file is a sequence of bits, bytes, lines, or records, the meaning of which is defined by the
file’s creator and user. Many different types of information may be stored in a file.

File Attributes
File Attributes gives the Operating System information about the file and how it is
intended to use.A file’s attributes vary from one operating system to another but
typically consist of these:
• 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 isthe non-human-readable name for the file.
• Type. This information is needed for systems that support different types of files.
• Location. This information is a pointer to a device and to the location of the file
on that device.
• 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.

Some newer file systems also support extended file attributes, including
character encoding of the file and security features such as a file checksum.
8.Explain different directory structures with neat diagram?

Directory Structure
The most common schemes for defining the logical structure of a
directory are the
following,
1. Single-Level Directory
• The simplest directory structure is the single-level directory. All files are
contained in thesame directory, which is easy to support and understand.
• Limitations
➢ All files are in the same directory, they must have unique names. If two users
call their data file test.txt, then the unique-name rule is violated.
➢ Even a single user on a single-level directory may find it difficult to
remember the namesof all the files as the number of files increases. Keeping
track of so many files is a problem.

2. Two-Level Directory
• The standard solution to eliminate confusion of file names among different
users is to createa separate directory for each user.
• So the two level directory structure contains 2 directories
➢ Master File Directory (MFD) at the top level.
➢ User File Directory (UFD) at the second level and
➢ Actual files are at the third level.
• Each user has his own user file directory (UFD). When a user job starts or a
user logs in, the system’s master file directory (MFD) is searched. The MFD
is indexed by user name or account number, and each entry points to the UFD
for that user
• When a user refers to a particular file, only his own UFD is searched.
• To create a file for a user, the operating system searches only that user’s UFD
to ascertain whether another file of that name exists.
• To delete a file, the operating system confines its search to the local UFD; thus,
it cannot accidentally delete another user’s file that has the same name.

• Although the two-level directory structure solves the name-collision problem,


it still has disadvantages.
• This structure effectively isolates one user from another. Isolation is an
advantage when the users are completely independent but is a disadvantage
when the users want to cooperate on some task and to access one another’s files.
Some systems simply do not allow local user files to be accessed by other users.
• If access is permitted one user must have the ability to name a file in another
user’s directory. To name a file uniquely, the user must give both user name and
file name as Path Name.
• If user A wishes to access her own test file named test.txt, she can simply refer
to test.txt. To access the file named test.txt of user B (with directory-entry name
userb), however, she might have to refer to /userb/test.txt(windows os) and
/u/pbg/test(Unix, Linux).
• A special situation occurs with the system files. If a user wants them, they are
searched in USD if found ok if not found we should copy the system files into
each UFD but copying allthe system files would waste an enormous amount of
space.
• The standard solution is to use special user directory. Whenever a file name is
given to be loaded, the operating system first searches the local UFD. If the file
is found, it is used. If it is not found, the system automatically searches the
special user directory that contains the system files.
• The sequence of directories searched when a file is named is called the
search path.
3. Tree-Structured Directories
• A tree is the most common directory structure. The tree has a root directory,
and every filein the system has a unique path name.
• A directory (or subdirectory) contains a set of files or subdirectories. A directory
is simply another file, but it is treated in a special way. All directories have the
same internal format. One bit in each directory entry defines the entry as a file
(0) or as a subdirectory (1). Specialsystem calls are used to create and delete
directories.
• Current Directory
➢ Each process has a current directory. The current directory should contain
most of the files that are of current interest to the process.
➢ When reference is made to a file, the current directory is searched. If a file
is needed that is not in the current directory, then the user usually must
either specify a path nameor change the current directory (using change
directory () system call) to be the directory holding that file.

• Path Names
➢ It describes the path the OS must take to get to some point.
➢ Path names can be of two types: absolute and relative.
➢ An absolute path name begins at the root and follows a path down to the
specified file,giving the directory names on the path.
➢ A relative path name defines a path from the current directory.
➢ If the current directory is root/spell/mail, then the relative path name
prt/first refers to the same file as does the absolute path name
root/spell/mail/prt/first.
• Deletion of a directory
➢ If a directory is empty, its entry in the directory that contains it can simply be
deleted.
➢ However, suppose the directory to be deleted is not empty but contains
several files or subdirectories.
➢ One of two approaches can be taken. Some systems will not delete a
directory unless it is empty. Thus, to delete a directory, the user must first
delete all the files in that directory. If any subdirectories exist, this
procedure must be applied recursively to them, so that they can be deleted
also. This approach can result in a substantial amount of work.
➢ An alternative approach, such as that taken by the UNIX rm command, is
to provide an option: when a request is made to delete a directory, all that
directory’s files and subdirectories are also to be deleted.

4. Acyclic-Graph Directories
• A tree structure prohibits the sharing of files or directories. An acyclic
graph i.e., a graphwith no cycles which allows directories to share
subdirectories and files.
• The same file or subdirectory may be in two different directories. An
acyclic-graphdirectory structure is more flexible than a simple tree structure,
but it is also more complex.

• Implementation
a. Link: A common way is to create a new directory entry called a link. A link
is effectively a pointer to another file or subdirectory. A link may be
implemented as an absolute or a relative path name. When a reference to a
file is made, we search the directory. If the directory entry is marked as a
link, then the name of the real file is included in the link information. We
resolve the link by using that path name to locate the real file.
b. Duplication: Shared files duplicate all information about them in both
sharing directories. Thus, both entries are identical and equal. A major
problem with duplicate directory entries is maintaining consistency when a
file is modified.
9.What is file system structure and draw the file system structure architecture?
10.Explain the usage of open, create, read, write, systemcalls ?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy