Os Unit 5
Os Unit 5
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.
∙ Files are stored on disk or other storage and do not disappear when a
user logs off.
∙ Files have names and are associated with access permission that
permits controlled sharing.
∙ Files could be arranged or more complex structures to reflect the
relationship between them.
Operating Systems - Unit 5 GNIT, Hyderabad.
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 Type
File type refers to the ability of the operating system to distinguish different types of file
such as text files source files and binary files etc. Many operating systems support
many types of files. Operating system like MS-DOS and UNIX have the following types
of files −
Ordinary files
Directory files
∙ These files contain list of file names and other information related to these files.
Special files
Functions of File
∙ Create file, find space on disk, and make an entry in the directory. ∙
Write to file, requires positioning within the file
∙ Read from file involves positioning within the file
∙ Delete directory entry, regain disk space.
∙ Reposition: move read/write position.
∙ 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.
Direct/Random access
Space Allocation
Files are allocated disk spaces by operating system. Operating systems deploy
following three main ways to allocate disk space to files.
∙ Contiguous Allocation
∙ Linked Allocation
∙ Indexed Allocation
Contiguous Allocation
Linked Allocation
Indexed Allocation
SINGLE-LEVEL DIRECTORY
In this a single directory is maintained for all the users.
Naming problem: Users cannot have same name for two files. ∙
Grouping problem: Users cannot group files according to their need.
TREE-STRUCTURED DIRECTORY :
Directory is maintained in the form of a tree. Searching is efficient and also
Disadvantage
∙ External fragmentation will occur, making it difficult to find contiguous blocks of
space of sufficient length. Compaction algorithm will be necessary to free up
additional space on disk.
∙ Also, with pre-allocation, it is necessary to declare the size of the file at the
time of creation.
2. Linked Allocation(Non-contiguous allocation) : Allocation is on an
individual block basis. Each block contains a pointer to the next block in the
Operating Systems - Unit 5 GNIT, Hyderabad.
chain. Again the file table needs just a single entry for each file, showing the
starting block and the length of the file. Although pre-allocation is possible, it is
more common simply to allocate blocks as needed. Any free block can be
added to the chain. The blocks need not be continuous. Increase in file size is
always possible if free disk block is available. There is no external
fragmentation because only one block at a time is needed but there can be
internal fragmentation but it exists only in the last disk block of file.
Disadvantage:
∙ Internal fragmentation exists in last disk block of file.
∙ There is an overhead of maintaining the pointer in every disk block. ∙ If
the pointer of any disk block is lost, the file will be truncated. ∙ It
supports only the sequencial access of files.
3. Indexed Allocation:
It addresses many of the problems of contiguous and chained allocation. In this
case, the file allocation table contains a separate one-level index for each file:
The index has one entry for each block allocated to the file. Allocation may be on
the basis of fixed-size blocks or variable-sized blocks. Allocation by blocks
eliminates external fragmentation, whereas allocation by variable-size blocks
improves locality. This allocation technique supports both sequential and direct
Operating Systems - Unit 5 GNIT, Hyderabad.
access to the file and thus is the most popular form of file allocation.
Disk Free Space Management
Just as the space that is allocated to files must be managed ,so the space that is
not currently allocated to any file must be managed. To perform any of the file
allocation techniques,it is necessary to know what blocks on the disk are
available. Thus we need a disk allocation table in addition to a file allocation
table.The following are the approaches used for free space management.
1. Bit Tables : This method uses a vector containing one bit for each block on
the disk. Each entry for a 0 corresponds to a free block and each 1
corresponds to a block in use.
For example: 00011010111100110001
In this vector every bit correspond to a particular block and 0 implies that,
that particular block is free and 1 implies that the block is already occupied.
A bit table has the advantage that it is relatively easy to find one or a
contiguous group of free blocks. Thus, a bit table works well with any of the
file allocation methods. Another advantage is that it is as small as possible.
2. Free Block List : In this method, each block is assigned a number
sequentially and the list of the numbers of all free blocks is maintained in a
reserved block of the disk.
Operating Systems - Unit 5 GNIT, Hyderabad.
There are several logical structures of a directory, these are given below.
∙ 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. if two
users call their dataset test, then the unique name rule violated.
Advantages:
∙ We can give full path like /User-name/directory-name/.
∙ Different users can have the same directory as well as the file name. ∙
Searching of files becomes easier due to pathname and user-grouping.
Disadvantages:
∙ A user is not allowed to share files with other users.
Operating Systems - Unit 5 GNIT, Hyderabad.
∙ Still,
it not very scalable, two files of the same type cannot be grouped
together in the same user.
∙ Tree-structured directory –
Once we have seen a two-level directory as a tree of height 2, the natural
generalization is to extend the directory structure to a tree of arbitrary
height.
This generalization allows the user to create their own subdirectories and to
organize their files accordingly.
A tree structure is the most common directory structure. The tree has a root directory,
and every file in the system has a unique path.
Advantages:
∙ Very general, since full pathname can be given.
∙ Very scalable, the probability of name collision is less.
∙ Searching becomes very easy, we can use both absolute paths as well as
relative.
Disadvantages:
Operating Systems - Unit 5 GNIT, Hyderabad.
∙ Every file does not fit into the hierarchical model, files may be saved into
multiple directories.
∙ We can not share files.
∙ It is inefficient, because accessing a file may go under multiple directories.
Disadvantages:
∙ This method suffers from both internal and external fragmentation. This makes it
inefficient in terms of memory utilization.
∙ Increasing file size is difficult because it depends on the availability of contiguous
memory at a particular instance.
2. Linked List Allocation
Operating Systems - Unit 5 GNIT, Hyderabad.
In this scheme, each file is a linked list of disk blocks which need not be contiguous.
The disk blocks can be scattered anywhere on the disk.
The directory entry contains a pointer to the starting and the ending file block. Each
block contains a pointer to the next block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The
last block (25) contains -1 indicating a null pointer and does not point to any other
block.
Advantages:
∙ This is very flexible in terms of file size. File size can be increased easily since the
system does not have to look for a contiguous chunk of memory.
16
∙ This method does not suffer from external fragmentation. This makes it relatively
better in terms of memory utilization.
Disadvantages:
∙ Because the file blocks are distributed randomly on the disk, a large number of
seeks are needed to access every block individually. This makes linked allocation
slower.
∙ It does not support random or direct access. We can not directly access the blocks
of a file. A block k of a file can be accessed by traversing k blocks sequentially
(sequential access ) from the starting block of the file via block pointers.
Advantages:
∙ This supports direct access to the blocks occupied by the file and therefore
provides fast access to the file blocks.
∙ It overcomes the problem of external fragmentation.
Disadvantages:
∙ The pointer overhead for indexed allocation is greater than linked allocation. ∙ For
very small files, say files that expand only 2-3 blocks, the indexed allocation would
keep one entire block (index block) for the pointers which is inefficient in terms of
memory utilization. However, in linked allocation we lose the space of only 1
pointer per block.
What is a File ?
A file can be defined as a data structure which stores the sequence of records. Files are
stored in a file system, which may exist on a disk or in the main memory. Files can be
simple (plain text) or complex (specially-formatted).
The collection of files is known as Directory. The collection of directories at the different
levels, is known as File System.
Attributes of the File
1.Name
Every file carries a name by which the file is recognized in the file system. One directory
cannot have two files with the same name.
2.Identifier
Along with the name, Each File has its own extension which identifies the type of the file.
For example, a text file has the extension .txt, A video file can have the extension .mp4.
3.Type
In a File System, the Files are classified in different types such as video files, audio files,
text files, executable files, etc.
4.Location
In the File System, there are several locations on which, the files can be stored. Each
file carries its location as its attribute.
5.Size
The Size of the File is one of its most important attribute. By size of the file, we mean the
number of bytes acquired by the file in the memory.
6.Protection
The Admin of the computer may want the different protections for the different files. Therefore
each file carries its own set of permissions to the different group of Users.
Every file carries a time stamp which contains the time and date on which the file is last
modified.
1.Create
Creation of the file is the most important operation on the file. Different types of files are
created by different methods for example text editors are used to create a text file, word
processors are used to create a word file and Image editors are used to create the
image files.
2.Write
3.Read
Every file is opened in three different modes : Read, Write and append. A Read pointer
is maintained by the OS, pointing to the position up to which, the data has been read.
4.Re-position
Re-positioning is simply moving the file pointers forward or backward depending upon
the user's requirement. It is also called as seeking.
5.Delete
Deleting the file will not only delete all the data stored inside the file, It also deletes all
the attributes of the file. The space which is allocated to the file will now become
available and can be allocated to the other files.
6.Truncate
Truncating is simply deleting the file except deleting attributes. The file is not completely
deleted although the information stored inside the file get replaced.
In sequential access, the OS read the file word by word. A pointer is maintained which
initially points to the base address of the file. If the user wants to read first word of the
file then the pointer provides that word to the user and increases its value by 1 word.
This process continues till the end of the file.
Modern word systems do provide the concept of direct access and indexed access but
the most used method is sequential access due to the fact that most of the files such as
text files, audio files, video files, etc need to be sequentially accessed.
Direct Access
Suppose every block of the storage stores 4 records and we know that the record we
needed is stored in 10th block. In that case, the sequential access will not be
implemented because it will traverse all the blocks in order to access the needed record.
Direct access will give the required result despite of the fact that the operating system
has to perform some complex tasks such as determining the desired block number.
However, that is generally implemented in database applications.
Indexed Access
In index accessing, searching in a large database became very quick and easy but we
need to have some extra space in the memory to store the index value.
Directory Structure
Each partition must have at least one directory in which, all the files of the partition can
be listed. A directory entry is maintained for each file in the directory which stores all the
information related to that file.
A directory can be viewed as a file which contains the Meta data of the bunch of
1. File Creation
2. Search for the file
Advantages
1. Implementation is very simple.
2. If the sizes of the files are very small then the searching becomes faster. 3. File
creation, searching, deletion is very simple since we have only one directory.
Disadvantages
1. We cannot have two files with the same name.
Every Operating System maintains a variable as PWD which contains the present
directory name (present user name) so that the searching can be done appropriately.
Tree Structured Directory
In Tree structured directory system, any directory entry can either be a file or sub
directory. Tree structured directory system overcomes the drawbacks of two level
directory system. The similar kind of files can now be grouped in one directory.
Each user has its own directory and it cannot enter in the other user's directory.
However, the user has the permission to read the root's data but he cannot write or
modify this. Only administrator of the system has the complete access of root directory.
Searching is more efficient in this directory structure. The concept of current working
directory is used. A file can be accessed by two types of path, either relative or absolute.
Absolute path is the path of the file with respect to the root directory of the system while
relative path is the path with respect to the current working directory of the system. In
tree structured directory systems, the user is given the privilege to create the files as
well as directories.
The permissions are R W X which are regarding reading, writing and the execution of
the files or directory. The permissions are assigned to three types of users: owner, group
and others.
There is a identification bit which differentiate between directory and file. For a directory,
it is d.
These kinds of directory graphs can be made using links or aliases. We can have
multiple paths for a same file. Links can either be symbolic (logical) or hard link
(physical).
2. In the case of hard link, the actual file will be deleted only if all the references to it
gets deleted.
Operating Systems -
File Systems
File system is the part of the operating system which is responsible for file management.
It provides a mechanism to store the data and access to the file contents including data
and programs. Some Operating systems treats everything as a file for example Ubuntu.
o File Structure
We have seen various data structures in which the file can be stored. The task of
the file system is to maintain an optimal file structure.
Whenever a file gets deleted from the hard disk, there is a free space created in
the disk. There can be many such spaces which need to be recovered in order to
reallocate them to other files.
The major concern about the file is deciding where to store the files on the hard
disk. There are various disks scheduling algorithm which will be covered later in
this tutorial.
A File may or may not be stored within only one block. It can be stored in the non
contiguous blocks on the disk. We need to keep track of all the blocks on which
the part of the files reside.
Most of the Operating Systems use layering approach for every task including file
systems. Every layer of the file system is responsible for some activities.
The image shown below, elaborates how the file system is divided in different layers,
and also the functionality of each layer.
o 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.
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.
o Generally, files are divided into various logical blocks. Files are to be stored in the hard
disk and to be retrieved from the hard disk. Hard disk is divided into various tracks and
sectors. 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.
o Once File organization module decided which physical block the application program
needs, it passes this information to basic file system. The basic file system is responsible
for issuing the commands to I/O control in order to fetch those blocks.
o I/O controls contain the codes by using which it can access hard disk. These codes are
known as device drivers. I/O controls are also responsible for handling interrupts.
MBR is sometimes called master partition table because it includes a partition table
which locates every partition in the hard disk.
Master boot record (MBR) also includes a program which reads the boot sector record
of the partition that contains operating system.
What happens when you turn on your computer?
Due to the fact that the main memory is volatile, when we turn on our computer, CPU
cannot access the main memory directly. However, there is a special program called as
BIOS stored in ROM is accessed for the first time by the CPU.
BIOS contains the code, by executing which, the CPU access the very first partition of
hard disk that is MBR. It contains a partition table for all the partitions of the hard disk.
Since, MBR contains the information about where the operating system is being stored
and it also contains a program which can read the boot sector record of the partition,
hence the CPU fetches all this information and load the operating system into the main
memory.
Boot Control Block contains all the information which is needed to boot an
operating system from that volume. It is called boot block in UNIX file system. In
NTFS, it is called the partition boot sector.
2. Volume Control Block
Volume control block 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 file system, it is known as super block. In NTFS, this information
is stored inside master file table.
A directory structure (per file system) contains file names and pointers to
corresponding 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.
The in-memory data structures are used for file system management as well as
performance improvement via caching. This information is loaded on the mount time
and discarded on ejection.
In-memory mount table contains the list of all the devices which are being
mounted to the system. Whenever the connection is maintained to a device, its
entry will be done in the mount table.
This is the list of directory which is recently accessed by the CPU. The directories
present in the list can also be accessed in the near future so it will be better to
store them temporally in cache.
This is the list of all the open files in the system at a particular time. Whenever the
user open any file for reading or writing, the entry will be made in this open file
table.
It is the list of open files subjected to every process. Since there is already a list
which is there for every open file in the system thereforeIt only contains Pointers
to the appropriate entry in the system wide table.
Directory Implementation
There is the number of algorithms by using which, the directories can be implemented.
However, the selection of an appropriate directory implementation algorithm may
significantly affect the performance of the system.
The directory implementation algorithms are classified according to the data structure
they are using. There are mainly two algorithms which are used in these days.
1. Linear List
In this algorithm, all the files in a directory are maintained as singly lined list. Each file
contains the pointers to the data blocks which are assigned to it and the next file in the
directory.
Characteristics
1. When a new file is created, then the entire list is checked whether the new file name is
matching to a existing file name or not. In case, it doesn't exist, the file can be created at
the beginning or at the end. Therefore, searching for a unique name is a big concern
because traversing the whole list takes time.
2. The list needs to be traversed in case of every operation (creation, deletion, updating,
etc) on the files therefore the systems become inefficient.
2. Hash Table
To overcome the drawbacks of singly linked list implementation of directories, there is
an alternative approach that is hash table. This approach suggests to use hash table
along with the linked lists.
A key-value pair for each file in the directory gets generated and stored in the hash
table. The key can be determined by applying the hash function on the file name while
the key points to the corresponding file stored in the directory.
Now, searching becomes efficient due to the fact that now, entire list will not be
searched on every operating. Only hash table entries are checked using the key and if
an entry found then the corresponding file will be fetched using the value.
Allocation Methods
There are various methods which can be used to allocate disk space to the files.
Selection of an appropriate allocation method will significantly affect the performance
and efficiency of the system. Allocation method provides a way in which the disk will be
utilized and the files will be accessed.
Contiguous Allocation
If the blocks are allocated to the file in such a way that all the logical blocks of the file
get the contiguous physical block in the hard disk then such allocation scheme is known
as contiguous allocation.
In the image shown below, there are three files in the directory. The starting block and
the length of each file are mentioned in the table. We can check in the table that the
contiguous blocks are assigned to each file as per its need.
Disadvantages
1. The disk will become fragmented.
2. It may be difficult to have a file grow.
Advantages
1. There is no external fragmentation with linked allocation.
2. Any free block can be utilized in order to satisfy the file block requests.
3. File can continue to grow as long as the free blocks are available. 4.
Directory entry will only contain the starting block address.
Disadvantages
1. Random Access is not provided.
2. Pointers require some space in the disk blocks.
3. Any of the pointers in the linked list must not be broken otherwise the file will get
corrupted.
4. Need to traverse each block.
File Allocation Table overcomes this drawback of linked list allocation. In this scheme, a
file allocation table is maintained, which gathers all the disk block links. The table has
one entry for each disk block and is indexed by block number.
File allocation table needs to be cached in order to reduce the number of head seeks.
Now the head doesn't need to traverse all the disk blocks in order to access one
successive block.
It simply accesses the file allocation table, read the desired block entry from there and
access that block. This is the way by which the random access is accomplished by using
FAT. It is used by MS-DOS and pre-NT Windows versions.
Advantages
Disadvantages
1. Each Disk block needs a FAT entry.
2. FAT size may be very big depending upon the number of FAT entries. 3. Number
of FAT entries can be reduced by increasing the block size but it will also increase
Internal Fragmentation.
Indexed Allocation
Limitation of FAT
Limitation in the existing technology causes the evolution of a new technology. Till now,
we have seen various allocation methods; each of them was carrying several
advantages and disadvantages.
File allocation table tries to solve as many problems as possible but leads to a
drawback. The more the number of blocks, the more will be the size of FAT.
Therefore, we need to allocate more space to a file allocation table. Since, file allocation
table needs to be cached therefore it is impossible to have as many space in cache.
Here we need a new technology which can solve such problems.
Disadvantages
1. A bad index block could cause the lost of entire file.
2. Size of a file depends upon the number of pointers, a index block can hold.
3. Having an index block for a small file is totally wastage.
4. More pointer overhead
For the larger files, the last entry of the index block is a pointer which points to another
index block. This is also called as linked schema.
o The outer level index is used to find the inner level index.
Operating
Systems - Unit 5 GNIT, Hyderabad.
Inode
In UNIX based operating systems, each file is indexed by an Inode. Inode are the
special disk block which is created with the creation of the file system. The number of
files or directories in a file system depends on the number of Inodes in the file system.
1. Attributes (permissions, time stamp, ownership details, etc) of the file 2. A number of
direct blocks which contains the pointers to first 12 blocks of the file.
3. A single indirect pointer which points to an index block. If the file cannot be indexed
entirely by the direct blocks then the single indirect pointer is used. 4. A double indirect
pointer which points to a disk block that is a collection of the pointers to the disk blocks
which are index blocks. Double index pointer is used if the file is too big to be indexed
entirely by the direct blocks as well as the single indirect pointer.
5. A triple index pointer that points to a disk block that is a collection of pointers. Each
of the pointers is separately pointing to a disk block which also contains a collection of
pointers which are separately pointing to an index block that contains the pointers to
the file blocks.
inode video
Operating Systems - Unit 5 GNIT, Hyderabad.
Disk Scheduling Algorithms
Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk.
Disk scheduling is also known as I/O scheduling.
Disk scheduling is important because:
∙Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by the disk controller. Thus other I/O requests need to wait in the
waiting queue and need to be scheduled.
∙Two or more request may be far from each other so can result in greater disk arm
movement.
∙Hard drives are one of the slowest parts of the computer system and thus need to be
accessed in an efficient manner.
There are many Disk Scheduling Algorithms but before discussing them let’s have a quick look
at some of the important terms:
∙Seek Time:Seek time is the time taken to locate the disk arm to a specified track where
the data is to be read or write. So the disk scheduling algorithm that gives minimum
average seek time is better.
∙Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to
rotate into a position so that it can access the read/write heads. So the disk
scheduling algorithm that gives minimum rotational latency is better.
∙Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating
speed of the disk and number of bytes to be transferred.
∙Disk Access Time: Disk Access Time is:
Disk Access Time = Seek Time + Rotational Latency + Transfer Time
∙Disk
Response Time: Response Time is the average of time spent by a request waiting to perform
its I/O operation. Average Response time is the response time of the all
FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are
addressed in the order they arrive in the disk queue. Let us understand this with the help of an
example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
Seek time is
calculated as:
=(199-50)+(199-0)+(43-0)
=391
Advantages:
∙Provides more uniform wait time compared to SCAN
So, the
seek time is calculated as:
=(190-50)+(190-16)
=314
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm
is at 50, and it is also given that the disk arm should move “towards the larger value”
So, the
seek time is calculated as:
=(190-50)+(190-16)+(43-16)
=341
Operating
Systems - Unit 5 GNIT, Hyderabad.
N-STEP SCAN – It is also known as N-STEP LOOK algorithm. In this a buffer is created for N
requests. All requests belonging to a buffer will be serviced in one go. Also once the buffer is full
no new requests are kept in this buffer and are sent to another one. Now, when these N
requests are serviced, the time comes for another top N requests and this way all get requests
get a guaranteed service
Advantages
∙ It eliminates starvation of requests completely
FSCAN– This algorithm uses two sub-queues. During the scan all requests in the first queue
are serviced and the new incoming requests are added to the second queue. All new requests
are kept on halt until the existing requests in the first queue are serviced. Advantages
∙ FSCAN along with N-Step-SCAN prevents “arm stickiness” (phenomena in I/O
scheduling where the scheduling algorithm continues to service requests at or
near the current sector and thus prevents any seeking)
Advantages:
∙ Whenever a file is to be allocated a free block, the operating system can simply allocate
the first block in free space list and move the head pointer to the next free block in the
list.
Disadvantages:
4. Counting
Counting is another approach for free space management. Generally, some contiguous blocks
are allocated but some are free simultaneously. When the free space is allocated to a process
according to the contiguous allocation algorithm or clustering. So we cannot keep the list of n
free block address but we can keep the address of the first free block and then the numbers of n
free contiguous block which follows the first block. When there is an entry in the free space list it
For the Figure-1, we scan the bitmap sequentially for the first non-zero word. The
first group of 8 bits (00001110) constitute a non-zero word since all bits are not 0.
After the non-0 word is found, we look for the first 1 bit. This is the 5th bit of the
non-zero word. So, offset = 5.
Therefore, the first free block number = 8*0+5 = 5.
2. Linked List –
In this approach, the free disk blocks are linked together i.e. a free block contains a
pointer to the next free block. The block number of the very first disk block is stored
at a separate location on disk and is also cached in memory.
In Figure-2, the free space list head points to Block 5 which points to Block 6, the
next free block and so on. The last free block would contain a null pointer indicating
the end of free list.
A drawback of this method is the I/O required for free space list traversal.
Operating Systems - Unit 5 GNIT, Hyderabad.
3. Grouping –
This approach stores the address of the free blocks in the first free block. The first free
block stores the address of some, say n free blocks. Out of these n blocks, the first n-1
blocks are actually free and the last block contains the address of next free n blocks.
An advantage of this approach is that the addresses of a group of free disk blocks can
be found easily.
4. Counting –
This approach stores the address of the first free disk block and a number n of free
contiguous disk blocks that follow the first block.
Every entry in the list would contain:
1. Address of first free disk block
2. A number n
For example, in Figure-1, the first entry of the free space list would be: ([Address of
Block 5], 2), because 2 contiguous free blocks follow block 5.
Operating Systems - Unit 5 GNIT, Hyderabad.
Input-output system calls in C | Create, Open, Close, Read, Write
Important Terminology
What is the File Descriptor?
File descriptor is integer that uniquely identifies an open file of the process. File Descriptor
table: File descriptor table is the collection of integer array indices that are file descriptors in
which elements are pointers to file table entries. One unique file descriptors table is provided in
operating system for each process.
File Table Entry: File table entries is a structure In-memory surrogate for an open file, which is
created when process request to opens file and these entries maintains file position.
Standard File Descriptors: When any process starts, then that process file descriptors table’s
fd(file descriptor) 0, 1, 2 open automatically, (By default) each of these 3 fd references file table
entry for a file named /dev/tty
/dev/tty: In-memory surrogate for the terminal
Write to stdout => write to fd 1 : Whenever we see any output to the video screen, it’s from
the file named /dev/tty and written to stdout in screen through fd 1.
Write to stderr => write to fd 2 : We see any error to the video screen, it is also from that file
write to stderr in screen through fd 2.
// C program to illustrate
// open system call
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
extern int errno;
int main()
{
// if file does not have in directory
// then file foo.txt is created.
int fd = open("foo.txt", O_RDONLY | O_CREAT);
Output:
fd = 3
Output:
opened the fd = 3
closed the fd.
Output:
fd2 = 3
Here, In this code first open() returns 3 because when main process created, then fd 0, 1, 2 are
already taken by stdin, stdout and stderr. So first unused file descriptor is 3 in file descriptor
table. After that in close() system call is free it this 3 file descriptor and then after set 3 file
descriptor as null. So when we called second open(), then first unused fd is also 3. So, output
of this program is 3.
Operating Systems - Unit 5 GNIT, Hyderabad.
4. read: From the file indicated by the file descriptor fd, the read() function reads cnt bytes of
input into the memory area indicated by buf. A successful read() updates the access time for the
file.
Syntax in C language
size_t read (int fd, void* buf, size_t cnt);
Parameters:
∙ fd: file descriptor
∙buf: buffer to read data from
∙cnt: length of buffer
Returns: How many bytes were actually read
∙ return Number of bytes read on success
∙ return 0 on reaching end of file
∙ return -1 on error
∙ return -1 on signal interrupt
Important points
∙buf needs to point to a valid memory location with length not smaller than the specified
size because of overflow.
∙ fd should be a valid file descriptor returned from open() to perform read operation
because if fd is NULL then read should generate error.
∙cnt is the requested number of bytes read, while the return value is the actual number of
bytes read. Also, some times read system call should read less bytes than cnt.
// C program to illustrate
// read system Call
#include<stdio.h>
#include <fcntl.h>
int main()
{
int fd, sz;
char *c = (char *) calloc(100, sizeof(char));
fd = open("foo.txt", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
Output:
called read(3, c, 10). returned that 10 bytes were read.
Those bytes are as follows: 0 0 0 foo.
Suppose that foobar.txt consists of the 6 ASCII characters “foobar”. Then what is the
output of the following program?
// C program to illustrate
// read system Call
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
char c;
int fd1 = open("sample.txt", O_RDONLY, 0);
int fd2 = open("sample.txt", O_RDONLY, 0);
read(fd1, &c, 1);
read(fd2, &c, 1);
printf("c = %c\n", c);
exit(0);
}
Output:
c=f
close(fd);
}
Output:
called write(3, "hello geeks\n", 12). it returned 11
Here, when you see in the file foo.txt after running the code, you get a “hello geeks“. If foo.txt file
already have some content in it then write system call overwrite the content and all previous
content are deleted and only “hello geeks” content will have in the file.
// C program to illustrate
// I/O system Calls
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
close(fd[0]);
close(fd[1]);
return 0;
}
Output:
hello world
In this code, buf1 array’s string “hello world” is first write in to stdin fd[0] then after that this
string write into stdin to buf2 array. After that write into buf2 array to the stdout and print output
“hello world“.