Unit5 OS
Unit5 OS
File System Interface and Operations -Access methods, Directory Structure, Protection, File
System Structure, Allocation methods, Free-space Management. Usage of open, create, read,
write, close, lseek, stat, ioctl system calls.
FILE ATTRIBUTES:
Name: A file is named for the convenience of the user and is referred by its name. A
name is usually a string of characters.
Identifier: This unique tag, usually a number, identifies the file within the file system.
Type: Files are of so many types. The type depends on the extension of the file. The
different types of files details are given in below table.
Location: This is a pointer to the location of the file on storage device.
Size: The current size of the file (in bytes, words, blocks).
Protection: Access control information determines who can do reading, writing,
executing and so on.
Time, Date, User identification: This information may be kept for creation, last
modification, and last use.
FILE OPERATIONS
Creating a file: First OS check whether free space is available or not. If there is no free
space available, file can not be created. If the space is available then file is created and an
FILE TYPES:
The name of the file consists of 2 parts. One is name and second is extension. The file type is
depending on extension of the file. The extension of the file defines what type of file it is and
with what application software is used to open or run it.
File Type Extension Purpose
Executable .exe , .com , .bin Ready to run machine language program
Source code .c , .cpp , .asm Source code in various languages.
Object .obj , .o Compiled, machine language not linked
Batch .bat , .sh Commands to the command interpreter
Text .txt , .doc Textual data, documents
Word processor .doc , .wp , .rtf Various word processors or formats
Library .lib , .dll Library routines for programmers
.gif , .pdf , .jpg ASCII or Binary file in a format for
Print or View
printing or viewing
.arc , .zip Related files grouped into one file, sometimes
Archive
compressed for archiving or storage
Multimedia .mpeg , .mp3, .avi Binary file containing audio or A/V information
For example, consider a file consisting of 100 records. Let the current position of read/write
head is at 45th record. Suppose next we want to read the 75th record then, it access sequentially
from 45, 46, 47 …….. 74, 75. Even though after 45th we need 75th, the read/write head traverse
all the records from 45 to 75. So, it is a time consuming method.
Consider other example, let CD consists of 10 songs and at present we are listening song 3, if
we want to listen song 7, we can shift to 7. (In case of tape record cassette it is not possible)
This access method is a combination of both the sequential access as well as direct access. The
file contains set of records and a group of records form a block. The main concept is to perform
direct access on the blocks and then sequential access on the records in a block. This access
method involves maintaining an index. The index is similar to an index in the text book. The
index contains 2 fields, a pointer field and a key field. To access data from a file, direct access is
performed to locate the block using pointer field and within the block the record is accessed
sequentially. Sometimes indexes may be big. So hierarchies of indexes are built in which one
direct access of an index leads to info to access another index directly and so on till the actual
file is accessed sequentially for the particular record.
a) Absolute Path: It Begins at root and follows a path down to the specified file.
Ex: \user2\programs\a1.java
4. PROTECTION
The information stored in a computer system should be protected from improper access.
Protection mechanisms provide controlled access by limiting types of file access that can be
made. Access is permitted or denied depending on several factors, such as the user type, the
access type requested.
Number of passwords that a user needs to remember may become large, if different
passwords set to different files.
If only one password is used for all files, then once it is discovered, all files are
accessible.
When an application program asks for a file, the first request is directed to the logical file
system. The logical file system contains the Meta data of the file and directory structure.
It maintains file structure via file control blocks. A file control block (inode in Unix file
systems) contains information about the file, ownership, permissions, location of the file
contents. If the application program doesn't have the required permissions of the file then
this layer will throw an error. Logical file systems also verify the path to the file.
Files are to be stored and retrieved from the hard disk. Hard disk is divided into various
tracks. Each track is divided into sectors. Each sector is divided into blocks. The file
content is divided into various logical blocks. Each logical block is mapped and stored
into Hard disk blocks. Therefore, in order to store and retrieve the files, the logical blocks
need to be mapped to physical blocks. This mapping is done by File organization module.
It is also responsible for free space management.
When you switch on computer, a special program called as BIOS stored in ROM is
executed by processor. BIOS contain the code to access the very first partition of hard disk called
Master boot record (MBR). The MBR contains the information regarding how and where the
Operating system is located in the hard disk so that it can be booted into the RAM. If the disk
does not contain an OS, this block can be empty.MBR also includes a partition table which
locates every partition in the hard disk.
Volume control block contain all the information regarding that volume such as number of
blocks, size of each block, partition table, pointers to free blocks and free FCB blocks. In UNIX
A directory structure (per file system) contains file names and pointers to corresponding File
Control Blocks (FCBs). In UNIX, it includes inode numbers associated to file names.
File Control block contains all the details about the file such as ownership details, permission
details, file size, etc. In UFS, this detail is stored in inode. In NTFS, this information is stored
inside master file table as a relational database structure. A typical file control block is shown in
the image below.
File Permissions
File Dates (Create, Access, Write)
File Owner, Group, ACL
File Size
File Data Blocks
Figure: File Control Block
When an application program needs a file, first, it must be opened. The open( ) call
passes a file name to the logical file system. The open( ) system call first searches the system
wide open file table to see if the file is already in use by another process. If it is, a per process
open file table entry is created pointing to the existing system wide open file table. If the file is
not already open, the directory structure is searched for the given file name. Once the file is
found, FCB is copied into a system wide open file table in memory. This table not only stores the
FCB but also tracks the number of processes that are using the file.
Next, an entry is made in the per – process open file table, with the pointer to the entry in
the system wide open file table and some other fields. These are the fields include a pointer to
the current location in the file ( for the next read/write operation) and the access mode in which
the file is open. The open () call returns a pointer to the appropriate entry in the per-process file
system table. All file operations are preformed via this pointer. When a process closes the file,
the per- process table entry is removed. And the system wide entry open count is decremented.
When all users that have opened the file completed their task then the file is closed, any updated
i. Contiguous allocation:
A single continuous set of blocks are allocated to a file at the time of file creation.
When a file need more than one block, a linked list of blocks is maintained.
In this each block contains a pointer to the next block in the chain and last block contains
NULL pointer.
The directory maintains the file names with the starting and ending blocks as shown in
the diagram.
The file allocation table contains a separate one-level index for each file.
We can get all the block numbers allocated to a file from the index block.
The directory structure contain file name and its associated index block.
Let us consider the instance of disk blocks on the disk shown in the Figure 1 (where
white blocks are allocated and grey blocks are free) can be represented by a bitmap of 32 bits as:
Disk Block No
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0
Bit Vector
The bit vector: 00111100111111000110000001110000
The main advantage of the bitmap is that it is simple to understand and efficient in finding the
free blocks in the disk.
iii. Grouping
This approach forms groups based on the contiguous free blocks.
The first free block stores the address of first group of contiguous free blocks.
The last free block in the first group stores the address of second group of contiguous free
blocks and so on.
An advantage of this approach is that the addresses of a group of free disk blocks can be
found easily.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *path, int flags, [mode_t mod]);
The number of arguments in this function can be two or three. The third argument is used
only when creating a new file. When we want to open an existing file only two arguments are
used. The function returns the smallest available file descriptor (fd value 0, 1 and 2 are reserved
The argument “path” specifies the name of the file, while “mod” defines the access
rights. The access rights are given in the above topic ( open( ) system call ). If the file to be created
does not exist, a new i-node is allocated and a link is added to the directory. If the file exists, it
loses its contents and it will be opened for writing. In this case, the second argument is ignored
and the old ownership and the access permissions are not modified. This system call returns the
#include <unistd.h>
ssize_t read(int fd, void* buf, size_t noct);
It reads “noct” bytes from the opened file referred by the file descriptor “fd” and it puts those
read bytes into a buffer “buf”. The pointer (current position) is incremented automatically after a
reading the given amount of bytes. The function returns the number of bytes read, 0 for end of
file (EOF) and -1 in case an error occurred.
It writes “noct” bytes from the buffer “buf” into the opened file referred by the file descriptor
“fd”. The function returns the number of bytes written or -1 in case of an error.
The function returns 0 in case of successfully closing the file and -1 in case of an error. When the
process terminated, all the files opened by it are closed automatically.
#include <unistd.h>
off_t lseek(int fd, off_t offset, int ref);
The first argument “fd” refers file descriptor of an opened file. The second argument “offset”
refers number of positions to be moved. The third argument “ref” gives the position from where
the displacement of file pointer to be done.
If “ref” is set to SEEK_SET the positioning is done from the beginning of the file.
If “ref” is set to SEEK_CUR the positioning is done from the current position.
If “ref” is set to SEEK_END then the positioning is done from the end of the file.
The function returns the new current position after displacement from the given file or -1 in case
of an error.
The first argument “path” gives the file name. The second argument “buf” is used to
store the file attributes read from the given i-node of a file. The file attributes can be file access
types, owner, file size, last access time, last modified time, etc. On success, the functions return
zero, and on error, −1 is returned. The structure struct stat is described in the sys/stat.h header and
has the following fields:
struct stat {
mode_t st_mode; /* file access types and rights */
ino_t st_ino; /* i-node */
dev_t st_dev; /* identifier of device containing file */
nlink_t st_nlink; /* nr of links */
uid_t st_uid; /* owner ID */
gid_t st_gid; /* group ID */
off_t st_size; /* ordinary file size */
time_t st_atime; /* last time it was accessed */
time_t st_mtime; /* last time it was modified */
time_t st_ctime; /* last time settings were changed */
IOCTL is referred as Input and Output Control. The system call ioctl( ) is used to interact with
device driver files. The major use of this is to handle some specific operations of a device for
which the kernel does not have a system call by default. It manipulates the underlying device
parameters of device driver files. Some real time applications of ioctl( ) are:
The first argument “fd” is a file descriptor of an opened file. The ioctl command needs to be
executed on this opened file, which would generally be device files. The second argument
“request” is a device-dependent request code. The request code varies from device to device.
The ioctl command implements the task associated with request code to achieve the desired
functionality. The third argument is an untyped pointer to memory. It's traditionally char
*argp. An ioctl( ) request has encoded in it whether the argument is an in parameter or out
parameter, and the size of the argument argp in bytes. Macros and defines used in specifying
an ioctl( ) request are located in the file <sys/ioctl.h>. Usually, on success zero or
positive value is returned. On error, -1 is returned.