0% found this document useful (0 votes)
15 views51 pages

OS Unit 3 Complete

The document outlines key concepts in memory management, virtual memory, and file systems, detailing techniques such as swapping, paging, and segmentation. It explains the differences between logical and physical address spaces, memory allocation strategies, and fragmentation issues. Additionally, it covers the implementation of file systems, including file attributes, access methods, and directory structures.

Uploaded by

kptejasgowda
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)
15 views51 pages

OS Unit 3 Complete

The document outlines key concepts in memory management, virtual memory, and file systems, detailing techniques such as swapping, paging, and segmentation. It explains the differences between logical and physical address spaces, memory allocation strategies, and fragmentation issues. Additionally, it covers the implementation of file systems, including file attributes, access methods, and directory structures.

Uploaded by

kptejasgowda
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/ 51

Syllabus: Unit – 03

MEMORY MANAGEMENT
Logical and Physical Address space

Swapping

Contiguous Allocation

Paging

Segmentation

Segmentation with Paging

VIRTUAL MEMORY
Introduction to Virtual Memory

Demand Paging

Page Replacement

Page Replacement Algorithms

Allocation of frames

Thrashing

FILE SYSTEM
File Concepts – Attributes, Operations and Types of Files

File System

File Access Methods

Directory Structure

Protection

File System Implementation – File System Structure, Allocation Methods,


Free Space Management

1
DIAGRAM LIST
1. Swapping

2. Single partitioned allocation

Operating system
(OS)

User process

Wasted memory
space

3. Multi partitioned allocation

Mainmemory Mainmemory

Operating Operating
system system

Process 5
Process 1 Free
Process 2 Process 2
Process 3 Free
Process 4 Process 4
wasted Free

2
4. Paging table architecture

5. Virtual Memory

3
6. Demand paging

4
Chapter - 1

MEMORY MANAGEMENT
Memory is central to the operation of a modern computer system.
Memory is a large array of words or bytes, each with its own address. The
CPU fetches instructions from memory according to the value of the
program counter. These instructions may cause additional loading from
and storing to specific memory addresses.

A typical instruction execution cycle:

1. First fetch an instruction from memory.

2. The instruction is then decoded and may cause operands to be


fetched from memory.

3. After the instruction has been executed on the operands, results may
be stored back in memory.

Memory management is the functionality of an operating system


which handles or manages primary memory. Memory management
keeps track of each and every memory location either it is allocated to
some process or it is free. It checks how much memory is to be
allocated to processes. It decides which process will get memory at
what time. It tracks whenever some memory gets freed or unallocated
and correspondingly it updates the status.

Memory management provides protection by using two registers,


a base register and a limit register. The base register holds the
smallest legal physical memory address and the limit register specifies
the size of the range. For example, if the base register holds 300000
and the limit register is 120000, then the program can legally access
all addresses from 300000 through 411999.

5
Instructions and data to memory addresses can be done in following ways

 Compile time -- When it is known at compile time where the process


will reside, compile time binding is used to generate the absolute code.
 Load time -- When it is not known at compile time where the process
will reside in memory, then the compiler generates re-locatable code.
 Execution time -- If the process can be moved during its execution
from one memory segment to another, then binding must be delayed
until run time

Dynamic Loading

In dynamic loading, a routine of a program is not loaded until it is


called by the program. All routines are kept on disk in a re-locatable load
format. The main program is loaded into memory and is executed. Other
routines methods or modules are loaded on request. Dynamic loading makes
better memory space utilization and unused routines are never loaded.

Dynamic Linking

Linking is the process of collecting and combining various modules of


code and data into a executable file that can be loaded into memory and
executed. Operating system can link system level libraries to a program.
When it combines the libraries at load time, the linking is called static
linking and when this linking is done at the time of execution, it is called as
dynamic linking.

In static linking, libraries linked at compile time, so program code size


becomes bigger whereas in dynamic linking libraries linked at execution
time so program code size remains smaller.

Logical versus Physical Address Space

An address generated by the CPU is a logical address whereas address


actually available on memory unit is a physical address. Logical address
is also known as Virtual address.

Virtual and physical addresses are the same in compile-time and load-
time address-binding schemes. Virtual and physical addresses differ in
execution-time address-binding scheme.

The set of all logical addresses generated by a program is referred to


as a logical address space. The set of all physical addresses corresponding to
these logical addresses is referred to as a physical address space.
6
The run-time mapping from virtual to physical address is done by the
Memory Management Unit (MMU) which is a hardware device. MMU uses
following mechanism to convert virtual address to physical address.

 The value in the base register is added to every address generated by a


user process which is treated as offset at the time it is sent to
memory. For example, if the base register value is 10000, then an
attempt by the user to use address location 100 will be dynamically
reallocated to location 10100.
 The user program deals with virtual addresses; it never sees the real
physical addresses.

Swapping

Swapping is a mechanism in which a process can be swapped


temporarily out of main memory to a backing store and then brought back
into memory for continued execution.

Backing store is a usually a hard disk drive or any other secondary


storage which fast in access and large enough to accommodate copies of all
memory images for all users. It must be capable of providing direct access to
these memory images.

Major time consuming part of swapping is transfer time. Total transfer


time is directly proportional to the amount of memory swapped. Let us
assume that the user process is of size 100KB and the backing store is a
standard hard disk with transfer rate of 1 MB per second. The actual
transfer of the 100K process to or from memory will take
100KB / 1000KB per second
= 1/10 second
= 100 milliseconds

7
Contiguous memory allocation:

Contiguous memory allocation is a classical memory allocation model


that assigns a process consecutive memory blocks (that is, memory blocks
having consecutive addresses). Contiguous memory allocation is one of the
oldest memory allocation schemes. When a process needs to execute,
memory is requested by the process. The size of the process is compared
with the amount of contiguous main memory available to execute the
process. If sufficient contiguous memory is found, the process is allocated
memory to start its execution. Otherwise, it is added to a queue of waiting
processes until sufficient free contiguous memory is available.

Main memory usually has two partitions

 Low Memory -- Operating system resides in this memory.


 High Memory -- User processes then held in high memory.

Single contiguous allocation or Single – partition allocation

With this setup a simple and more staring forward memory


management scheme is to run merely one program at a time. Thus sharing
the complete main memory between this single program and the resident
OS. That is to run one program at a time (Mono-programming without
swapping or paging). This scheme is referred to as single contiguous
allocation or single – partition allocation. This allocation scheme is depicted
in the figure below:

8
Operating system 0 Wasted memory
(OS) space

User process User process

User programs
Wasted memory Operating System
space (OS)

640K

Main memory Main memory

When a computer system has this organization – Only one user


process at a time can be executing. The user process or program (job) has
complete control over the CPU until it is terminated or an error occurs. The
OS also called resident monitor (program) can reside in either upper or lower
part of the main memory.

Working:

Whenever a user types a command for his program execution, the OS


copies the requested program from hard disk to main memory as single user
process and executes the same. When the process terminates, the result will
be displayed together with a display prompt or command prompt (C:\> or
D:\>) character. Thus indicating that OS is waiting to accept a new
command at blinking cursor of command prompt. When OS receives a new
command, it loads again the program or user process concerned afresh into
main memory by overwriting the previous user process. Early computer
systems such as single-user systems like (PCs) running MS-DOS as their OS
used this allocation scheme.

The disadvantages of single contiguous allocation scheme include:

 Resources including memory and CPU were found in poor utilization


 Lack of hardware support to allow effective allocation of memory
 No provisions to execute multiple processes at the same time due to
single-partition allocation.

This type of single – contiguous allocation was used in small, inexpensive


single user computer system. Despite its disadvantages, the single-
contiguous allocation scheme is simple and easy to implement.

9
Multiple – partition allocation (partitioned memory management or
partitioned memory allocation scheme)

In single – partition allocation, only one user program (job) can be


executed at a time. To solve the problem of wasted memory and CPU idle
time-multiprogramming was introduced. With multiprogramming it is
possible to maintain two or more user processes concurrently executing or
atleast in the states of execution.

Working:

To implement multiprogramming Multiple-partition allocation scheme


was introduced. Accordingly, the main memory is divided into several fixed-
size partitions and each partition may be allocated to hold exactly one
process (user job) at any given instant of time. In otherwords, main memory
is partitioned in such a way that each user job or a disjoint process can be
allocated a separate contiguous section of main memory.

In this case number of partitions at any given time dictate the degree
of multiprogramming. Thus if one or more partitions are free, processes
will be selected from an input ready queue and are loaded in to vacant
partitions on one-process, per-partition basis. When a process terminates its
execution successfully, the corresponding partition becomes free for loading
another process by overwriting.

Main memory Main memory

Operating system Operating system

Process 5
Process 1 Free
Process 2 Process 2
Process 3 Free
Process 4 Process 4
Free
wasted

Strategies for memory allocation

 In multiple-partition allocation scheme, it is assumed in general that


there will be a set of vacant memory holes called partitions of various

10
sizes (big, small and moderate sizes) and are scattered across memory
at any given time.

 Once a process arrives with a memory request, then OS scans
through this set of memory holes, and it will attempt to find a hole
that is large enough (appropriate size) to accommodate the process.

 Suppose a hole is too much large in size, it can be halved or bisected
into two partitions – one hole of appropriate size is allocated for
loading the requested process (arrived process) and the remaining part
is returned to pool of unused memory holes.

 Similarly once a process terminates, the occupied memory block will
be released automatically and is then returned to pool of free (unused)
memory holes.

 Suppose when a memory block is released such that it is adjacent to
another free (vacant) hole, then they will be merged together to form a
higher size memory hole.

 This kind of memory allocation procedure reflect a particular instance
of the general dynamic storage allocation problem. The problem
highlights on how to fulfill a memory request of size ‘n’ from a given
list of free memory holes.

 For which the set of free holes can be searched to locate as to which
hole is the best one (or more appropriate one in size) to allocate as per
the popular strategies as the First-fit, best-fit and worst-fit.

 First Fit: The first hole that is big enough is allocated to program.
 Best Fit: The smallest hole that is big enough is allocated to program.
 Worst Fit: The largest hole that is big enough is allocated to program.

Operating system uses the following memory allocation mechanism.

Sl.no Memory allocation Description


01 Single -partition In this type of allocation, relocation-register
allocation scheme is used to protect user processes
from each other, and from changing
operating-system code and data. Relocation
register contains value of smallest physical
address whereas limit register contains range
of logical addresses. Each logical address
must be less than the limit register.
02 Multiple –partition In this type of allocation, main memory is
allocation divided into a number of fixed-sized

11
partitions where each partition should
contain only one process. When a partition is
free, a process is selected from the input
queue and is loaded into the free partition.
When the process terminates, the partition
becomes available for another process

Fragmentation:

As processes are loaded and removed from memory, the free memory
space is broken into little pieces. It happens after sometimes that processes
cannot be allocated to memory blocks considering their small size and
memory blocks remains unused. This problem is known as Fragmentation.
Fragmentation is of two types

 External fragmentation: Total memory space is enough to satisfy a


request or to reside a process in it, but it is not contiguous so it
cannot be used.
 Internal fragmentation: Memory block assigned to process is bigger.
Some portion of memory is left unused as it cannot be used by
another process.

External fragmentation can be reduced by compaction or shuffle memory


contents to place all free memory together in one large block. To make
compaction feasible, relocation should be dynamic.

Paging:

External fragmentation is avoided by using paging technique. Paging


is a technique in which physical memory is broken into blocks of the same
size called pages (size is power of 2, between 512 bytes and 8192 bytes).
When a process is to be executed, it's corresponding pages are loaded into
any available memory frames.

Logical address space of a process can be non-contiguous and a process is


allocated physical memory whenever the free memory frame is available.
Operating system keeps track of all free frames. Operating system needs n
free frames to run a program of size n pages.

Address generated by CPU is divided into

 Page number (p) -- page number is used as an index into a page table
which contains base address of each page in physical memory.

12
 Page offset (d) -- page offset is combined with base address to define
the physical memory address.

Figure show the paging table architecture

Segmentation

Segmentation is a technique to break memory into logical pieces


where each piece represents a group of related information. For example,
data segments or code segment for each process, data segment for operating
system and so on. Segmentation can be implemented using or without using
paging.

Unlike paging, segments are having varying sizes and thus eliminates
internal fragmentation. External fragmentation still exists but to lesser
extent.

Address generated by CPU is divided into

 Segment number (s) -- segment number is used as an index into a


segment table which contains base address of each segment in
physical memory and a limit of segment.

13
 Segment offset (o) -- segment offset is first checked against limit and
then is combined with base address to define the physical memory
address.

Segmentation with Paging

Both paging and segmentation have their advantages and


disadvantages, it is better to combine these two schemes to improve on
each. The combined scheme is known as 'Page the Elements'. Each segment
in this scheme is divided into pages and each segment is maintained in a
page table. The logical address space is divided into 2 partitions: first
partition consists of up to 8KB segment private to that process (Local
Descriptor table), second partition consists of 8kb segment shared among all
the processes (Global Descriptor table).

The logical address is divided in to selector and offset

The selector is of 16 bit number,

g p
s
Segment number Deals with protection
Indicate GDT\LDT

The offset is of 32 bit number specifying the location of


the byte (word) within the segment. The base and the limit information
about the segment are used to generate linear address. The linear address is
divided into page number and a page offset, further page number is divided
in to page directory pointer and page table pointer.

14
****************************** End of Chapter 1 ******************************

15
Chapter - 2

Virtual memory
Memory is hardware that your computer uses to load the operating
system and run programs. It consists of one or more RAM chips that each
have several memory modules. The amount of real memory in a computer is
limited to the amount of RAM installed. Common memory sizes are 256MB,
512MB, and 1GB.

Because your computer has a finite amount of RAM, it is possible to


run out of memory when too many programs are running at one time. This
is where virtual memory comes in. Virtual memory increases the available
memory of your computer by enlarging the "address space," or places in
memory where data can be stored. It does this by using hard disk space for
additional memory allocation. However, since the hard drive is much slower
than the RAM, data stored in virtual memory must be mapped back to real
memory in order to be used.

The process of mapping data back and forth between the hard drive
and the RAM takes longer than accessing it directly from the memory. This
means that the more virtual memory is used, the more it will slow your
computer down.

Advantage: The primary advantage or objective of Virtual Memory systems


is the ability to load and execute a process that requires a larger amount of
memory than what is available by loading the process in parts and then
executing them.

The disadvantage is that Virtual Memory systems tend to be slow and


require additional support from the system's hardware for address
translations. It can be said that the execution speed of a process in a
Virtual Memory system can equal, but never exceed, the execution speed of
the same process with Virtual Memory turned off. Hence, we do not have an
advantage with respect to the execution speed of the process. The
advantage lies in the ability of the system to eliminate external
fragmentation.

The other disadvantage of Virtual Memory systems is the possibility


of Thrashing due to excessive Paging and Page faults. It may be noted that
Trash Point is a point after which the execution of a process comes to a halt;
the system is busier paging pages in and out of the memory than executing
them.

16
Following are the situations, when entire program is not required to be
loaded fully in main memory.

 User written error handling routines are used only when an error
occurred in the data or computation.

 Certain options and features of a program may be used rarely.

 Many tables are assigned a fixed amount of address space even
though only a small amount of the table is actually used.

 The ability to execute a program that is only partially in memory
would counter many benefits.

 Less number of I/O would be needed to load or swap each user
program into memory.

 A program would no longer be constrained by the amount of physical
memory that is available.

 Each user program could take less physical memory, more programs
could be run the same time, with a corresponding increase in CPU
utilization and throughput.

Virtual memory is commonly implemented by demand paging. It can also be


implemented in a segmentation system. Demand segmentation can also be
used to provide virtual memory.

Demand Paging

A demand paging system is quite similar to a paging system with


swapping. When we want to execute a process, we swap it into memory.
Rather than swapping the entire process into memory, however, we use a
lazy swapper called pager.
17
When a process is to be swapped in, the pager guesses which pages
will be used before the process is swapped out again. Instead of swapping in
a whole process, the pager brings only those necessary pages into memory.
Thus, it avoids reading into memory pages that will not be used in anyway,
decreasing the swap time and the amount of physical memory needed.

Hardware support is required to distinguish between those pages that


are in memory and those pages that are on the disk using the valid-invalid
bit scheme. Where valid and invalid pages can be checked by checking the
bit. Marking a page will have no effect if the process never attempts to
access the page. While the process executes and accesses pages that are
memory resident, execution proceeds normally.

Access to a page marked invalid causes a page-fault trap. This trap is the
result of the operating system's failure to bring the desired page into
memory. But page fault can be handled as following

18
Step Description

1 Check an internal table for this process, to determine whether the


reference was a valid or it was an invalid memory access

2 If the reference was invalid, terminate the process. If it was valid, but
page have not yet brought in, page in the latter.

3 Find a free frame

4 Schedule a disk operation to read the desired page into the newly
allocated frame

5 When the disk read is complete, modify the internal table kept with
the process and the page table to indicate that the page is now in
memory

6 Restart the instruction that was interrupted by the illegal address


trap. The process can now access the page as though it had always
been in memory. Therefore, the operating system reads the desired
page into memory and restarts the process as though the page had
always been in memory

19
Advantages

Following are the advantages of Demand Paging

 Large virtual memory.


 More efficient use of memory.
 Unconstrained multiprogramming. There is no limit on degree of
multiprogramming.

Disadvantages

Following are the disadvantages of Demand Paging

 Number of tables and amount of processor overhead for handling page


interrupts are greater than in the case of the simple paged
management techniques.
 Due to the lack of explicit constraints on jobs address space size.

PAGE REPLACEMENT

As we know that demand paging offers extremely large virtual memory


to programmers without constraining the actual size of physical main
memory.

However, if all page frames are full for a given fixed size main memory
and a page fault occurs demanding a page to be brought into main memory
then page replacement policy or algorithm has to determine as to which
page (page frame) from physical main memory must be removed (or swapped
–out to hard disk). Therefore, page replacement is fundamental to demand
paging. Although we have a wide variety of page-replacement algorithms, the
one with good paging performance and that yields the lowest page –fault
rate will be preferred.

The various page-replacement algorithms can be listed out as follows:

1) First In First Out (FIFO) page replacement algorithm

2) Optimal Page replacement algorithm

3) Least Recently Used (LRU) page replacement algorithm

20
1. First In First Out page replacement algorithm

FIFO page replacement algorithm replaces the page that has


been resident in main memory for the longest time. When a
page is to be replaced, the oldest page (the one arrived at the
earliest) is swapped out. For which OS maintains a separate
queue of pages that are all resident in physical main memory
frames.

The oldest page is at the head of queue (First –in) and most
recently arrived page is at the tail of the queue.

On a page fault, the page at the head of queue will be


removed (First –out) and new page (that has been swapped-in to
main memory frame) will be appended to the tail of queue.

Drawback:

 FIFO algorithm emphasizes on the length of time a page has been


resident in physical memory rather than how much the page is being
accessed and its contents are used.

 It is simple to implement but its functional behavior cannot be


incorporated and applicable to most of the programs, since it is
completely independent of the locality.

 If an active page is being replaced based on FIFO, to swap-in a new


page, then page fault may occur almost immediately to get back that
swapped-out active page from backing store.

 Thus FIFO kind of page replacement will increase the page-fault rate
and slow-down process execution.

2. Optimal page replacement algorithm

It suggest that, on a page fault, replace the page which will


not be used or referenced in future for the longest period of
time. That is those pages getting frequent references need not
be replaced. Some how one should know about a page reference
which will not be referenced say until 10,100 or perhaps 1000
instructions later. Therefore an optimal page must be chosen to
be swapped-out from main memory.

21
Limitations of this algorithm:

 This algorithm is not generally realizable since when a page fault


occurs, the OS has no way of knowing in advance, as to when each
page will be referenced next?. Therefore it is difficult to implement the
optimal page replacement algorithm

Benefits of this algorithm:

 This algorithm has lowest page-fault rate of all algorithms

 This algorithm can be used for simulating and evaluating other new
algorithms for their comparative studies.

3. Least – Recently – Used (LRU) Page Replacement algorithm

It suggest that, on a page fault, replace a page that has not


been referenced / used for a quite longer period of time.
That is remove least recently used page.

Working:

LRU replacement algorithm tie up a (time of use filed) timing label


with each page (that resident in main memory frame) indicating the time
of its last use. As and when a page is to be replaced, the LR algorithm
selects a page that has not been referenced for the longest length of time.
Thus functional behavior of LRU algorithm is considered as optimal page-
replacement algorithm but looking backward in time rather than forward.

LRU replacement scheme require considerable hardware assistance


for its implementation.

Advantages:

 LRU algorithm is easy to understand, moderately efficient to


implement and much better than FIFO algorithm.

Limitations:

 LRU algorithm is quite expensive and requires substantial


hardware support for its effective implementation.

A linked list of all pages in main memory is to be maintained and linked


list operation is time consuming.

22
Allocation of Frames:

The main memory of the operating system is divided into various


frames. The process is stored in these frames, and once the process is saved
as a frame, the CPU may run it. As a result, the operating system must set
aside enough frames for each process. As a result, the operating system
uses various algorithms in order to assign the frame.

Demand paging is used to implement virtual memory, an essential


operating system feature. It requires the development of a page replacement
mechanism and a frame allocation system. If you have multiple processes,
the frame allocation techniques are utilized to define how many frames to
allot to each one. A number of factors constrain the strategies for allocating
frames:

1. You cannot assign more frames than the total number of frames
available.
2. A specific number of frames should be assigned to each process. This
limitation is due to two factors. The first is that when the number of
frames assigned drops, the page fault ratio grows, decreasing the
process's execution performance. Second, there should be sufficient
frames to hold all the multiple pages that any instruction may
reference.

There are mainly five ways of frame allocation algorithms in the OS. These
are as follows:

1. Equal Frame Allocation


2. Proportional Frame Allocation
3. Priority Frame Allocation
4. Global Replacement Allocation
5. Local Replacement Allocation

Equal Frame Allocation:

In equal frame allocation, the processes are assigned equally among


the processes in the OS. For example, if the system has 30 frames and 7
processes, each process will get 4 frames. The 2 frames that are not

23
assigned to any system process may be used as a free-frame buffer pool in
the system.

Disadvantage

In a system with processes of varying sizes, assigning equal frames to


each process makes little sense. Many allotted empty frames will be wasted
if many frames are assigned to a small task.

Proportional Frame Allocation

The proportional frame allocation technique assigns frames based on


the size needed for execution and the total number of frames in memory.

The allocated frames for a process pi of size si are ai = (si/S)*m, in


which S represents the total of all process sizes, and m represents the
number of frames in the system.

Disadvantage

The only drawback of this algorithm is that it doesn't allocate frames


based on priority. Priority frame allocation solves this problem.

Priority Frame Allocation

Priority frame allocation assigns frames based on the number of frame


allocations and the processes. Suppose a process has a high priority and
requires more frames that many frames will be allocated to it. Following
that, lesser priority processes are allocated.

Global Replacement Allocation:

When a process requires a page that isn't currently in memory, it may


put it in and select a frame from the all frames sets, even if another process
is already utilizing that frame. In other words, one process may take a frame
from another.

Advantages

Process performance is not hampered, resulting in higher system


throughput.

24
Disadvantages

The process itself may not solely control the page fault ratio of a
process. The paging behavior of other processes also influences the number
of pages in memory for a process.

Local Replacement Allocation:

When a process requires a page that isn't already in memory, it can


bring it in and assign it a frame from its set of allocated frames.

Advantages

The paging behavior of a specific process has an effect on the pages in


memory and the page fault ratio.

Disadvantages

A low priority process may obstruct a high priority process by refusing


to share its frames.

Global Vs. Local Replacement Allocation:

The number of frames assigned to a process does not change using a


local replacement strategy. On the other hand, using global replacement, a
process can choose only frames granted to other processes and enhance the
number of frames allocated.

Thrashing:

Thrashing is a phenomenon that occurs in computer OS when the


system spends an excessive amount of time swapping data between Physical
memory (RAM) and Virtual memory (disk storage) due to high memory
demand and low available resources.

Thrash is the poor performance of a virtual memory (or paging)


system when the same pages are being loaded repeatedly due to a lack of
main memory to keep them in memory. Depending on the configuration and
algorithm, the actual throughput of a system can degrade by multiple orders
of magnitude.
25
Thrashing occurs when a computer's virtual memory resources are
overused, leading to a constant state of paging and page faults, inhibiting
most application-level processing. It causes the performance of the
computer to degrade or collapse. The situation can continue indefinitely
until the user closes some running applications or the active processes free
up additional virtual memory resources.

o Page fault: We know every program is divided into some pages. A page
fault occurs when a program attempts to access data or code in its
address space but is not currently located in the system RAM.
o Swapping: Whenever a page fault happens, the operating system will
try to fetch that page from secondary memory and try to swap it with
one of the pages in RAM. This process is called swapping.

Thrashing is when the page fault and swapping happens very frequently
at a higher rate, and then the operating system has to spend more time
swapping these pages. This state in the operating system is known as
thrashing. Because of thrashing, the CPU utilization is going to be reduced
or negligible.

The basic concept involved is that if a process is allocated too few


frames, then there will be too many and too frequent page faults. As a
26
result, no valuable work would be done by the CPU, and the CPU utilization
would fall drastically.

The long-term scheduler would then try to improve the CPU utilization
by loading some more processes into the memory, thereby increasing the
degree of multiprogramming. Unfortunately, this would result in a further
decrease in the CPU utilization, triggering a chained reaction of higher page
faults followed by an increase in the degree of multiprogramming, called
thrashing.

Algorithms during Thrashing:

Whenever thrashing starts, the operating system tries to apply either


the Global page replacement Algorithm or the Local page replacement
algorithm.

1. Global Page Replacement

Since global page replacement can bring any page, it tries to bring
more pages whenever thrashing is found. But what actually will happen is
that no process gets enough frames, and as a result, the thrashing will
increase more and more. Therefore, the global page replacement algorithm is
not suitable when thrashing happens.

2. Local Page Replacement

Unlike the global page replacement algorithm, local page replacement


will select pages which only belong to that process. So there is a chance to
reduce the thrashing. But it is proven that there are many disadvantages if
we use local page replacement. Therefore, local page replacement is just an
alternative to global page replacement in a thrashing scenario.

***************************** End of Chapter 2 *******************************

27
Chapter – 3

File System

The file system consists of two distinct parts: a collection of files, each
storing related data and a directory structure, which organizes and provides
information about all the files in the system.

File Concept:
Computers can store information on various storage media, such as
magnetic disks, magnetic tapes and optical disks. So that the computer
system will be convenient to use, the OS provides a uniform logical view of
stored information. The OS abstracts from the physical properties of its
storage devices to define a logical storage unit, the file. Files are mapped by
the OS onto physical devices. These storage devices are usually non-
volatile, so the contents are persistent between system reboots.

Definition: A file is a named collection of related information that is


recorded on secondary storage. From a user’s perspective, a file is the
smallest allotment of logical secondary storage. Files may be free form, such
as text files, or may be formatted rigidly. In general, a file is a sequence of
bits, bytes, lines or records, the meaning of which is defined by the files
creator or user. The concept of a file is thus extremely general.

Many different types of information may be stored in a file – source or


executable programs, numeric or text data, photos, music, video and so on.
A file has a certain defined structure, which depends on its type. A text file
is a sequence of characters organized into lines (and possibly pages). A
source file is a sequence of functions, each of which is further organized as
declarations followed by executable statements. An executable file is a
series of code sections that the loader can bring into memory and execute.

28
File Attributes
A file is named, for the convenience of its human users, and is
referred to by its name. a name is usually a string of characters such as
example.c. Some systems differentiate between uppercase and lowercase
characters in names, whereas other systems do not. When a file is named, it
becomes independent of the process, the user and even the system that
created it.

A file’s attributes vary from one OS to another but typically consists 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 is the 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.

The information about all files is kept in the directory structure,


which also resides on secondary storage. Typically a directory entry
consists of the files name and its unique identifier. The identifier in turn
locates the other file attributes. It may take more than a KB to record this
information for each file. In a system with many files, the size of the
directory itself may be MB. Because directories, like files, must be non-
volatile, they must be stored on the device and brought into memory as
needed.

29
File Operations
A file is an abstract data type. To define a file properly, we need to
consider the operations that can be performed on files. The OS can provide
system calls to create, write, read, reposition, delete and truncate files.

Creating a file: 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.

Writing a file: 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 files 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.

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

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/O. This file
operation is also known as a file seek.

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.

Truncating a file: 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 the file length – but lets the file be reset to length zero and its file space
released.

These six basic operations comprise the minimal set of required file
operations. Other common operations include appending new information
to the end of an existing file and renaming an existing file.
30
Types of Files
A common technique for implementing file types is to 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. In this way, the user and the OS
can tell from the name alone what the type of a file ie., Most OS allow users
to specify a file name as a sequence of characters followed by a period and
terminated by an extension made up of additional characters. Eg:
mysore.doc, factorial.c, fibo.java etc.,

File type Usual extension Function

Executable exe, com, bin or more Ready to run machine


language program

Object obj, o Compiled, machine


language, not linked

Source code c, cc, java, pert, asm Source code in various


languages

Batch bat, sh Commands to the command


interpreter

Markup xml, html, tex Textual data, documents

Word processor xml, rtf, doc Various word processor


formats

Library lib, a, so, dll Libraries of routines for


programmers

Print or view gif, pdf, jpg ASCII or binary file in a


format for printing or
viewing

Archive rar, zip, tar Related files grouped into


one file, sometimes
compressed for archiving or
storage

Multimedia mpeg, mov, mp3, mp4, avi Binary file containing audio
or A/V information

31
File System
An integral part of an OS is what is called a File system. A file system
is a data structure that stores data and information on storage devices (hard
drives, floppy disk etc) making them easily retrievable. Different OS’s use
different file systems, but all have similar features.

A file system is a method an OS uses to store, organize and manage


files and directories on a storage device. Some common types of file systems
include:

1. FAT (File Allocation Table): An older file system used by older versions
of Windows and other OS

2. NTFS (New Technology File System): A modern file system used by


Windows. It supports features such as file and folder permissions,
compression and encryption.

3. ext (Extended File System): A file system commonly used on Linux


and Unix based OS

4. HFS (Hierarchical File System): A file system used by macOS.

5. APFS (Apple File System): A new file system introduced by Apple for
their Macs and OS devices.

Advantages of using File System:

a) Organization – A file system allows files to be organized into


directories and sub-directories, making it easier to manage and
locate files.

b) Data protection – File systems often include features such as


file and folder permissions, backup and restore and error
detection and correction to protect data from loss or corruption.

c) Improved performance – A well designed file system can


improve the performance of reading and writing data by
organizing it efficiently on disk.

32
Disadvantages of using a file system:

a) Compatibility issue – Different file systems may not be compatible


with each other, making it difficult to transfer data between different
OS

b) Disk space overhead – File systems may use some disk space to store
metadata and other overhead information, reducing the amount of
space available for user data.

c) Vulnerability – File systems can be vulnerable to data corruption,


malware and other security threats, which can compromise the
stability and security of the system.

File Access Methods:


Files store information, when it is used, this information must be
accessed and read into computer memory. The information in the file can be
accessed in several ways.

d) Sequential Access:

The simplest access method is sequential access.


Information in the file is processed in order, one record
after the other. This mode of access is by far the most
common. Eg: editors and compilers usually access files in
this fashion.

e) Direct Access:

Another method is direct access (or relative access).


Here, a file is made up of fixed length logical records that
allow programs to read and write records rapidly in no
particular order. The direct access method is based on a
disk model of a file, since disks allow random access to
any file block. For direct access the file is viewed as a
numbered sequence of blocks or records. Thus, we may
read block 14, then read block 53 and then write block 7.
There are no restrictions on the order of reading or writing
for a direct-access file.

33
Direct access files are of great use for immediate access to
large amount of information. Databases are often of this
type. When a query concerning a particular subject
arrives, we compute which block contains the answer and
then read that block directly to provide the desired
information.

Not all OS support both sequential and direct access for


files. Some systems allow only sequential file access,
others allow only direct access. Some systems require that
a file be defined as sequential or direct when it is created.

Other Access Methods:

Other access methods can be built on top of a direct access method.


These methods generally involve the constructive of an index for the file. The
index, like an index in the back 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.

With large files, the index file itself may become too large to be kept in
memory. The solution is to create an index for the index file. The primary
index file contains pointers to secondary index files, which point to the
actual data items.

Directory Structure
The directory can be viewed as a symbol table that translates file
names into their directory entries. If we take such a view, we see that the
directory itself can be organized in many ways. The organization must allow
us to insert entries, to delete entries, to search for a named entry and to
list all the entries in the directory.

Operations that are to be performed on a directory:

1. Search for a file: We need to be able to search a directory structure to


find the entry for a particular file. Since files have symbolic names
and similar names may indicate a relationship among files, we may
want to be able to find all files whose names match a particular
pattern.

2. Create a file: New files need to be created and added to the directory

34
3. Delete a file: When a file is no longer needed, we want to be able to
remove it from the directory.

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

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

6. 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 file 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.

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.

A single level directory has significant limitations, however, when the


number of files increases or when the system has more than one user. Since
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 file names of all the files as the number of files increases.

cat bo a test data mail cont hex records

In the above diagram rectangle specifies directory, circle specifies a file

35
Two – Level directory:

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.

In the two-level directory structure, each user has his own User File
Directory (UFD). The UFD’s have similar structure but each lists only the
files of a single user. When a user job starts or a user logs in, the systems
Master File Directory (MFD) is searched. The MFD is indexed by user name
on 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.
Thus, different users may have files with the same name, as long as all the
file names within each UFD are unique. To create a file for a user, the OS
searches only that users UFD to ascertain whether another file of that name
exists. To delete a file, the OS confines its search to the local UFD, thus it
cannot accidentally delete another users file that has the same name.

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.

Thus, a user name and a file name define a path name. Every file in
the system has a path name. to name, a file uniquely a user must know the
path name of the file desired.

MFD

User1 User2 User3 User4

UFD

cat bo a test data mail cont hex records

36
Other directories include: Tree-structured directories, Acyclic – Graph
directories, General Graph directory etc.,

Protection
When information is stored in a computer system, we want to keep it
safe from physical damage (the issue of reliability) and improper access.

Reliability is generally provided by duplicate copies of files. Many


computers have systems programs that automatically (or through computer
operator intervention) copy disk files to tape at regular intervals (once per
day or week or month) to maintain a copy should a file system be
accidentally destroyed. File systems can be damaged by hardware problems
(such as errors in reading or writing), power surges or failures, head
crashes, dirt, temperature extremes, and vandalism. Files may be deleted
accidentally. Bugs in the file system software can also cause file contents to
be lost.

Types of Access:

Protection mechanisms provide controlled access by limiting the types


of file access that can be made. Access is permitted or denied depending on
several factors, one of which is the type of access requested. Several
different types of operations may be controlled:

 Read: Read from the file

 Write: Write or rewrite the file

 Execute: Load the file into memory and execute it

 Append: Write new information at the end of the file

 Delete: Delete the file and free its space for possible reuse

 List: List the name and attributes of the file

Other operations such as renaming, copying and editing the file, may also
be controlled.

37
File System Implementation:
Disks provide most of the secondary storage on which file systems are
maintained. Two characteristics make them convenient for this purpose:

1. A disk can be rewritten in place: It is possible to read a block from


the disk, modify the block, and write it back into the same place.

2. A disk can access directly any block of information it contains.


Thus, it is simple to access any file either sequentially or randomly
and switching from one file to another requires only moving the read –
write heads and waiting for the disk to rotate.

To improve I/O efficiency, I/O transfers between memory and disk are
performed in units of blocks. Each block has one or more sectors.
Depending on the disk drive, sector size varies from 32 bytes to 4,096
bytes; the usual size is 512 bytes.

File systems provide efficient and convenient access to the disk by


allowing data to be stored, located and retrieved easily.

A file is a collection of related information. The file system resides on


secondary storage and provides efficient and convenient access to the disk
by allowing data to be stored, located, and retrieved.
File system implementation in an operating system refers to how the file
system manages the storage and retrieval of data on a physical storage
device such as a hard drive, solid-state drive, or flash drive. The file system
implementation includes several components, including:
1. File System Structure: The file system structure refers to how the
files and directories are organized and stored on the physical storage
device. This includes the layout of file system data structures such as
the directory structure, file allocation table, and inodes.

2. File Allocation: The file allocation mechanism determines how files


are allocated on the storage device. This can include allocation
techniques such as contiguous allocation, linked allocation, indexed
allocation, or a combination of these techniques.

38
3. Data Retrieval: The file system implementation determines how the
data is read from and written to the physical storage device. This
includes strategies such as buffering and caching to optimize file I/O
performance.

4. Security and Permissions: The file system implementation includes


features for managing file security and permissions. This includes
access control lists (ACLs), file permissions, and ownership
management.
5. Recovery and Fault Tolerance: The file system implementation
includes features for recovering from system failures and maintaining
data integrity. This includes techniques such as journaling and file
system snapshots.

File system implementation is a critical aspect of an operating system as


it directly impacts the performance, reliability, and security of the system.
Different operating systems use different file system implementations
based on the specific needs of the system and the intended use cases.
Some common file systems used in operating systems include NTFS and
FAT in Windows, and ext4 and XFS in Linux.
File system is organized into many layers:

Application programs

Logical file system

File – organization module

Basic file system

I/O control

Devices

Layered file system

39
 I/O Control level – Device drivers acts as interface between devices
and OS, they help to transfer data between disk and main memory. It
takes block number as input and as output it gives low level hardware
specific instruction.

 Basic file system – It Issues general commands to device driver to


read and write physical blocks on disk. It manages the memory buffers
and caches. A block in buffer can hold the contents of the disk block
and cache stores frequently used file system metadata.

 File organization Module – It has information about files, location


of files and their logical and physical blocks. Physical blocks do not
match with logical numbers of logical block numbered from 0 to N. It
also has a free space which tracks unallocated blocks.

 Logical file system – It manages metadata information about a file


ie., includes all details about a file except the actual contents of file. It
also maintains via file control blocks. File control block (FCB) has
information about a file – owner, size, permissions, location of file
contents.

Advantages:

1. Duplication of code is minimized.

2. Each file system can have its own logical file system.

3. File system implementation in an operating system provides several


advantages, including:

4. Efficient Data Storage: File system implementation ensures efficient


data storage on a physical storage device. It provides a structured way
of organizing files and directories, which makes it easy to find and
access files.

5. Data Security: File system implementation includes features for


managing file security and permissions. This ensures that sensitive data
is protected from unauthorized access.

6. Data Recovery: The file system implementation includes features for


recovering from system failures and maintaining data integrity. This
helps to prevent data loss and ensures that data can be recovered in the
event of a system failure.

40
7. Improved Performance: File system implementation includes
techniques such as buffering and caching to optimize file I/O
performance. This results in faster access to data and improved overall
system performance.

8. Scalability: File system implementation can be designed to be


scalable, making it possible to store and retrieve large amounts of data
efficiently.

9. Flexibility: Different file system implementations can be designed to


meet specific needs and use cases. This allows developers to choose the
best file system implementation for their specific requirements.

10. Cross-Platform Compatibility: Many file system implementations


are cross-platform compatible, which means they can be used on
different operating systems. This makes it easy to transfer files between
different systems.

In summary, file system implementation in an operating system


provides several advantages, including efficient data storage, data security,
data recovery, improved performance, scalability, flexibility, and cross-
platform compatibility. These advantages make file system implementation
a critical aspect of any operating system.

Disadvantages:

If we access many files at same time then it results in low performance. We


can implement file system by using two types data structures:

1. Boot Control Block – It is usually the first block of volume and it


contains information needed to boot an operating system. In UNIX it is
called boot block and in NTFS it is called as partition boot sector.

2. Volume Control Block – It has information about a particular


partition ex:- free block count, block size and block pointers etc. In
UNIX it is called super block and in NTFS it is stored in master file
table.

3. Directory Structure – They store file names and associated inode


numbers. In UNIX, includes file names and associated file names and in
NTFS, it is stored in master file table.

4. Per-File FCB – It contains details about files and it has a unique


identifier number to allow association with directory entry. In NTFS it is
stored in master file table.
41
5. Mount Table – It contains information about each mounted volume.

6. Directory-Structure cache – This cache holds the directory


information of recently accessed directories.

7. System wide open-file table – It contains the copy of FCB of each


open file.

8. Per-process open-file table – It contains information opened by that


particular process and it maps with appropriate system wide open-file.

9. Linear List – It maintains a linear list of filenames with pointers to


the data blocks. It is time-consuming also. To create a new file, we must
first search the directory to be sure that no existing file has the same
name then we add a file at end of the directory. To delete a file, we
search the directory for the named file and release the space. To reuse
the directory entry either we can mark the entry as unused or we can
attach it to a list of free directories.

10. Hash Table – The hash table takes a value computed from the file
name and returns a pointer to the file. It decreases the directory search
time. The insertion and deletion process of files is easy. The major
difficulty is hash tables are its generally fixed size and hash tables are
dependent on hash function on that size.

Key steps involved in file system implementation:

File system implementation is a crucial component of an operating system,


as it provides an interface between the user and the physical storage
device. Here are the key steps involved in file system implementation:
1. Partitioning the storage device: The first step in file system
implementation is to partition the physical storage device into one or
more logical partitions. Each partition is formatted with a specific file
system that defines the way files and directories are organized and
stored.

2. File system structures: File system structures are the data


structures used by the operating system to manage files and directories.
Some of the key file system structures include the superblock, inode
table, directory structure, and file allocation table.

3. Allocation of storage space: The file system must allocate storage


space for each file and directory on the storage device. There are several

42
methods for allocating storage space, including contiguous, linked, and
indexed allocation.

4. File operations: The file system provides a set of operations that can
be performed on files and directories, including create, delete, read,
write, open, close, and seek. These operations are implemented using
the file system structures and the storage allocation methods.

5. File system security: The file system must provide security


mechanisms to protect files and directories from unauthorized access or
modification. This can be done by setting file permissions, access
control lists, or encryption.

6. File system maintenance: The file system must be maintained to


ensure efficient and reliable operation. This includes tasks such as disk
defragmentation, disk checking, and backup and recovery.

Overall, file system implementation is a complex and critical component


of an operating system. The efficiency and reliability of the file system have
a significant impact on the performance and stability of the entire system.

Allocation Methods
In almost every case, many files are stored on the same disk. The
main problem is how to allocate space to these files so that disk space
is utilized effectively and files can be accessed quickly. Three major
methods of allocating disk space are in wide use.

1. Contiguous allocation

2. Linked allocation

3. Indexed allocation

Each method has advantages and disadvantages, although some systems


support all the above methods. It is more common for a system to use one
method for all files.

1. Contiguous Allocation

It requires that each file occupy a set of contiguous blocks


on the disk. Thus, the number of disk seeks required for
accessing contiguously allocated files is minimal. Contiguous
43
allocation of a file is defined by the disk address and length of
the first block. Accessing a file that has been allocated
contiguously is easy. For direct access to block i of a file that
starts at block b, we can immediately access block b + i. thus
both sequential and direct access can be supported by
contiguous allocation.

One difficulty is finding space for a new file. These


algorithms suffer from the problem of external fragmentation.
As files are allocated and deleted, the free disk space is broken
into little pieces. External fragmentation exists whenever free
space is broken into chunks. Problem with contiguous
allocation is determining how much space is needed for a file.

2. Linked Allocation:

It 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 block of the file. Each
block contains a pointer to the next block. These pointers are
not made available to the user. Thus, if each block is 512 bytes
in size, and a disk address (the pointer) requires 4 bytes, then
the user sees blocks of 508 bytes.

44
There is no external fragmentation with linked allocation.
A file can continue to grow as long as free blocks are available.
Consequently, it is never necessary to compact disk space.

Linked allocation does have disadvantages, however, the


major problem is that it can be used effectively only for
sequential access files. To find the ith block of a file, we must
start at the beginning of that file and follow the pointers until
we get to the ith block. Each access to a pointer requires a disk
read and some require a disk seek. Consequently it is
inefficient to support direct – access capability fir linked
allocation files.

Another disadvantage is the space required for the


pointer. The usual solution to this problem is to collect blocks
into multiples, called clusters and to allocate clusters rather
than blocks.

Yet another problem of linked allocation is reliability.


Recall that the files are linked together by pointers scattered all
over the disk, and consider what would happen if a pointer were
lost or damaged. A bug in the OS software or a disk hardware
failure might result in picking up the wrong pointer. This error
could in turn result in linking into the free space list or into
another file.

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 OS. A section of
disk at the beginning of each volume is set aside to contain the
table. The table has one 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. A benefit is that random-access
time is improved.

45
3. Indexed Allocation:

Linked allocation solves the external fragmentation and


size declaration problem 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 address. The ith entry in the index block points to
the ith block of the file. Indexed allocation supports direct
access, without suffering from external fragmentation, because
any free block on the disk can satisfy a request for more space.
Indexed allocation does suffer from wasted space. However, the
pointer overhead of the index block is generally greater than the
pointer overhead of the linked allocation.

46
Free Space Management
Free space management is a critical aspect of operating systems as it
involves managing the available storage space on the hard disk or other
secondary storage devices. The operating system uses various techniques
to manage free space and optimize the use of storage devices. Here are
some of the commonly used free space management techniques:
1. Linked Allocation: In this technique, each file is represented by a
linked list of disk blocks. When a file is created, the operating system
finds enough free space on the disk and links the blocks of the file to
form a chain. This method is simple to implement but can lead to
fragmentation and wastage of space.

2. Contiguous Allocation: In this technique, each file is stored as a


contiguous block of disk space. When a file is created, the operating
system finds a contiguous block of free space and assigns it to the file.
This method is efficient as it minimizes fragmentation but suffers from
the problem of external fragmentation.

47
3. Indexed Allocation: In this technique, a separate index block is
used to store the addresses of all the disk blocks that make up a file.
When a file is created, the operating system creates an index block and
stores the addresses of all the blocks in the file. This method is efficient
in terms of storage space and minimizes fragmentation.

4. File Allocation Table (FAT): In this technique, the operating system


uses a file allocation table to keep track of the location of each file on
the disk. When a file is created, the operating system updates the file
allocation table with the address of the disk blocks that make up the
file. This method is widely used in Microsoft Windows operating
systems.

5. Volume Shadow Copy: This is a technology used in Microsoft


Windows operating systems to create backup copies of files or entire
volumes. When a file is modified, the operating system creates a shadow
copy of the file and stores it in a separate location. This method is
useful for data recovery and protection against accidental file deletion.

Overall, free space management is a crucial function of operating


systems, as it ensures that storage devices are utilized efficiently and
effectively.
The system keeps tracks of the free disk blocks for allocating space to
files when they are created. Also, to reuse the space released from deleting
the files, free space management becomes crucial. The system maintains a
free space list which keeps track of the disk blocks that are not allocated to
some file or directory. The free space list can be implemented mainly as:
1. Bitmap or Bit vector – A Bitmap or Bit Vector is series or collection
of bits where each bit corresponds to a disk block. The bit can take two
values: 0 and 1: 0 indicates that the block is allocated and 1 indicates a
free block. The given instance of disk blocks on the disk where green

48
blocks are allocated can be represented by a bitmap of 16 bits

as: 0000111000000110.
2. Advantages –
 Simple to understand.
 Finding the first free block is efficient. It requires scanning the
words (a group of 8 bits) in a bitmap for a non-zero word. (A 0-valued
word has all bits 0). The first free block is then found by scanning for
the first 1 bit in the non-zero word.

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

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

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

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

Here are some advantages and disadvantages of free space


management techniques in operating systems:

50
Advantages:

Efficient use of storage space: Free space management techniques


help to optimize the use of storage space on the hard disk or other
secondary storage devices.

1. Easy to implement: Some techniques, such as linked allocation, are


simple to implement and require less overhead in terms of processing
and memory resources.

2. Faster access to files: Techniques such as contiguous allocation can


help to reduce disk fragmentation and improve access time to files.

Disadvantages:

1. Fragmentation: Techniques such as linked allocation can lead to


fragmentation of disk space, which can decrease the efficiency of storage
devices.

2. Overhead: Some techniques, such as indexed allocation, require


additional overhead in terms of memory and processing resources to
maintain index blocks.

3. Limited scalability: Some techniques, such as FAT, have limited


scalability in terms of the number of files that can be stored on the disk.
4. Risk of data loss: In some cases, such as with contiguous allocation,
if a file becomes corrupted or damaged, it may be difficult to recover the
data.

5. Overall, the choice of free space management technique depends on


the specific requirements of the operating system and the storage
devices being used. While some techniques may offer advantages in
terms of efficiency and speed, they may also have limitations and
drawbacks that need to be considered.

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*GOOD LUCK *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

51

You might also like