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

OS Unit 4

Uploaded by

priyakanthan2023
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 views399 pages

OS Unit 4

Uploaded by

priyakanthan2023
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/ 399

UNIT 4

Memory Management
Basics of Memory Management

Main Memory

Courtesy : Abraham Silberschatz, Peter


Baer Galvin, Greg Gagne, Operating
systems, 9th ed., John Wiley & Sons, 2013
Memory : Basics
• Memory is central part for the operation of a modern computer
system
• Memory consists of 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 which may need additional loading from and
storing to specific memory addresses.
• A typical instruction-execution cycle, first fetches an instruction from
memory. The instruction is then decoded and may cause operands to
be fetched from memory.
• After the instruction has been executed on the operands, results may
be stored back in memory.
Memory : Basics (Cont.)
• The memory unit sees only a
• Stream of memory addresses;
• It does not know how they are generated (by the instruction counter,
indexing, indirection, literal addresses, and so on) or
• What they are for (instructions or data).
• Hence we can ignore how a program generates a memory address.
• We are interested only in the sequence of memory addresses
generated by the running program.
Basic Hardware
• Program must be brought (from disk) into memory and placed within a
process for it to be run
• Main memory and registers are only storage CPU can access directly
• Memory unit only sees a stream of addresses + read requests, or
address + data and write requests
• Register access in one CPU clock (or less)
• Main memory can take many cycles, causing a stall Cache sits between
main memory and CPU registers
• Protection of memory required to ensure correct operation
A Base and a Limit Register
• Each process should have a separate memory space.
• To do this, we need the ability to determine the range of legal
addresses that the process may access and to ensure that the process
can access only these legal addresses.
• Operating system provide this protection by using two registers:
• a base and
• a limit
• The base register holds the smallest legal physical memory address
• The limit register specifies the size of the range
A Base and a Limit Register
• For example, if the base register
holds 300040 and
• The limit register is 120900, then
the program can legally access all
addresses from 300040 through
420939 (inclusive).
Hardware Address Protection
Address Binding
• Usually, a program resides on a disk as a binary executable file.
• The program to be executed must be brought into memory and
placed within a process.
• Depending on the memory management in use, the process may be
moved between disk and memory during its execution.
• The processes on the disk that are waiting to be brought into memory
for execution form the input queue.
Address Binding
• Addresses may be represented in different ways during the execution
of the program
• Addresses in the source program are generally symbolic (such as
count)
• A compiler will typically bind these symbolic addresses to relocatable
addresses (such as “14 bytes from the beginning of this module”)
• The linkage editor or loader will in turn bind the relocatable
addresses to absolute addresses (such as 74014)
• Each binding is a mapping from one address space to another.
Binding of Instructions and Data to Memory
• Address binding of instructions and data to memory
addresses can happen at three different stages
– Compile time: If memory location known a priori, absolute code can be generated; must
recompile code if starting location changes

– Load time: Must generate relocatable code if memory location is not known at compile
time

– Execution time: Binding delayed until run time if the process can be moved during its
execution from one memory segment to another
• Need hardware support for address maps (e.g., base and limit registers)
Multistep Processing of a User Program
Program Execution
Compilation

Compiled code

Source Code
Assembler

Assembly Code
Linker/Loader

Machine Code
Logical vs Physical Address Space
• An address generated by the CPU is commonly referred to as a logical address

• An address seen by the memory unit—that is, the one loaded into the
memory-address register of the memory—is commonly referred to as a
physical address.

• The compile-time and load-time address-binding methods generate identical


logical and physical addresses.

• However, the execution-time address binding scheme results in differing logical


and physical addresses.
Logical vs Physical Address Space (Cont.)

• In this case, we usually refer to the logical address as a virtual address.

• The set of all logical addresses generated by a program is a logical address


space;

• The set of all physical addresses corresponding to these logical addresses is a


physical address space.

• The run-time mapping from virtual to physical addresses is done by a hardware


device called the memory-management unit (MMU).
Dynamic Relocation using a
Relocation Register
• The base register is now called a
Relocation Register.
• The value in the relocation register is
added to every address generated by a
user process at the time the address is
sent to memory.
• For example, if the base is at 14000,
then an attempt by the user to address
location 0 is dynamically relocated to
location 14000;
• an access to location 346 is mapped to
location 14346.
Logical vs Physical Address Space (Cont.)

• The two different types of addresses:


• logical addresses (in the range 0 to max) and
• physical addresses (in the range R + 0 to R + max for a base value R)
• The user generates only logical addresses and thinks that the process
runs in locations 0 to max.
• The user program generates only logical addresses and thinks that
the process runs in locations 0 to max.
• However, these logical addresses must be mapped to physical
addresses before they are used.
Dynamic Loading
Dynamic linking
• Static linking
• System libraries and program code seperately
• Some OS support only static linking

• Dynamic linking
• Linking is postponed until execution time
• Wastage of both disk space and main memory is avoided
Swapping
• A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution
• Total physical memory space of processes can exceed physical memory
• This increases the degree of multiprogramming in a system
• Backing store – Normally its hard disk
CONTIGUOUS MEMORY ALLOCATION
Contiguous Allocation

• The main memory must accommodate both the operating


system and the various user processes.
• The memory is usually divided into two partitions:
1. one for the resident operating system
2. one for the user processes.
Contiguous Allocation (Cont.)
• Several user processes want to reside in memory at the same time.
• Then it is to be considered, how to allocate available memory to
the processes that are in the input queue waiting to be brought into
memory.
• In contiguous memory allocation, each process is contained in a
single section of memory that is contiguous to the section
containing the next process.
Contiguous Allocation (Cont.)

• The relocation register contains the value of the smallest physical


address;
• The limit register contains the range of logical addresses (for
example, relocation = 100040 and limit = 74600).
• Each logical address must fall within the range specified by the
limit register.
• The MMU maps the logical address dynamically by adding the
value in the relocation register.
Contiguous Allocation (Cont.)

• This mapped address is sent to memory. When the CPU scheduler


selects a process for execution, the dispatcher loads the relocation
and limit registers with the correct values as part of the context
switch.

• Because every address generated by a CPU is checked against


these registers, we can protect both the operating system and the
other users programs and data from being modified by this
running process.
Hardware Support for Relocation and
Limit Registers
Contiguous Allocation (Cont.)

• The relocation-register scheme provides an effective way


to allow the operating system’s size to change
dynamically. This flexibility is desirable in many
situations.

• For example, the operating system contains code and


buffer space for device drivers.
Contiguous Allocation (Cont.)

• If a device driver (or other operating-system service) is not


commonly used, we do not want to keep the code and data in
memory, as we might be able to use that space for other purposes.

• Such code is sometimes called transient operating-system code;


it comes and goes as needed. Thus, using this code changes the
size of the operating system during program execution.
Contiguous Allocation Technique

• There are two popular techniques used for contiguous


memory allocation

⮚Fixed Partitioning

⮚Variable Partitioning
Fixed Partitioning
• Static partitioning is a fixed partitioning scheme.
• In this partitioning, number of partitions (non-overlapping) in RAM
are fixed but size of each partition may or may not be same.
• As it is contiguous allocation, hence no spanning is allowed.
• Here partition are made before execution or during system configure.
• Each partition is allowed to store only one process.
• These partitions are allocated to the processes as they arrive.
Fixed Partitioning (Cont.)

• The degree of multiprogramming is bound by the number of


partitions.
• In this multiple partition method, 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.
Fixed Partitioning (Cont.)
Example
Disadvantages of Fixed Partitioning
⮚Limitation on the Size of the Process: – Sometimes, when the size of the process
is larger than the maximum partition size, then we cannot load that process into the
memory. So, this is the main disadvantage of the fixed partition.
⮚Degree of Multiprogramming is Less: – We can understand from the degree of
multiprogramming that it means at the same time, the maximum number of
processes we can load into the memory. In Fixed Partitioning, the size of the
partition is fixed, and we cannot vary it according to the process size; therefore, in
fixed partitioning, the degree of multiprogramming is less and fixed.
⮚Internal Fragmentation
Let us first discuss about what is fragmentation and then discuss about Internal
Fragmentation
Fragmentation
• Fragmentation is an unwanted problem where the memory blocks
cannot be allocated to the processes due to their small size and the
blocks remain unused.
• It can also be understood as when the processes are loaded and
removed from the memory they create free space or hole in the
memory and these small blocks cannot be allocated to new
upcoming processes and results in inefficient use of memory.
• Basically, there are two types of fragmentation:
▪ Internal Fragmentation
▪ External Fragmentation
Internal Fragmentation
• It occurs when the space is left inside the partition after
allocating the partition to a process.
• This space is called as internally fragmented space.
• This space can not be allocated to any other process.
• This is because only Fixed (static) partitioning allows to store
only one process in each partition.
• Internal Fragmentation occurs only in Fixed (static)
partitioning .
Internal Fragmentation (Cont.)
Solution to Internal Fragmentation

• This problem is occurring because we have fixed the sizes of


the memory blocks. This problem can be removed if we use
dynamic partitioning for allocating space to the process.
• In dynamic partitioning, the process is allocated only that much
amount of space which is required by the process. So, there is
no internal fragmentation.
Variable - Partitioning

• In the variable-partition scheme, the operating system keeps a table


indicating which parts of memory are available and which are
occupied.
• Initially, all memory is available for user processes and is
considered one large block of available memory, a hole.
• Eventually, memory contains a set of holes of various sizes.
• As processes enter the system, they are put into an input queue.
Variable – Partitioning (Cont.)
Variable – Partitioning (Cont.)
• The operating system takes into account the memory
requirements of each process and the amount of available
memory space in determining which processes are allocated
memory.
• When a process is allocated space, it is loaded into memory,
and it can then compete for CPU time.
• When a process terminates, it releases its memory, which the
operating system may then fill with another process from the
input queue.
Variable – Partitioning (Cont.)

• At any given time, then, we have a list of available block sizes


and an input queue. The operating system can order the input
queue according to a scheduling algorithm. Memory is
allocated to processes until, finally, the memory requirements
of the next process cannot be satisfied — that is, no available
block of memory (or hole) is large enough to hold that process.
Variable – Partitioning (Cont.)

• The operating system can then wait until a large enough block is
available, or it can skip down the input queue to see whether the
smaller memory requirements of some other process can be met.
• In general, as mentioned, the memory blocks available comprise a set
of holes of various sizes scattered throughout memory. When a
process arrives and needs memory, the system searches the set for a
hole that is large enough for this process.
Variable – Partitioning (Cont.)
• If the hole is too large, it is split into two parts. One part is
allocated to the arriving process; the other is returned to the set of
holes.
• When a process terminates, it releases its block of memory,
which is then placed back in the set of holes.
• If the new hole is adjacent to other holes, these adjacent holes are
merged to form one larger hole.
• At this point, the system may need to check whether there are
processes waiting for memory and whether this newly freed and
recombined memory could satisfy the demands of any of these
waiting processes.
Variable – Partitioning (Cont.)
Variable – Partitioning (Cont.)

• This procedure is a particular instance of the general dynamic storage-


allocation problem, which concerns how to satisfy a request of size n
from a list of free holes.

• There are many solutions to this problem. The first-fit, best-fit, and
worst-fit strategies are the ones most commonly used to select a free hole
from the set of available holes.
Partition Allocation Algorithm
First Fit
• Allocate the first hole that is big enough.
• Searching can start either at the beginning
of the set of holes or at the location where
the previous first-fit search ended.
• We can stop searching as soon as we find
a free hole that is large enough.
Best Fit
• Allocate the smallest hole that is
big enough.
• We must search the entire list,
unless the list is ordered by size.
• This strategy produces the smallest
leftover hole.
Worst Fit
• Allocate the largest hole.
• Again, we must search the entire list,
unless it is sorted by size.
• This strategy produces the largest leftover
hole which may be useful for the other
process
Problem to Solve

Question

Given five memory partitions of 100Kb, 500Kb, 200Kb, 300Kb, 600Kb (in
order), how would the first-fit, best-fit, and worst-fit algorithms place processes
of 212 Kb, 417 Kb, 112 Kb, and 426 Kb (in order)? Which algorithm makes the
most efficient use of memory?
Steps to Solve – First Fit
Steps to Solve – Best Fit
Steps to Solve – Worst Fit
Disadvantages of Variable - Partitioning

• Complex Memory Allocation


• External Fragmentation
Complex Memory Allocation

• The task of allocation and deallocation is tough because the size of the
partition varies whenever the partition is allocated to the new process.
The operating system has to keep track of each of the partitions.
• So, due to the difficulty of allocation and deallocation in the dynamic
memory allocation, and every time we have to change the size of the
partition; therefore, it is tough for the operating system to handle
everything.
External Fragmentation

• It occurs when the total amount of empty space required to store the
process is available in the main memory.

• But because the space is not contiguous, so the process can not be
stored.
External Fragmentation (Cont.)
• Consider, three processes P1 (1MB), P2 (3MB), and P3 (1MB), and we want
to load the processes in the various partitions of the main memory.
• Now the processes P1 and P3 are completed, and space that is assigned to the
process P1 and P3 is freed. Now we have partitions of 1 MB each, which are
unused and present in the main memory, but we cannot use this space to load
the process of 2 MB in the memory because space is not contiguous.
• The rule says that we can load the process into the memory only when it is
contiguously residing in the main memory. So, if we want to avoid external
fragmentation, then we have to change this rule.
External Fragmentation (Cont.)
• Example
Solution to External Fragmentation

• This problem is occurring because we are allocating memory


continuously to the processes. So, if we remove this condition
external fragmentation can be reduced.

• One of the way to remove external fragmentation is compaction.


When dynamic partitioning is used for memory allocation then
external fragmentation can be reduced by merging all the free
memory together in one large block.
Solution to External Fragmentation
(Cont.)
Solution to External Fragmentation
(Cont.)
• This technique is also called defragmentation. This larger block of
memory is then used for allocating space according to the needs of the
new processes.
Problem with Compaction
• Due to compaction, the system efficiency is decreased because we need to
move all the free spaces from one place to other.
• In this way, the more amount of time is wasted, and the CPU remains ideal
all the time. Instead of that, with the help of compaction, we can avoid
external fragmentation, but this will make the system inefficient.
Solution to External Fragmentation
(Cont.)

• Another way, paging & segmentation (non-contiguous memory


allocation techniques) where memory is allocated non-
contiguously to the processes.
Fixed vs Variable Partitioning
S.NO. Fixed partitioning Variable partitioning

In multi-programming with variable partitioning


In multi-programming with fixed partitioning the
1. the main memory is not divided into fixed sized
main memory is divided into fixed sized partitions.
partitions.

In variable partitioning, the process is allocated a


2. Only one process can be placed in a partition.
chunk of free memory.

3. It does not utilize the main memory effectively. It utilizes the main memory effectively.

There is presence of internal fragmentation and


4. There is external fragmentation.
external fragmentation.

5. Degree of multi-programming is less. Degree of multi-programming is higher.

6. It is more easier to implement. It is less easier to implement.

7. There is limitation on size of process. There is no limitation on size of process.


Internal vs External Fragmentation
INTERNAL FRAGMENTATION EXTERNAL FRAGMENTATION

Memory allocated to a process may be slightly larger than the


External fragmentation exists when there is enough total memory
requested memory. The difference between these two numbers is
space to satisfy a request but available spaces are not contiguous.
internal fragmentation.

First-fit and best-fit memory allocation does not suffer from internal First-fit and best-fit memory allocation suffers from external
fragmentation. fragmentation.

Systems with variable-sized allocation units, such as the multiple


Systems with fixed-sized allocation units, such as the single
partitions scheme and segmentation suffer from external
partitions scheme and paging suffer from internal fragmentation.
fragmentation.
Non Contiguous Allocation

• Non-contiguous memory allocation is a memory allocation


technique.
• It allows to store parts of a single process in a non-contiguous
fashion.
• Thus, different parts of the same process can be stored at
different places in the main memory.
Techniques
SEGMENTATION
Non-Contiguous Memory Allocation

Segmentation
Segmentation
• Memory-management scheme that supports user view of memory.

• Segmentation overcomes the problem of dealing with memory in terms of its


physical properties which is inconvenient to both the operating system and
the programmer.

• The system have more freedom to manage memory.

• The programmer would get an experience to work in more natural


programming environment.
Program and Segment
• A program is a collection of segments in which segment is a logical
unit which consists of the following :
• main program
• procedure
• Function
• Method
• Object
• local variables, global variables
• common block
• Stack
• symbol table
• arrays
User’s View of a Program
• Most programmers prefer to view memory as a collection of
variable-sized segments with no necessary ordering among the
segments.

• From programmer point of view , a program will appear as a main


program with a set of methods, procedures, or functions. Program
also includes the following data structures:
• Objects
• Arrays
• Stacks and
• Variables

• Modules or data elements is referred by name.

• The programmer talks about “the stack,” “the math library,” and “the
main program” without caring what addresses in memory these
elements occupy.

• Programmer is not concerned with whether the stack is stored before


or after the Sqrt() function.
Logical View of Segmentation
• In the figure, both user space and physical 1
memory space are given.
4
• Segments vary in length, and the length of each
is intrinsically defined by its purpose in the 1
program.
2
• Segments are numbered as 1,2,3 and 4.
3 2
• In Physical memory space, Segments are
allocated in different parts of the memory
3
4
• For example,
Elements within a segment are identified by
their offset from the beginning of the
segment: the first statement of the program, user space physical memory space
the seventh stack frame entry in the stack and
the fifth instruction of the Sqrt()
Example – Compilation of C program

• Normally, when a program is compiled, the compiler automatically constructs


segments reflecting the input program.

• A “C” compiler might create separate segments for the following:


• The code
• Global variables
• The heap, from which memory is allocated
• The stacks used by each thread
• The standard C library

• Libraries that are linked in during compile time might be assigned separate segments.

• The loader would take all these segments and assign them segment numbers.
Segmentation Architecture
• Logical address consists of a two tuple:
<segment-number, offset>
• A logical address consists of two parts: a segment number, s, and an offset
into that segment, d.
• The segment number is used as an index to the segment table.
• The offset d of the logical address must be between 0 and the segment limit.
• If it is not, a trap is sent to the operating system (logical addressing attempt
beyond end of segment).
• When an offset d is legal, it is added to the segment base to produce the
address in physical memory of the desired byte.
Segmentation Architecture (Cont.)
• Segment table – maps two-dimensional physical addresses; each table entry has:
• base – contains the starting physical address where the segments reside in memory
• limit – specifies the length of the segment

• Segment-table base register (STBR) points to the segment table’s location in memory

• Segment-table length register (STLR) indicates number of segments used by a program;


segment number s is legal if s < STLR
Segmentation Architecture (Cont.)
• Protection
• With each entry in segment table associate:
• validation bit = 0 ⇒ illegal segment
• read/write/execute privileges
• Protection bits associated with segments with code sharing occurs at segment
level
• Since segments varies in length, memory allocation is a dynamic
Segmentation Hardware
Segmentation Example
• There are Five Segments numbered from 0 through 4.

• The segments are stored in physical memory as shown in the


diagram.

• The segment table has a separate entry for each segment,


giving the beginning address of the segment in physical
memory (or base) and the length of that segment (or limit).

For example, segment 2 is 400 bytes long and begins at location


4300.
A reference to byte 53 of segment 2 is mapped onto location
4300 + 53 = 4353.

A reference to segment 3, byte 852, is mapped to 3200 (the base of


segment 3) + 852 = 4052.

A reference to byte 1222 of segment 0 would result in a trap to the


operating system, as this segment is only 1,000 bytes long.
PAGING
Paging
• Paging is a memory management scheme that eliminates the need
for contiguous allocation of physical memory.
• Paging permits the physical address space of a process to be non –
contiguous.
• The mapping from virtual to physical address is done by the memory
management unit (MMU) which is a hardware device and this
mapping is known as paging technique.
Example
• Consider a process is divided into 4 pages P0, P1, P2 and P3.
• In the above example, the process P is divided into 4 sections namely
P0, P1, P2, P3.
• All the four sections are mapped in the main memory in a non –
contiguous fashion.
Paging
• In the paging mechanism two addresses are involved
• Logical Address
CPU always generates a logical address.
• Physical Address
A physical address is needed to access the main memory.
Logical Address and Physical Address
• Logical Address or Virtual Address :
An address generated by the CPU
• Logical Address Space or Virtual Address Space:
The set of all logical addresses generated by a program
• Physical Address:
An address actually available on memory unit
• Physical Address Space :
The set of all physical addresses corresponding to the logical
addresses
• The Physical Address Space is conceptually divided into a number of
fixed-size blocks, called frames.
• The Logical address Space is also divided into fixed-size blocks,
called pages.
• Page Size = Frame Size
Logical Address
• Address generated by CPU is divided into:
• Page number (p) – used as an index into a page table which contains base
address of each page in physical memory
• Page offset (d) – combined with base address to define the physical memory
address that is sent to the memory unit

• For given logical address space 2m and page size 2n


Physical Address
• Physical Address is divided into
Frame number(f): Number of bits required to represent the frame of
Physical Address Space or Frame number.
Frame offset(d): Number of bits required to represent particular
word in a frame or frame size of Physical Address Space or word
number of a frame or frame offset.
• If Logical Address = 31 bit, then Logical Address Space = 231 words = 2 G
words (1 G = 230)
• If Logical Address Space = 128 M words = 27 * 220 words, then Logical
Address = log2 227 = 27 bits
• If Physical Address = 22 bit, then Physical Address Space = 222 words = 4
M words (1 M = 220)
• If Physical Address Space = 16 M words = 24 * 220 words, then Physical
Address = log2 224 = 24 bits
Memory Management
• The Mapping between these two address is done to execute the
process using the paging technique.
• The Hardware device that at run time maps virtual address to
physical address is done by the Memory Management Unit (MMU)
Paging Hardware
• How does system perform translation? Simplest solution: use a page
table.
• Page table is a linear array indexed by virtual page number that gives
the physical page frame that contains that page through the look up
process.
Paging Hardware
Paging Hardware
LOOK UP PROCESS
Step 1 :Extract page number.
Step 2 : Extract offset.
Step 3 : Check that page number is within address space of process.
Step 4 : Look up page number in page table.
Step 5 : Add offset to resulting physical page number
Step 6 : Access memory location.
Paging Model of Logical and Physical
Memory
• In the example a process is divided into four pages as
page0, page1, page 2 and page3.
• All the four pages are mapped to the frames in the physical
memory through the page table.
• The page table maps the page number to its corresponding
frame number in the physical memory.
• page0 is mapped to frame1 , page1 is mapped to frame 4,
page2 is mapped to frame 3 and page4 is mapped to frame
7.
Paging Example
Free Frame List
Free Frame List
The example
Fig (a) shows a process divided into 4 pages and the list of free frames
available in the physical memory.

Fig(b) shows the mapping of these pages to the free frames available and
the updated list of free frames available in the physical memory.
Page Table
• Page table is kept in main memory
• Page-table base register (PTBR) points to the page table
• Page-table length register (PTLR) indicates size of the page table
Problem with paging
• In this scheme every data/instruction access requires two memory
accesses
• One for the page table and one for the data / instruction
• The two memory access problem can be solved by the use of a
special fast-lookup hardware cache called associative memory or
translation look-aside buffers (TLBs)
Translation Lookaside Buffer (TLB)
• Speed up the lookup problem with a cache. Store most recent
page lookup values in TLB.
Procedure of TLB
Step 1 :Extract page number.
Step 2 : Extract offset.
Step 3 : Look up page number in TLB.
Step 4 : If there, add offset to physical page number and access
memory location.
Step 5 : Otherwise, trap to OS. OS performs check, looks up physical
page number, and loads translation into TLB. Restarts the
instruction.
Paging with TLB
• If the page number is not in the TLB (TLB miss) a memory
reference to the page table must be made.
• In addition, we add the page number and frame number into
TLB
• If the TLB already full, the OS have to must select one for
replacement
• Some TLBs allow entries to be wire down, meaning that they
cannot be removed from the TLB, for example kernel codes
• The percentage of times that a particular page number is found
in the TLN is called hit ratio
• Fixed size allocation of physical memory in page frames dramatically
simplifies allocation algorithm.
• OS can just keep track of free and used pages and allocate free pages
when a process needs memory.
• There is no fragmentation of physical memory into smaller and smaller
allocatable chunks.
Effective Access Time
• Associative Lookup = ε time unit
• Can be < 10% of memory access time
• Hit ratio = α
• Hit ratio – percentage of times that a page number is found in the associative
registers; ratio related to number of associative registers
• Consider α = 80%, ε = 20ns for TLB search, 100ns for memory access
• Effective Access Time (EAT)
EAT = (1 + ε) α + (2 + ε)(1 – α)
=2+ε–α
• Consider α = 80%, ε = 20ns for TLB search, 100ns for memory access
• EAT = 0.80 x 100 + 0.20 x 200 = 120ns
Memory Protection
The process has to be protected for the following reasons:
• Preventing one process from reading or writing another process' memory.
• Preventing one process from reading another process' memory.
• Preventing a process from reading or writing some of its own memory.
• Preventing a process from reading some of its own memory.
Memory Protection
• Memory protection is implemented by associating protection bit with each frame to
indicate if read-only or read-write access is allowed
• Can also add more bits to indicate page execute-only, and so on
• Valid-invalid bit attached to each entry in the page table:
• “valid” indicates that the associated page is in the process’ logical address space,
and is thus a legal page
• “invalid” indicates that the page is not in the process’ logical address space
• Or use page-table length register (PTLR)
• Any violations result in a trap to the kernel
Valid – Invalid Bit
• Pages that are not loaded into memory are marked as invalid in the
page table, using the invalid bit.
• In the above example Page0 to page5 is present in the memory .so ,
the corresponding valid/Invalid bit is marked as V(Valid).
• The other pages which are not loaded such as page6 and page7 are
marked as I(Invalid)
PageFault
• if a page is needed that was not originally loaded up, then a page
fault trap is generated
• Page fault is handled by the following steps:
Step 1: The memory address requested is first checked, to make sure it
was a valid memory request.
Step 2: If the reference was invalid, the process is terminated.
Otherwise, the page must be paged in.
Step 3: A free frame is located, possibly from a free-frame list.
Step 4: A disk operation is scheduled to bring in the necessary page
from disk.
Step 5: When the I/O operation is complete, the process's page table is
updated with the new frame number, and the invalid bit is changed
to indicate that this is now a valid page reference.
Step 6: The instruction that caused the page fault must now be
restarted from the beginning
Shared Pages
• Shared code
• One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers, window systems)
• Similar to multiple threads sharing the same process space
• Also useful for interprocess communication if sharing of read-write pages is
allowed
• Private code and data
• Each process keeps a separate copy of the code and data
• The pages for the private code and data can appear anywhere in the logical
address space
Shared Pages
Shared Pages

• In the example figure, three pages ed1,ed2 and ed3 are shred by
three processes Process1 , Process2, Process3.
• The page is loaded only once and the corresponding frame number is
updated in the page table of the three processes.
STRUCTURE OF A PAGE TABLE
Structure of the Page Table

• Memory structures for paging can get huge using straight-forward methods
• Consider a 32-bit logical address space as on modern computers
• Page size of 4 KB (212)
• Page table would have 1 million entries (232 / 212)
• If each entry is 4 bytes -> 4 MB of physical address space / memory for page
table alone
• That amount of memory used to cost a lot
• Don’t want to allocate that contiguously in main memory
• Hierarchical Paging
• Hashed Page Tables
• Inverted Page Tables
Two-Level Paging Example

• Simple solution to this problem is to divide the page table into smaller pieces We can accomplish
this division in several ways
• A logical address (on 32-bit machine with 1K page size) is divided into:
• a page number consisting of 22 bits
• a page offset consisting of 10 bits

• Since the page table is paged, the page number is further divided into:
• a 12-bit page number
• a 10-bit page offset

• Thus, a logical address is as follows:

• where p1 is an index into the outer page table, and p2 is the displacement within the page of the inner
page table
Hierarchical Page Tables

• Break up the logical address space into multiple page tables

• A simple technique is a two-level page table

• We then page the page table


Two-Level Page-Table Scheme
Address-Translation Scheme
for two level 32 bit paging
architecture
64-bit Logical Address Space
• For a system with a 64-bit logical address space, a two-level paging scheme is no longer appropriate
• If page size is 4 KB (212)
• Then page table has 252 entries
• If two level scheme, inner page tables could be 210 4-byte entries
• Address would look like

• Outer page table has 242 entries or 244 bytes


• One solution is to add a 2nd outer page table
• But in the following example the 2nd outer page table is still 234 bytes in size
• And possibly 4 memory access to get to one physical memory location
• We can divide the outer page table in various ways. For example, we
can page the outer page table, giving us a three-level paging scheme.
Suppose that the outer page table is made up of standard-size pages
(210 entries, or 212 bytes). In this case, a 64-bit address space is still
daunting
• This keeps growing that is the reason hierarchical page tables are
generally considered inappropriate
Three-level Paging Scheme
Hashed Page Tables
• One approach for handling address spaces larger than 32 bits is to use
a hashed page table, with the hash value being the virtual page
number
• Common in address spaces > 32 bits
• The virtual page number is hashed into a page table
• This page table contains a chain of elements hashing to the same
location
• Each element contains (1) the virtual page number (2) the value of the
mapped page frame (3) a pointer to the next element
Hashed Page Tables (Cont.)

• Virtual page numbers are compared in this chain searching for a match
• If a match is found, the corresponding physical frame is extracted

• Variation for 64-bit addresses is clustered page tables


• Similar to hashed but each entry refers to several pages (such as 16)
rather than 1
• Especially useful for sparse address spaces (where memory
references are non-contiguous and scattered)
Hashed Page Table
Inverted Page Table

• Rather than each process having a page table and keeping track of all
possible logical pages, track all physical pages

• One entry for each real page of memory

• Entry consists of the virtual address of the page stored in that real memory
location, with information about the process that owns that page
Inverted Page Table Architecture
Paging - ARM
• The paging scheme used by ARM processors uses a 2 level page table.
• The first level page table has 16kB size and each entry covers a region
of 1MB (sections).
• The second level page tables have 1kB size and each entry covers a
region of 4KB (pages).
• The TLB also supports 16MB sections and 64kB pages
Paging - ARM

• On ARMv4 and ARMv5 pages might also be divided into what the
manual calls 'subpages‘
• Example : for a 4KB page, 1KB subpages, and for a 64KB page, 16KB
subpages.
• The ARM processor supports 2 independent page tables.
• The first page table can be abbreviated from the full 16kB
• The second page table is always 16KB in size and is used for addresses
beyond the end of the first page table.
Paged Segmentation
• Paging + Segmentation
• Takes advantage of both paging and segmentation
• Implementation: Treat each segment independently. Each segment
has a page table
• Logical address now consists of 3 parts:
• Segment number
• Page number
• Offset
Paged Segmentation Implementation

• Segment table has base address for page table


• Look up <page number, offset> in page table
• Get physical address in return
• Examples: MULTICS and Intel 386
Combined Paging and Segmentation Scheme

• In a combined paging/segmentation system, a user’s address is


broken up into a number of segments.
• Each segment is broken up into a number of fixed-sized pages which
are equal in length to the main memory frame
Address Translation
Advantages of Combined Scheme
• Reduces memory usage as opposed to pure paging
• Page table size limited by segment size
• Segment table has only one entry per actual segment
• Share individual pages by copying page table entries
• Share whole segments by sharing segment table entries, which is the
same as sharing the page table for that segment
• Most advantages of paging still hold
• Simplifies memory allocation
• Eliminates external fragmentation
• This system combines the efficiency in paging with protection and
sharing capabilities of segmentation
Disadvantage
• Internal fragmentation still exists
Example: The Intel 32 and 64-bit
Architectures
• Dominant industry chips

• Pentium CPUs are 32-bit and called IA-32 architecture

• Current Intel CPUs are 64-bit and called IA-64 architecture

• Many variations in the chips, cover the main ideas here


Example: The Intel IA-32 Architecture
• Memory management is divided as – Segmentation and Paging
• CPU generates logical address
• Selector given to segmentation unit
• Which produces linear addresses
• Linear address given to paging unit
• Which generates physical address in main memory
• Paging units form equivalent of MMU
• Pages sizes can be 4 KB or 4 MB
The Intel IA-32 Segmentation
• Supports both segmentation and paging
• Each segment can be 4 GB
• Up to 16 K segments per process
• Divided into two partitions
• First partition of up to 8 K segments are private to process
(kept in local descriptor table (LDT))
• Second partition of up to 8K segments shared among all
processes (kept in global descriptor table (GDT))
Intel IA-32 Segmentation
• The logical address is a pair (selector, offset), where the selector is a
16-bit number:

• s designates segment number


• g indicates if segment is GDT or LDT
• p deals with protection
• Offset is a 32-bit number and it specifies the location of the byte
within segment
Intel IA-32 Segmentation
Intel IA-32 Segmentation
• There are six segment registers – six segments can be addressed at a
time
• There are six 8-byte microprogram registers to hold LDT or GDT
descriptors
• Linear address is 32-bits long
• Base and limit of segment is used to generate linear address
• Limit is used to check address validity
• Invalid address results in a trap
• Value of base with offset results in 32-bit address
IA-32 Paging
• Pages sizes of 4KB or 4MB is allowed in IA-32
• 4KB pages use two-level paging scheme

• The 10 higher order bits reference the page directory – outermost


page table
• The innermost 10 bits refer the inner page table
• Lower order 12 bits refer the offset in the page
• 1 bit in page directory is Page-Size flag
• If set, indicates 4MB page rather than the default 4KB page
Intel IA-32 Paging Architecture
Intel IA-32 Page Address Extensions
• 32-bit address limits led Intel to create page address extension (PAE),
allowing 32-bit apps access to more than 4GB of memory space
• Paging went to a 3-level scheme
• Top two bits refer to a page directory pointer table
• Page-directory and page-table entries moved to 64-bits in size
• Net effect is increasing address space to 36 bits – 64GB of physical
memory
• Linux and MAC support PAE
• But, 32-bit versions of Windows desktop operating systems still provide
support for only 4 GB of physical memory, even if PAE is enabled.
Intel IA-32 Page Address Extensions
Intel x86-64
• Current generation Intel x86 architecture
• Support for larger physical and logical address spaces
• 64 bit-systems can address 264 of addressable memory (16 exabytes)
• In practice, only implement 48 bit addressing
• Page sizes of 4 KB, 2 MB, 1 GB
• Four levels of paging hierarchy
• Can also use PAE so virtual addresses are 48 bits and physical
addresses are 52 bits
Virtual Memory
Virtual memory is a technique that allows the execution of processes that are not completely in Main

memory.

Virtual memory – involves the separation of logical memory as perceived by users from physical memory.

Need for virtual memory

⮚ Programs often have code to handle unusual error conditions. Since these errors seldom, if ever, occur in

practice, this code is almost never executed.

⮚ Arrays, lists, and tables are often allocated more memory than they actually need. An array may be declared

100 by 100 elements, even though it is seldom larger than 10 by 10 elements.

⮚ An assembler symbol table may have room for 3,000 symbols, although the average program has less than

200 symbols. Certain options and features of a program may be used rarely. Even in those cases where the

entire program is needed, it may not all be needed at the same time.
Virtual Memory
⮚ One major advantage of this scheme is that
programs can be larger than physical memory.

⮚ Further, virtual memory abstracts main memory into


an extremely large, uniform array of storage,
separating logical memory as viewed by the user
from physical memory.

⮚ This separation allows an extremely large virtual


memory to be provided for programmers when only
a smaller physical memory is available.

⮚ Virtual memory makes the task of programming much easier, because the programmer no longer needs to
worry about the amount of physical memory available.
⮚ This technique frees programmers from the concerns of memory-storage limitations.
⮚ Virtual memory also allows processes to share files easily and to implement shared memory.
⮚ In addition, it provides an efficient mechanism for process creation.
Virtual Memory

⮚ A program would no longer be constrained by


the amount of physical memory that is
available.

⮚ Users would be able to write programs for an


extremely large virtual address space,
simplifying the programming task.

⮚ Because each user program could take less


physical memory.

⮚ More programs could be run at the same time,


with a corresponding increase in CPU
utilization and throughput.

⮚ With no increase in response time or


turnaround time.
Virtual address space
⮚The virtual address space of a process refers to the logical (or virtual) view
of how a process is stored in memory.
⮚The virtual address space for a process is the set of virtual memory
addresses that it can use.
⮚The address space for each process is private and cannot be accessed by
other processes unless it is shared.
⮚A virtual address does not represent the actual physical location of an
object in memory.
⮚Instead, the system maintains a page table for each process, which is an
internal data structure used to translate virtual addresses into their
corresponding physical addresses.
⮚Each time a thread references an address, the system translates the virtual
address to a physical address.
Virtual address space

⮚ In this view, a process begins at a certain logical address—say, address 0—and


exists in contiguous memory, as shown in Figure.

⮚ Here it is allowed the heap to grow upward in memory as it is used for dynamic
memory allocation.

⮚ Allows the stack to grow downward in memory through successive function calls.

⮚ The large blank space (or hole) between the heap and the stack is part of the
virtual address space but will require actual physical pages only if the heap or
stack grows.

⮚ Virtual address spaces that include holes are known as sparse address spaces.

⮚ Using a sparse address space is beneficial because the holes can be filled as the
stack or heap segments grow or if we wish to dynamically link libraries (or
possibly other shared objects) during program execution.

Virtual address space


Virtual Address Space vs Physical
Address Space
⮚ Address uniquely identifies a
location in the memory.
⮚ The logical address is a
virtual address and can be
viewed by the user.
⮚ The user can’t view the
physical address directly.
⮚ The logical address is used
like a reference, to access the
physical address.
⮚ Logical address is generated by CPU during a
program execution whereas, the physical
address refers to a location in the memory unit.
Shared Library Using Virtual Memory
⮚ In addition to separating logical memory from physical memory,
virtual memory allows files and memory to be shared by two or more
processes through page sharing.

This leads to the following benefits:


⮚ System libraries can be shared by several processes through
mapping of the shared object into a virtual address space.
⮚ Although each process considers the libraries to be part of its
virtual address space, the actual pages where the libraries
reside in physical memory are shared by all the processes.
⮚ Typically, a library is mapped read-only into the space of each
process that is linked with it, Similarly, processes can share
memory.
⮚ Two or more processes can communicate through the use of
shared memory.
⮚ Pages can be shared during process creation with the fork()
system call, thus speeding up process creation.
Shared Library Using Virtual Memory
⮚ Virtual memory allows
one process to create a
region of memory that it
can share with another
process.

⮚ Processes sharing this


region consider it part
of their virtual address
space, yet the actual
physical pages of
memory are shared,
much as is illustrated in
Figure.
Demand Paging – Basic Concepts

How an executable program might be loaded from disk into memory?

⮚ Load the entire program in physical memory at program execution time.


⮚ problem of this method, we may not initially need the entire program
in memory

⮚ An alternate approach to resolve this problem to load pages only as they


are needed. This method called as Demand Paging.

⮚ Demand paging commonly used in virtual memory systems.

⮚ Demand paged virtual memory, pages are loaded only when they are
needed during program execution.
Demand Paging

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


swapping where processes reside in secondary memory and pages are
loaded only on demand, not in advance.
Demand Paging

⮚ In the context of a demand-paging system, use of the term “swapper” is


technically incorrect.

⮚ Because swapper manipulates entire process into memory, whereas a


pager is concerned with the individual pages of a process.

⮚ We thus use “pager,” rather than “swapper,” in connection with demand


paging.

⮚ Lazy swapper – will not swap the entire process into memory, A lazy
swapper never swaps a page into memory unless that page will be needed.
Valid-Invalid Bit
⮚ Need some form of hardware support to distinguish
between the pages that are in memory and the pages that
are on the disk.

⮚ The valid–invalid bit scheme can be used for this


purpose.

⮚ With each page table entry a valid–invalid bit is


associated
(v ⇒ in-memory – memory resident, i ⇒ not-in-
memory)

⮚ Initially valid–invalid bit is set to i on all entries.

⮚ Example of a page table snapshot:


Page Table
When Some Pages Are Not in Main Memory
⮚ Page fault - if the process tries to access a page that was not brought into memory.
⮚ Operating system’s failure to bring the desired page into memory.
Page Fault
The procedure for handling page fault
1. Check an internal table (usually kept with the process control block) whether
the reference was a valid or an invalid memory access.
2. If the reference
1. Invalid, we terminate the process.
2. Valid but we have not yet brought in that page, we now page it in.
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. We restart the instruction that was interrupted by the trap. The process can
now access the page as though it had always been in memory.
Steps in Handling a Page Fault
Aspects of Demand Paging
⮚Extreme case – start process with no pages in memory
⮚OS sets instruction pointer to first instruction of process, non-memory-
resident -> page fault
⮚After this page is brought into memory, the process continues to
execute, faulting as necessary until every page that it needs is in
memory.
⮚At that point, it can execute with no more faults.
⮚This scheme is pure demand paging: never bring a page into memory
until it is required.
⮚The hardware to support demand paging
⮚Page table - This table has the ability to mark an entry invalid through
a valid–invalid bit or a special value of protection bits.
⮚Secondary memory - This memory holds those pages that are not
present in main memory.
⮚The secondary memory is usually a high-speed disk. It is known as the
swap device
Performance of Demand Paging
⮚Demand paging can significantly affect the performance of a computer
system.
⮚Memory-access time denoted ma, ranges from 10 to 200 nanoseconds. If
there is no page faults, the effective access time is equal to the memory
access time.
⮚If page fault occurs, we must first read the relevant page from disk and
then access the desired word.
⮚Let p be the probability of a page fault (0 ≤ p ≤ 1).
⮚We would expect p to be close to zero, expect to have only a few page
faults.
⮚The effective access time is then
effective access time = (1 − p) × ma + p × page fault time.
⮚To compute the effective access time, we must know how much time is
needed to service a page fault.
Performance of Demand Paging
A page fault causes the following sequence to occur:
1. Trap to the operating system

2. Save the user registers and process state

3. Determine that the interrupt was a page fault

4. Check that the page reference was legal and determine the location of the page on the disk

5. Issue a read from the disk to a free frame:

1. Wait in a queue for this device until the read request is serviced

2. Wait for the device seek and/or latency time

3. Begin the transfer of the page to a free frame

6. While waiting, allocate the CPU to some other user

7. Receive an interrupt from the disk I/O subsystem (I/O completed)

8. Save the registers and process state for the other user

9. Determine that the interrupt was from the disk

10.Correct the page table and other tables to show page is now in memory
Performance of Demand Paging
⮚Not all 12 steps are necessary in every case.
⮚When the CPU is allocated to another process while waiting I/O occurs.
⮚Allows multiprogramming to maintain CPU utilization but requires
additional time to resume the page-fault service routine when the I/O
transfer is complete.

⮚In any case, we are faced with three major components of the page-fault
service time:
⮚ Service the page-fault interrupt.
⮚ Read in the page.
⮚ Restart the process.
Performance of Demand Paging

⮚The first and third tasks can be reduced, with careful coding to
several hundred instructions. These tasks may take from 1 to
100 microseconds each.
⮚Probably, The page-switch time - 8 milliseconds.
⮚ A typical hard disk has an average latency of 3 milliseconds,

⮚ A seek of 5 milliseconds

⮚Remember also that we are looking at only the device-service


time.
⮚If a queue of processes is waiting for the device, we have to
add device-queueing time
Performance of Demand Paging
For example,
Average page-fault service time - 8 milliseconds
memory access time - 200 nanoseconds
The effective access time?
effective access time = (1 − p) × ma + p × page fault time.
effective access time = (1 − p) × (200) + p (8 milliseconds)
= (1 − p) × 200 + p × 8,000,000
= 200 + 7,999,800 × p.
Let p be the probability of a page fault (0 ≤ p ≤ 1)

⮚We see, then, that the effective access time is directly proportional to
the page-fault rate.
Performance of Demand Paging
⮚ Swap space I/O faster than file system I/O even if on the same device
⮚ Swap allocated in larger chunks, less management needed than file
system
⮚ Copy entire process image to swap space at process load time
⮚ Then page in and out of swap space
⮚ Used in older BSD Unix
⮚ Demand page in from program binary on disk, but discard rather than
paging out when freeing frame
⮚ Used in Solaris and current BSD
⮚ Still need to write to swap space
⮚ Pages not associated with a file (like stack and heap) –
anonymous memory
⮚ Pages modified in memory but not yet written back to the file
system
⮚ Mobile systems
⮚ Typically don’t support swapping
⮚ Instead, demand page from file system and reclaim read-only pages
(such as code)
Demand Paging

Advantages
⮚ Large virtual memory.
⮚ More efficient use of memory.
⮚ There is no limit on degree of multiprogramming.

Disadvantages
⮚ Number of tables and the amount of processor over head for handling
page interrupts are greater than in the case of the simple paged
management techniques.
Copy-on-Write
⮚The fork() system call creates a child process that is a duplicate of its
parent. Creating a copy of the parent’s address space for the child,
duplicating the pages belonging to the parent.

⮚Child processes invoke the exec() system call immediately after creation,
the copying of the parent’s address space

⮚Copy-on-Write (COW) allows both parent and child processes to


initially share the same pages in memory
⮚If either process modifies a shared page, only then is the page copied

⮚COW allows more efficient process creation as only modified pages are
copied
Copy-on-Write
⮚The child process attempts to modify a page containing portions of the
stack, with the pages set to be copy-on-write.

⮚The operating system will create a copy of this page, mapping it to the
address space of the child process.

⮚The child process modify its copied page and not the page belonging to
the parent process.

⮚Obviously, when the copy-on-write technique is used, only the pages


that are modified by either process are copied; all unmodified pages can
be shared by the parent and child processes.

⮚The following figure shows copy on write on page C


Before Process 1 Modifies Page C

After Process 1 Modifies Page C


Copy-on-Write
⮚In general, free pages are allocated from a pool of zero-fill-on-demand
pages
⮚Pool should always have free frames for fast demand page execution
⮚Don’t want to have to free a frame as well as other processing
on page fault
⮚Why zero-out a page before allocating it?

⮚vfork() variation on fork() system call has parent suspend and child using
copy-on-write address space of parent
⮚Designed to have child call exec()
⮚Very efficient
Need for Copy-on-Write

⮚ Copy-on-write or CoW is a technique to efficiently copy data resources in a computer system.

⮚ If a unit of data is copied but not modified, the "copy" can exist as a reference to the original data.

⮚ Only when the copied data is modified is a copy created, and new bytes are actually written.

⮚ Copy-on-write is closely related to data deduplication.

⮚ Whereas data deduplication analyzes chunks or blocks of data, copy-on-write applies to entire files or
allocated units of memory.
Page Replacement Algorithms
⮚Page replacement is a process of swapping out an existing page from the
frame of a main memory and replacing it with the required page.
⮚Reason for Page Replacement
⮚Page fault
⮚A page fault occurs when a page referenced by the CPU is not
found in the main memory.
⮚The only solution for the page fault is, the required page has to be
brought from the secondary memory into the main memory.
⮚This is done by replacing the already occupied page in the main
memory with the desired page.
Steps in Page Replacement
⮚Identify the location of the desired page on the disk.
⮚If the page fault occurs, identify a free frame
⮚If there is a free frame then locate the desired page into the free
frame
⮚If there is no free frame then use a page-replacement algorithm to
select a victim frame that is to be replaced.
⮚Now write the victim page to the disk then update the page and
frame tables.
⮚Read the desired page into the newly freed frame then change the
page and frame tables.
⮚Continue the process when there is a page fault occurs again
Steps in Page Replacement
Page Replacement Algorithms
⮚Page replacement algorithms help to decide which page must be
swapped out from the main memory to create a room for the incoming
page.
⮚Various page replacement algorithms are
⮚First In First Out (FIFO) page replacement algorithm
⮚Optimal page replacement algorithm
⮚Least Recently Used (LRU) page replacement algorithm
⮚LRU Approximation page replacement algorithm
⮚Additional Reference Bits Algorithm
⮚Second Chance / Clock Algorithm
⮚Enhanced Second Chance Algorithm
⮚Counting Based page replacement algorithm
⮚Least Frequently Used (LRU) page replacement algorithm
⮚Most Frequently Used (MRU) page replacement algorithm
⮚Page Buffering algorithm
FIFO Page Replacement Algorithm
⮚As the name suggests, this algorithm works on the principle of
“First in First out“.
⮚It replaces the oldest page that has been present in the main
memory for the longest time.
⮚It is implemented by keeping track of all the pages in a queue.
⮚Steps in FIFO
⮚Bring the page into the queue
⮚If page is available then do
⮚Else there is a page fault
⮚If there is a free frame, locate the desired page which causes the page
fault
⮚If there is no free frame, identify the victim frame to be replaced
⮚The page which comes first into the queue will be chosen as the victim
frame.
⮚Now this victim frame will be replaced with the desired page and the
queue is updated for further process.
FIFO Page replacement algorithms

Queue after second replacement

4* 7* 6* 1*
7 6 1 2
6 1 2 7
Initial Queue Queue after first replacement Queue after third replacement

* Indicates the top element of the queue or the


element which comes first into the queue
FIFO Illustrating Belady’s Anomaly

If number of page frames increases page fault also increases. This is called as Belady’s Anomaly
Optimal Page Replacement Algorithm

⮚This algorithm replaces the page that will not be referred by the
CPU in future for the longest time.
⮚It is practically difficult to implement this algorithm.
⮚This is because the pages that will not be used in future for the
longest time cannot be predicted.
⮚However, it is the best known algorithm and gives the least
number of page faults.
⮚Hence, it is used as a performance measure criterion for other
algorithms.
Optimal Page Replacement Algorithm
⮚Steps in the Working Process (This is only for understanding purpose)
⮚Create required number of columns based on the reference strings
used
⮚A pointer is used which moves from MSB to LSB of the columns
⮚The pointer identifies the future reference of the strings
⮚If the page fault occurs, identify the free frame
⮚If there is a free frame, locate the desired page into the free frame
⮚If there is no free frames, choose a victim frame that is to replaced
with the desired page
⮚The victim frame is chosen by the pointer, where the pointer choses
a victim frame which will be referred very later in the future
⮚Now the desired page is located in the main memory which caused
the page fault
1 2 3 4 5 6 7 8 9 10

4 7 6 1 7 6 1 2 7 2

7 is again referred 6 is again referred 1 is again referred 2 is again referred


4 is not in 6th position in in 7th position in in 10th position in
in 5th and 9th
referred in the the future the future the future
position in the
future
future
LRU Page Replacement Algorithm
⮚As the name suggests, this algorithm works on the principle of
“Least Recently Used“.
⮚It replaces the page that has not been referred by the CPU for
the longest time.
⮚Use past knowledge rather than future
⮚Additionally LRU can be implemented using counter and stack
Positions 1 2 3 4 5 6 7 8 9 10

7 is referred in
6 is referred in
4 is referred in the 2nd position
the 3nd position
the 1st position before, so it is
before, so it is
before, so it is replaced
replaced
replaced
LRU IMPLEMENTATION
Counter implementation
⮚Every page entry has a counter
⮚every time page is referenced through this entry
⮚When a page needs to be changed, look at the
counters to find smallest value Search through table
LRU IMPLEMENTATION
Stack implementation
⮚Keep a stack of page numbers in a double link form
⮚If Page referenced
⮚move it to the top
⮚requires 6 pointers to be changed
⮚But each update more expensive
⮚No search for replacement
⮚LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
Stack to Record Most Recent Page References
LRU Approximation Algorithms

⮚The reference bit for a page is set by the hardware whenever that page is
referenced (either a read or a write to any byte in the page).
⮚Reference bits are associated with each entry in the page table.
⮚With each page associate a bit, initially as ‘0’
⮚When page is referenced, set the bit to ‘1’
⮚Now replace the page whose bit is ‘0’ if bit ‘1’ exists
⮚Order of the page is not known but the used pages are known
LRU Approximation – Additional Reference Bits
Algorithm
⮚Limitations in using single reference bit had been solved by
providing the order of the pages
⮚Additional reference bits are ‘1’ byte in capacity
⮚Already existing reference bits are shifted to right in the
additional reference bits table
⮚Page with the lowest number is the least recently used which
will be chosen for replacement
⮚FIFO can be used at the time of tie
⮚ Initially all the bits are set to ‘0’

Reference Bit Additional Bits

0 0 0 0 0 0 0 0
Page 0 0

0 0 0 0 0 0 0 0
Page 1 0

0 0 0 0 0 0 0 0
Page 2 0

0 0 0 0 0 0 0 0
Page 3 0

0 0 0 0 0 0 0 0
Page 4 0
⮚ Pages 1 and 3 are referred

Reference Bit Additional Bits

Page 0 0 0 0 0 0 0 0 0 0

Page 1 1 0 0 0 0 0 0 0 0

Page 2 0 0 0 0 0 0 0 0 0

Page 3 1 0 0 0 0 0 0 0 0

Page 4 0 0 0 0 0 0 0 0 0
⮚ The referred bit is copied to the MSB of the
additional bits and reset the reference bit to ‘0’

Reference Bit Additional Bits

Page 0 0 0 0 0 0 0 0 0 0

Page 1 0 1 0 0 0 0 0 0 0

Page 2 0 0 0 0 0 0 0 0 0

Page 3 0 1 0 0 0 0 0 0 0

Page 4 0 0 0 0 0 0 0 0 0
⮚ Now pages 1 and 2 are referred. Then the bits which
are present already in the MSB of the Additional bits
table is shifted to right one place
Reference Bit Additional Bits

Page 0 0 0 0 0 0 0 0 0 0

Page 1 1 0 1 0 0 0 0 0 0

Page 2 1 0 0 0 0 0 0 0 0

Page 3 0 0 1 0 0 0 0 0 0

Page 4 0 0 0 0 0 0 0 0 0
⮚ Again the process continues by copying the referred
bits to the MSB of Additional bits table

Reference Bit Additional Bits

Page 0 0 0 0 0 0 0 0 0 0

Page 1 0 1 1 0 0 0 0 0 0

Page 2 0 1 0 0 0 0 0 0 0

Page 3 0 0 1 0 0 0 0 0 0

Page 4 0 0 0 0 0 0 0 0 0
LRU Approximation – Second Chance/Clock
Algorithm
⮚By default FIFO is used
⮚Page is replaced based on reference bit
⮚If the reference bit is ‘0’ then the page is considered as not recently used and
taken for replacement
⮚If the reference bit is ‘1’ then the page is considered as recently used and it is
given a second chance to stay in the memory
⮚Now its reference bit is reset to ‘0’
⮚ Fig (a) denotes next victim
as page containing
reference bits as 1, Fig (b)
denotes that second chance
is given to the referred
pages and points the next
victim as page containing
reference bit as ‘0’

⮚At the mean time the


reference bit is reset to ‘0’
for the page who got the
second chance
• reference strings - 3 3 5 4 7 1 5 5 1 4 3 7 6 3 4 1
30 31 31 31

50 50

40

31 30 30 30

70 50 70 10 70 10 70 10

40 40 50 51

30 30 31 30

70 11 40 11 40 11 70 10

51 51 51 50
• reference strings - 3 3 5 4 7 1 5 5 1 4 3 7 6 3 4 1
30 30 31 30

70 11 40 11 40 11 70 10

51 51 51 50

60 60 60 60

70 10 70 30 70 30 10 30

50 50 40 40

• Number of page hits – 4


• Number of page faults – 12
LRU Approximation – Enhanced
Second Chance Algorithm

⮚Use both the reference bit and modify bit for the replacement
⮚(0,0) – neither recently used or modified – best to replace
⮚(0,1) – not recently used but modified – page needs to be
written before replacement
⮚(1,0) – recently used but clean – probably will be used again
soon
⮚(1,1) – recently used and modified – may be used again, has to
be written to the disk
⮚Out of these four classes, the page which falls under (0,0) will
be replaced first, then (0,1) will be chosen for replacement, next
(1,0) and finally (1,1) will be chosen for replacement
Understanding the Pros and cons of the
page replacement techniques
FIFO Page Replacement Algorithm
Advantages
⮚ Easy to understand and execute

Disadvantages
⮚ It is not very effective
⮚ System needs to keep track of each frame
⮚ Page fault increases when increasing the page frames,
undergoes Belady’s anomaly
Understanding the Pros and cons of the
page replacement techniques
Optimal Page Replacement Algorithm
Advantages
⮚ Lowest page fault rate
⮚ Never suffers from Belady's anomaly
⮚ Twice as good as FIFO

Disadvantages
⮚ Difficult to implement
⮚ It needs forecast i.e. Future knowledge
Understanding the Pros and cons of the page
replacement techniques
LRU Page Replacement Algorithms
Advantages
⮚ It is amenable to full statistical analysis
⮚Never suffers from Belady's anomaly
⮚Easy to identify the faulty page that is not needed to
long time

Disadvantages
⮚It has more complexity
⮚Need additional Data Structure and high hardware support
COUNTING ALGORITHMS
⮚It keeps count of number of references (frequencies) made to
each page in a reference string
⮚Whenever a page comes in its frequency increases
⮚Whenever a page leaves the memory its frequency is reset
⮚Least frequently used page replacement
⮚Page with least number of frequency is replaced first
⮚Assumption – Heavily used page will be referred again
⮚When there is a tie in frequencies, then use FIFO
COUNTING ALGORITHMS

⮚Most Frequently used Page replacement algorithm


⮚A page which has maximum number of frequencies in the counter will be replaced
first
⮚Assumption – The page just brought in the memory is yet to use again
⮚If there is a tie in frequencies use FIFO for replacement
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU
0 2 0 1 0 3 2 1 2 3

Counter
value of Page 0 : 0 Page 1 : 0 Page 2 : 0 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU

0 2 0 1 0 3 2 1 2 3
0

Counter
value of Page 0 : 1 Page 1 : 0 Page 2 : 0 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU

0 2 0 1 0 3 2 1 2 3
0 0
2

F F

Counter
value of Page 0 : 1 Page 1 : 0 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU

0 2 0 1 0 3 2 1 2 3
0 0 0
2 2

F F H

Counter
value of Page 0 : 2 Page 1 : 0 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU

0 2 0 1 0 3 2 1 2 3
0 0 0 0
2 2 2
1

F F H F

Counter
value of Page 0 : 2 Page 1 : 1 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU

0 2 0 1 0 3 2 1 2 3
0 0 0 0 0
2 2 2 2
1 1

F F H F H

Counter
value of Page 0 : 3 Page 1 : 1 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 0
2 2 2 2 3
1 1 1

F F H F H F

Counter
value of Page 0 : 3 Page 1 : 1 Page 2 : 0 Page 3 : 1
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 0 0
2 2 2 2 3 3
1 1 1 2

F F H F H F F

Counter
value of Page 0 : 3 Page 1 : 0 Page 2 : 1 Page 3 : 1
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 0 0 0
2 2 2 2 3 3 1
1 1 1 2 2

F F H F H F F F

Counter
value of Page 0 : 3 Page 1 : 1 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 0 0 0 0
2 2 2 2 3 3 1 1
1 1 1 2 2 2

F F H F H F F F H

Counter
value of Page 0 : 3 Page 1 : 1 Page 2 : 2 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform LFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 0 0 0 0 0
2 2 2 2 3 3 1 1 3
1 1 1 2 2 2 2

F F H F H F F F H F

Counter
value of Page 0 : 3 Page 1 : 0 Page 2 : 2 Page 3 : 1
each page

• Number of page hits – 3


• Number of page faults – 7
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU

0 2 0 1 0 3 2 1 2 3

Counter
value of Page 0 : 0 Page 1 : 0 Page 2 : 0 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU

0 2 0 1 0 3 2 1 2 3
0

Counter
value of Page 0 : 1 Page 1 : 0 Page 2 : 0 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU

0 2 0 1 0 3 2 1 2 3
0 0
2

F F

Counter
value of Page 0 : 1 Page 1 : 0 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0
2 2

F F H

Counter
value of Page 0 : 2 Page 1 : 0 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0
2 2 2
1

F F H F

Counter
value of Page 0 : 2 Page 1 : 1 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0
2 2 2 2
1 1

F F H F H

Counter
value of Page 0 : 3 Page 1 : 1 Page 2 : 1 Page 3 : 0
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 3
2 2 2 2 2
1 1 1

F F H F H F

Counter
value of Page 0 : 0 Page 1 : 1 Page 2 : 1 Page 3 : 1
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 3 3
2 2 2 2 2 2
1 1 1 1

F F H F H F H

Counter
value of Page 0 : 0 Page 1 : 1 Page 2 : 2 Page 3 : 1
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 3 3 3
2 2 2 2 2 2 2
1 1 1 1 1

F F H F H F H H

Counter
value of Page 0 : 0 Page 1 : 2 Page 2 : 2 Page 3 : 1
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 3 3 3 3
2 2 2 2 2 2 2 2
1 1 1 1 1 1

F F H F H F H H H

Counter
value of Page 0 : 0 Page 1 : 2 Page 2 : 3 Page 3 : 1
each page
Reference String – 0, 2, 0, 1, 0, 3, 2, 1, 2, 3
Total Frames allocated – 3
Perform MFU
0 2 0 1 0 3 2 1 2 3
0 0 0 0 0 3 3 3 3 3
2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1

F F H F H F H H H H

Counter
value of Page 0 : 0 Page 1 : 2 Page 2 : 3 Page 3 : 2
each page

• Number of page hits – 6


• Number of page faults – 4
Page-Buffering Algorithms
⮚Used in addition to page replacement algorithms
⮚System keeps a pool of free frames
⮚When a page fault occurs
⮚Victim frame is chosen
- but requires some time to choose the victim frame
⮚At that time desired page is read into a free frame which
is maintained by the system
- By doing this the desired page can start doing its
work without waiting for the swapping
process of the victim frame in the main memory
⮚Victim frame is written out later
⮚Finally Victim frame is added to the pool of free frames
Page-Buffering Algorithms
⮚Another idea is to maintain a list of modified pages
⮚Whenever paging device is idle, a modified page is selected and
written to the disk
⮚When there is read/write operation at that time paging device does
this job
⮚Modify bit is then reset
⮚Increases the probability that a page is clean when chosen for
replacement
⮚Used to identify the clean pages very quickly, because after
performing this operation maximum modified pages will be
already written to the disk
⮚Makes the replacement very easy
Page-Buffering Algorithms
⮚Yet Another idea is to keep a pool of free frames
⮚Remember which page was in each frame
⮚If the same page is needed again, it is taken from the pool of free
frames and used
⮚But the page should not be modified for reusing
⮚No I/O operation is required
⮚If the required page is not in the pool of free frames, another free
frame is selected
⮚The new page is read into free frame
Allocation of Frames
⮚How do we allocate the fixed amount of free memory among
the various process?
⮚Example: If we have 93 free frames and 2 processes, How
many frames does each process get?
⮚Consider a single-user system with 128 KB of memory
composed of pages 1 KB in size.
⮚This system has 128 frames.
⮚The operating system may take 35 KB of memory and
⮚Leaving remaining 93 frames for the user process.
⮚Other possible strategy: the user process is allocated with any
free frame.
Demand paging Vs. Pure Demand paging
Demand paging Pure Demand paging
⮚ In demand paging, a page is not loaded into main ⮚ In pure demand paging, even a single page is
memory until it is needed. not loaded into memory initially. Hence pure
demand paging causes a page fault. Page
fault, the situation in which the page is not
available whenever a processor needs to
execute it.

⮚ while in pure demand paging swapping,


⮚ In Demand paging follows that pages should only where all memory for a process is swapped
be brought into memory if the executing process from secondary storage to main memory
demands them. during the process startup.
Allocation of Frames – Pure Demand Paging

Under Pure demand paging:


⮚All 93 frames - initially kept on free-frame list.
⮚When a user process started execution, it would generate a
sequence of page faults.
⮚There is not page fault up to 93 frames.
⮚When the free-frame list was exhausted then a page-
replacement algorithm would be called to select one of the 93
in-memory pages to be replaced with the 94th, and so on.
⮚When the process terminated, the 93 frames would once again
be placed on the free-frame list.
Allocation of Frames
⮚Each process needs minimum number of frames
⮚Allocating minimum number of frames impacts on the performance.
⮚If the number of frames allocated to each process decreases, the
page-fault rate increases, which slow downs the execution of process
⮚Also when a page fault occurs before completion of an executing
then the instruction must be restarted.
⮚It is necessary to hold all the different pages that any single instruction
can reference.
⮚Example: IBM 370 – 6 pages to handle storage to storage (SS) MOVE
instruction:
⮚instruction is 6 bytes, might span 2 pages
⮚2 pages to handle from one storage area
⮚2 pages to handle to another storage area
Allocation schemes
⮚Minimum number of frames per process is defined by the
architecture.
⮚Maximum number is defined by the amount of available
physical memory in the system
⮚In between is still left with significant choice of frame
allocation.
⮚Two major allocation schemes
⮚Fixed allocation
⮚Equal Allocation
⮚Proportional Allocation
⮚Priority allocation
Fixed Allocation

⮚Equal allocation – For example, if there are 100 frames (after allocating
frames for the OS) and 5 processes, give each process 20 frames
⮚Keep some as free frame buffer pool

⮚Proportional allocation – Allocate according to the size of process


⮚Dynamic as degree of multiprogramming, process sizes change

Consider a system with a 1-KB frame size. If a


small student process of 10 KB and an
interactive database of 127 KB are the only two
processes running in a system with 62 free
frames, it does not make much sense to give
each process 31 frames. The student process
does not need more than 10 frames, so the other
21 are, strictly speaking, wasted
Fixed Allocation – Contd.

⮚In equal and proportional allocation

⮚If the multiprogramming level is increased, each process


will lose some frames for the new process.
⮚if the multiprogramming level decreases, the frames that
were allocated from departed process are spread to the
remaining processes.
Priority Allocation

⮚Suppose if we want to give the high-priority process with


more memory to speed up its execution than the low-priority
processes.

⮚Use a proportional allocation scheme using priorities


rather than size

⮚If process Pi generates a page fault,

⮚select for replacement one of its frames

⮚select for replacement a frame from a process with lower


priority number
Frame Allocation - Another factor

⮚Multiple processes competing for frames, we can classify page-


replacement algorithms into two broad categories namely
Global and Local.

Global replacement Local replacement


⮚Process selects a ⮚Each process selects from only
replacement frame from its own set of allocated frames
the set of all frames; one ⮚ More consistent per-process
performance
process can take a frame
⮚ But possibly underutilized
from another memory
⮚But then process execution
time can vary greatly
⮚But greater throughput so
more common
Global vs. Local Allocation – Contd.

⮚If, we allow high-priority processes to select frames


from low-priority processes for replacement
⮚Global replacement, approach allows a high-priority
process to increase its frame allocation at the expense of
a low-priority process
⮚Local replacement, the number of frames allocated to a
process does not change.
Non-Uniform Memory Access (NUMA)
⮚So far all memory accessed equally
⮚Many systems are NUMA – speed of access to memory varies
⮚Consider system boards containing CPUs and memory, interconnected
over a system bus
⮚Optimal performance comes from allocating memory “close
to” the CPU on which the thread is scheduled
⮚And modifying the scheduler to schedule the thread on the
same system board when possible
⮚Solved by Solaris by creating lgroups
⮚ Structure to track CPU / Memory low latency groups
⮚ Used my schedule and pager
⮚ When possible schedule all threads of a process and allocate all memory
for that process within the lgroup
Root cause of the Thrashing

⮚Thrashing is caused by under allocation of the minimum number of pages


required by a process, forcing it to continuously page fault.

⮚The system can detect thrashing by evaluating the level of CPU utilization as
compared to the level of multiprogramming.

⮚It can be eliminated by reducing the level of multiprogramming.


Thrashing

⮚A process is busy with swapping pages in and out.


This high paging activity is called Thrashing.
⮚A process is in thrashing if it is spending more time
on paging rather than executing.
Cause of Thrashing
If a process does not have “enough”
pages, the page-fault rate is very high.
This leads to:
⮚ low CPU utilization.
⮚ operating system thinks that it needs to increase
the degree of multiprogramming.
⮚ another process added to the system.
Demand Paging and Thrashing

⮚Why does demand paging work?


⮚To prevent thrashing, we must provide a process with as
many frames as it needs.

⮚But how do we know how many frames it “needs”?

⮚Locality model - working-set strategy starts by looking at how


many frames a process is actually using. This approach defines
the locality model of process execution
Demand Paging and Thrashing - Contd

⮚Process migrates from one locality to


another (Figure)
⮚Localities may overlap
⮚Example:
⮚ Function is called, that defines a new
locality.
⮚ In this, memory references are made to
the instructions of the function call, its
local variables, and a subset of the global
variables. When we exit the function, the
process leaves this locality, since the local
variables and instructions of the function
are no longer in active use.
Demand Paging and Thrashing – Cont.
⮚Why does thrashing occur?
⮚Σ size of locality > total memory size
⮚Limit effects by using local or priority page replacement
⮚Local replacement algorithms - if one process starts thrashing
it cannot steal frames from another process and cause
thrashing latter.
⮚However if processes are thrashing, they will be in the
queue for the paging device most of the time.
⮚The average service time for a page fault will increase,
due to the longer queue for the paging device.
⮚Thus the effective access time will increase even for a
process that is not thrashing.
Models to Avoid Thrashing

⮚There are 2 Models to Avoid Thrashing


⮚Working – set Model
⮚Works based on Locality of Reference

⮚Page Fault Frequency Model

⮚Based on Upper and Lower Page Fault Rate


Working Set Model
⮚It is based on the assumption of locality.
⮚Δ ≡ working-set window ≡ a fixed number of page references
Example: 10,000 instruction
⮚The idea is to examine the most recent Δ page references.
⮚WSSi (working set of Process Pi) = total number of pages referenced in
the most recent Δ (varies in time)
⮚if Δ too small will not encompass entire locality.
⮚if Δ too large will encompass several localities.
⮚if Δ = ∞ ⇒ will encompass entire program.
⮚D = Σ WSSi ≡ total demand frames
⮚if D > m ⇒ Thrashing
⮚Policy if D > m, then suspend one of the processes.
⮚if D <= m ⇒ No Thrashing
Working Set Model – Contd.

⮚The sequence of memory references shown in Figure,


⮚if Δ = 10 memory references,
⮚Then the working set at time
⮚t is {1, 2, 5, 6, 7}
1

⮚t , the working set has changed to {3, 4}.


2

The working set model states that a process can be in RAM if and only if all
of the pages that it is currently using (often approximated by the most recently
used pages) can be in RAM.
Keeping Track of the Working Set

⮚The working set prevents thrashing while keeping the degree of


multiprogramming as high as possible, in order to optimize the CPU
utilization.
⮚Difficulty: keeping track of the working set
Approximate with interval timer + a reference bit
⮚Example: Δ = 10,000
⮚Timer interrupts after every 5000 time units.
⮚Keep in memory 2 bits for each page.
⮚Whenever a timer interrupts copy and sets the values of all reference
bits to 0.
⮚If one of the bits in memory = 1 ⇒ page in working set.
⮚Why is this not completely accurate?
⮚Improvement = 10 bits and interrupt every 1000 time units.
Page-Fault Frequency
⮚More direct approach than Working Set model.
⮚Where working set seems a clumsy way to control thrashing.
⮚If page fault rate is too high, then the process need more frames.
⮚Whereas page fault is too low, then the process may have too many frames.

⮚ Establish “acceptable” page-fault


frequency (PFF) rate and use local
replacement policy
⮚ If actual rate too low, process loses
frame
⮚ If actual rate too high, process
gains frame
Page-Fault Frequency – Contd.

⮚ As with the working-set strategy, we may have to


swap out a process.
⮚ If the page-fault rate increases and no free frames
are available, we must select some process and
swap it out to backing store.
⮚ The freed frames are then distributed to processes
with high page-fault rates
STORAGE MANAGEMENT AND FILE MANAGEMENT
Mass-Storage Systems
• Overview of Mass Storage Structure
– Hard disk
– Magnetic tape
– Storage Array
– Storage Area Networks
– Network Attached Storage
Objectives
• To describe the physical structure of secondary storage devices
and its effects on the uses of the devices
• To explain the performance characteristics of mass-storage
devices
• To evaluate disk scheduling algorithms
Overview of Mass Storage Structure
• Magnetic disks provide bulk of secondary storage of modern
computers
– Drives rotate at 60 to 250 times per second
– Transfer rate is rate at which data flow between drive and computer
– Positioning time (random-access time) is time to move disk arm to desired
cylinder (seek time) and time for desired sector to rotate under the disk head
(rotational latency)
– Head crash results from disk head making contact with the disk surface -- That’s
bad
• Disks can be removable
• Drive attached to computer via I/O bus
– Busses vary, including EIDE, ATA, SATA, USB, Fibre Channel, SCSI, SAS, Firewire
– Host controller in computer uses bus to talk to disk controller built into drive or
storage array
Moving-head Disk Mechanism
Hard Disks
• Platters range from .85” to 14” (historically)
– Commonly 3.5”, 2.5”, and 1.8”

• Range from 30GB to 3TB per drive


• Performance
– Transfer Rate – theoretical – 6 Gb/sec
– Effective Transfer Rate – real – 1Gb/sec
– Seek time from 3ms to 12ms – 9ms common for desktop drives
– Average seek time measured or calculated based on 1/3 of tracks
– Latency based on spindle speed
– 1 / (RPM / 60) = 60 / RPM
– Average latency = ½ latency
Hard Disks
• Platters range from .85” to 14” (historically)
– Commonly 3.5”, 2.5”, and 1.8”

• Range from 30GB to 3TB per drive


• Performance
– Transfer Rate – theoretical – 6 Gb/sec
– Effective Transfer Rate – real – 1Gb/sec
– Seek time from 3ms to 12ms – 9ms common for
desktop drives
– Average seek time measured or calculated based
on 1/3 of tracks
– Latency based on spindle speed
– 1 / (RPM / 60) = 60 / RPM
– Average latency = ½ latency
Terms to be considered for calculating Hard Disk Performance

1. 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.
2. 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.
3. 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.
4. Disk Access Time: Disk Access = Seek time + Rotational latency + Transfer
time
Hard Disk Performance(contd…)
• To transfer a 4KB block on a 7200 RPM disk with a 5ms average seek time, 1Gb/sec transfer rate with a .1ms
controller overhead calculate average I/O time or Disk access time of 4KB block.

Disk Access time = Seek time + Rotational latency + Transfer time


The First Commercial Disk Drive

• 1956

• IBM RAMDAC computer


included the IBM Model 350
disk storage system
• 5M (7 bit) characters

• 50 x 24” platters

• Access time = < 1 second


Solid-State Disks
• Nonvolatile memory used like a hard drive
– Many technology variations

• Can be more reliable than HDDs


• More expensive per MB
• Maybe have shorter life span
• Less capacity
• But much faster
• Busses can be too slow -> connect directly to PCI for example
• No moving parts, so no seek time or rotational latency
Magnetic Tape
• Was early secondary-storage medium
– Evolved from open spools to cartridges

• Relatively permanent and holds large quantities of data

• Access time slow

• Random access ~1000 times slower than disk

• Mainly used for backup, storage of infrequently-used data, transfer medium between systems

• Kept in spool and wound or rewound past read-write head

• Once data under head, transfer rates comparable to disk


– 140MB/sec and greater

• 200GB to 1.5TB typical storage

• Common technologies are LTO-{3,4,5} and T10000


Disk Structure
• Disk drives are addressed as large 1-dimensional arrays of logical
blocks, where the logical block is the smallest unit of transfer
– Low-level formatting creates logical blocks on physical media
• The 1-dimensional array of logical blocks is mapped into the sectors of
the disk sequentially
– Sector 0 is the first sector of the first track on the outermost cylinder
– Mapping proceeds in order through that track, then the rest of the tracks in
that cylinder, and then through the rest of the cylinders from outermost to
innermost
– Logical to physical address should be easy
– Except for bad sectors
– Non-constant # of sectors per track via constant angular velocity
Disk Attachment
• Host-attached storage accessed through I/O ports talking to I/O busses

• SCSI itself is a bus, up to 16 devices on one cable, SCSI initiator


requests operation and SCSI targets perform tasks
– Each target can have up to 8 logical units (disks attached to device controller)

• FC is high-speed serial architecture


– Can be switched fabric with 24-bit address space – the basis of storage area
networks (SANs) in which many hosts attach to many storage units

• I/O directed to bus ID, device ID, logical unit (LUN)


Storage Array
• Can just attach disks, or arrays of disks

• Storage Array has controller(s), provides features to attached host(s)


– Ports to connect hosts to array
– Memory, controlling software (sometimes NVRAM, etc)
– A few to thousands of disks
– RAID, hot spares, hot swap (discussed later)
– Shared storage -> more efficiency
– Features found in some file systems
– Snaphots, clones, thin provisioning, replication, deduplication, etc
Storage Area Network
• Common in large storage environments

• Multiple hosts attached to multiple storage arrays - flexible


Storage Area Network (Cont.)
• SAN is one or more storage arrays
– Connected to one or more Fibre
Channel switches

• Hosts also attach to the switches


• Storage made available via LUN
Masking from specific arrays to
specific servers
• Easy to add or remove storage, add
new host and allocate it storage
– Over low-latency Fibre Channel fabric
Network-Attached Storage
• Network-attached storage (NAS) is storage
made available over a network rather than
over a local connection (such as a bus)
– Remotely attaching to file systems

• NFS and CIFS are common protocols


• Implemented via remote procedure calls
(RPCs) between host and storage over
typically TCP or UDP on IP network
• iSCSI protocol uses IP network to carry the
SCSI protocol
– Remotely attaching to devices (blocks)
Difference between SAN Vs NAS
Disk Scheduling
– Introduction to Disk Scheduling
– Why Disk Scheduling is Important
– Disk Scheduling Algorithms
– FCFS
– SSTF
– SCAN
– C-SCAN
– LOOK
– C-LOOK
- Disk Management
Disk Scheduling
What is Disk Scheduling?
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.
• The operating system is responsible for using hardware efficiently — for the disk drives, this means having
a fast access time and disk bandwidth
• Minimize seek time
• Seek time ≈ seek distance
• Disk bandwidth is the total number of bytes transferred, divided by the total time between the first
request for service and the completion of the last transfer
• There are many sources of disk I/O request
– OS
– System processes
– Users processes
• I/O request includes input or output mode, disk address, memory address, number of sectors to transfer
• OS maintains queue of requests, per disk or device
• Idle disk can immediately work on I/O request, busy disk means work must queue
– Optimization algorithms only make sense when a queue exists
Why Disk Scheduling is Important?
• 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.
Disk Scheduling (Cont.)
• Note that drive controllers have small buffers and can manage
a queue of I/O requests (of varying “depth”)
• Several algorithms exist to schedule the servicing of disk I/O
requests
• The analysis is true for one or many platters
Disk Scheduling Algorithms
– Disk Scheduling Algorithms
– FCFS
– SSTF
– SCAN
– C-SCAN
– LOOK
– C-LOOK
Disk Scheduling Algorithm - Example
• Consider a disk queue with requests for I/O to blocks on
cylinders 98, 183, 41, 122, 14, 124, 65, 67. The
FCFS,SSTF,SCAN,C-SCAN,LOOK, and C-LOOK scheduling
algorithm is used. The head is initially at cylinder number 53.
The cylinders are numbered from 0 to 199. The total head
movement (in number of cylinders) incurred while servicing
these requests is _______.
1. FCFS(First Come First Serve)

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

• head is initially at cylinder number 53. The cylinders are numbered from 0 to 199.

• 98, 183, 41, 122, 14, 124, 65, 67

1. (98-53)=45
2. (183-98)=85
3. (183-41)=142

4. (122-41)=81

5. 122-14=108

6. 124-14-110

7. 124-65=59

8.67-65=2
FCFS(contd..)
• Total head movements using FCFS= [(98 – 53) + (183 – 98) + (183 – 41) + (122 – 41) +
(122 – 14) + (124 – 14) + (124 – 65) + (67 – 65)]
• = 45 + 85 + 142 + 81 + 108 + 110 + 59 + 2
• =632
Advantages:
• Every request gets a fair chance
• No indefinite postponement
Disadvantages:
• Does not try to optimize seek time
• May not provide the best possible service
2. SSTF(Shortest Seek Time First)
• Shortest Seek Time First (SSTF) selects the request with the minimum seek time from the current
head position

• SSTF scheduling is a form of SJF scheduling; may cause starvation of some requests

• As a result, the request near the disk arm will get executed first. SSTF is certainly an improvement
over FCFS as it decreases the average response time and increases the throughput of system.

Advantages:

• Average Response Time decreases

• Throughput increases

Disadvantages:

• Overhead to calculate seek time in advance

• Can cause Starvation for a request if it has higher seek time as compared to incoming requests

• High variance of response time as SSTF favours only some requests


SSTF(contd…)
• head is initially at cylinder number 53. The cylinders are numbered from 0 to
199.
• 98, 183, 41, 122, 14, 124, 65, 67

Total head movement using SSTF=(65-53)+(67-65)+(67-41)+(41-14)+(98-14)+ (122-98)+(124-122)+(183-124)


=12+2+26+27+84+24+2+59=236)
3. SCAN
• The disk arm starts at one end of the disk, and moves toward the other end,
servicing requests until it gets to the other end of the disk, where the head
movement is reversed and servicing continues.
• SCAN algorithm Sometimes called the elevator algorithm
• As a result, the requests at the midrange are serviced more and those arriving
behind the disk arm will have to wait.
Advantages:
• High throughput
• Low variance of response time
• Average response time
Disadvantages:
• Long waiting time for requests for locations just visited by disk arm
SCAN or Elevator((1st Solution)
• head is initially at cylinder number 53. The cylinders are numbered from 0
to 199.
• 98, 183, 41, 122, 14, 124, 65, 67

Total Head Movement using SCAN=[(65-53)+(67-65)+(98-67)+(122-98)+(124-


122)+(183-124)+(199-183)+(199-41)+
(41-14)]
=12+2+31+24+2+59+16+158+27)=331
SCAN or Elevator(2nd Solution)- Best Choice
• head is initially at cylinder number 53. The cylinders are numbered from 0 to 199.
• 98, 183, 41, 122, 14, 124, 65, 67

Total Head Movement using SCAN=[(53-41)+(41-14)+(14-0)+(65-0)+(67-65)+


(98-67)+(122-98)+(124-122)+(183-124)]
=12+27+14+65+2+31+24+59
= 234
4. C-SCAN
• The head moves from one end of the disk to the other, servicing
requests as it goes
– When it reaches the other end, however, it immediately returns to the
beginning of the disk, without servicing any requests on the return trip

• Treats the cylinders as a circular list that wraps around from the last
cylinder to the first one
• Provides a more uniform wait time than SCAN

Advantages:
• Provides more uniform wait time compared to SCAN
C-SCAN(contd…)
• head is initially at cylinder number 53. The cylinders are numbered from 0 to 199.
• 98, 183, 41, 122, 14, 124, 65, 67

Total Head Movement using C-SCAN=[(65-53)+(67-65)+(98-67)+(122-98)+(124-122)+(183-124)+(199-


183)+(199-0)+(14-0)+(41-14)]
=12+2+31+24+2+59+16+199+14+27)=386
5. LOOK
• It is similar to the SCAN disk scheduling algorithm except for the
difference that the disk arm in spite of going to the end of the
disk goes only to the last request to be serviced in front of the
head and then reverses its direction from there only. Thus it
prevents the extra delay which occurred due to unnecessary
traversal to the end of the disk.
LOOK(Contd…)
• head is initially at cylinder number 53. The cylinders are numbered from 0 to 199.
• 98, 183, 41, 122, 14, 124, 65, 67

Total Head Movement using look=[(65-53)+(67-65)+(98-67)+(122-98)+(124-


122)+ (183-124)+(183-41)+(41-14)]
=12+2+31+24+2+59+142+27)=299
6. C-LOOK
• LOOK a version of SCAN, C-LOOK a version of C-SCAN

• Arm only goes as far as the last request in each direction, then
reverses direction immediately, without first going all the way to
the end of the disk
• In CLOOK, the disk arm in spite of going to the end goes only to
the last request to be serviced in front of the head and then from
there goes to the other end’s last request. Thus, it also prevents
the extra delay which occurred due to unnecessary traversal to
the end of the disk.
C- LOOK(contd…)
• head is initially at cylinder number 53. The cylinders are numbered from 0 to 199.
• 98, 183, 41, 122, 14, 124, 65, 67

Total Head Movement using C-LOOK=[(65-53)+(67-65)+(98-67)+(122-


98)+(124-122)+(183-124)+(183-14)+
(41-14)]
= 12+2+31+24+2+59+169+27)=326
No.of Head movements of All Algorithms
• Total head movements

• 1.FCFS=632

• 2.SSTF=232

• 3.SCAN=331or 234

• 4. C-SCAN=386

• 5.LOOK=299

• 6.C-SCAN=326
Selection of a Disk-Scheduling Algorithm
• SSTF is common and has a natural appeal

• SCAN and C-SCAN perform better for systems that place a heavy load on the disk
– Less starvation

• Performance depends on the number and types of requests

• Requests for disk service can be influenced by the file-allocation method


– And metadata layout

• The disk-scheduling algorithm should be written as a separate module of the operating system, allowing it to be
replaced with a different algorithm if necessary

• Either SSTF or LOOK is a reasonable choice for the default algorithm

• What about rotational latency?


– Difficult for OS to calculate

• How does disk-based queueing effect OS queue ordering efforts?


Disk Management
• Low-level formatting, or physical formatting — Dividing a disk into sectors that
the disk controller can read and write
– Each sector can hold header information, plus data, plus error correction
code (ECC)
– Usually 512 bytes of data but can be selectable

• To use a disk to hold files, the operating system still needs to record its own data
structures on the disk
– Partition the disk into one or more groups of cylinders, each treated as a
logical disk
– Logical formatting or “making a file system”
– To increase efficiency most file systems group blocks into clusters
– Disk I/O done in blocks
– File I/O done in clusters
Disk Management (Cont.)
• Raw disk access for apps that want to do their own block management, keep OS out of
the way (databases for example)

• Boot block initializes system


– The bootstrap is stored in ROM
– Bootstrap loader program stored in boot blocks of boot partition

• Methods such as sector sparing used to handle bad blocks


File System Interface
• File Concept

• File Access Methods

• File Sharing and Protection


Objective of File Interface
• To explain the function of file systems

• To describe the interfaces to file systems

• To discuss file-system design tradeoffs, including access methods,


file sharing, file locking, and structures
File Concept
File Concept
• 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 the most important mechanism for storing data permanently on mass-storage
devices. Permanently means that the data is not lost when the machine is switched off. Files
can contain:
• data in a format that can be interpreted by programs, but not easily by humans (binary files);
• alphanumeric characters, codified in a standard way (e.g., using ASCII or Unicode), and
directly readable by a human user (text files). Text files are normally organized in a sequence
of lines, each containing a sequence of characters and ending with a special character
(usually the newline character). Consider, for example, a Java program stored in a file on the
hard-disk. In this unit we will deal only with text files.
• Each file is characterized by a name and a directory in which the file is placed (one may
consider the whole path that allows one to find the file on the hard-disk as part of the name
of the file).
File Attributes
• Name: It is the only information stored in a human-readable form.
• Identifier: Every file is identified by a unique tag number within a file
system known as an identifier.
• Location: Points to file location on device.
• Type: This attribute is required for systems that support various types
of files.
• Size. Attribute used to display the current file size.
• Protection. This attribute assigns and controls the access rights of
reading, writing, and executing the file.
• Time, date and security: It is used for protection, security, and also
used for monitoring
File Operations
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
• Writing the file is different from creating the file. The OS maintains a write pointer for every file which points to the position
in the file from which, the data needs to be written.
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.
File Types – Names, Extension
File Structure

• 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 Structure
File Structure
classification

Simple Record Complex Structure –


None- Structure – Lines, Formatted
Sequence of Fixed and Document,
words,bytes Variable Length Relocatable load file
File Access Methods
File Access
Methods

Sequential Direct Hashed


File Access Methods
1. Sequential Access Method: – Sequential Access Method is one of the
simplest file access methods. Most of the OS (operating system) uses a
sequential access method to access the file. In this method, word by word,
the operating system read the file, and we have a pointer that points the file’s
base address. If we need to read the file’s first word, then there is a pointer
that offers the word which we want to read and increment the value of the
word by 1, and till the end of the file, this process will continue.
• The Modern world system offers the facility of index access and direct access
to file. But the sequential access method is one of the most used methods
because more files like text files, audio files, and the video files require to be
sequentially accessed.
• Key Points:
1. Sequential Access Method is reasonable for tape.
2. In the sequential access method, if we use read command, then the pointer is
moved ahead by 1.
3. If the write command is used, then the memory will be allocated, and at the
end of the file, a pointer will be moved.
File Access Methods
2. Direct Access Method: – Direct Access Method is also called the
Relative access method. In the Database System, we mostly use the
Direct Access Method. In most of the situations, there is a need for
information in the filtered form from the database. And in that case,
the speed of sequential access may be low and not efficient.
• Let us assume that each storage block stores 4 records, and we
already know that the block which we require is stored in the
10th block. In this situation, the sequential access method is not
suitable. Since if we use this, then to access the record which is
needed, this method will traverse all the blocks.
• So, in this situation, the Direct access method gives a more
satisfying result, else the OS (operating system) needs to perform
a few complex jobs like defining the block number, which is
required. It is mostly implemented in the application of the
database.
File Access Methods
3. Index Access Method: – Index Access Method is another essential
method of file, accessing. In this method, for a file an index is created, and
the index is just like an index which is at the back of the book. The index
includes the pointer to the different blocks. If we want to find a record in
the file, then first, we search in the index, and after that, with the help of
the pointer, we can directly access the file.
• In the Index Access method, we can search fast in the large database,
and also easy. But in this, the method need some additional space to
store the value of the index in the memory.
• Key Points:
1. On top of the sequential access method, the index access method is
built.
2. The Index access method can control the pointer with the help of the
index.
File Sharing
Objective of File sharing
• For multiple users-File sharing, file naming and file protection is a challenging
and important task.
• In order to handle this, file access control and protection need to be done.
• If a user wants to share a file with other user, proper protection schemes
need to be followed.
• Whenever a file is shared for various users on distributed systems , it must be
shared across distributed systems.
• In order to share and distribute files, Network File System (NFS) is used.
• For single user system ,the system need to maintain many files and directory
attributes.
File sharing-Multiple Users
• Many file systems evolved with the concept of file (or directory) owner
(or user) and group.
• The owner is the user who can change attributes and grant access and
who has the most control over the file.
• The group attribute defines a subset of users who can share access to
the file.
• For example, the owner of a file on a UNIX system can issue all
operations on a file, while members of the file’s group can execute one
subset of those operations, and all other users can execute another
subset of operations. Exactly which operations can be executed by
group members and other users is definable by the file’s owner
File sharing-Remote File Systems
• The communication among remote computers uses the concept called Networking
that allows the sharing of resources spread across a campus or even around the
world.
• The first implemented method for file sharing involves manually transferring files
between machines via programs like ftp.
• The second major method for file sharing uses a distributed file system (DFS) in
which remote directories are visible from a local machine.
• The third method for file sharing , uses WorldWide Web, is a reversion to the first.
• A browser is needed to gain access to the remote files, and separate operations
(essentially a wrapper for ftp) are used to transfer files.
• Increasingly, cloud computing is being used for file sharing as well.
File sharing-Remote File Systems
• ftp is used for both anonymous and authenticated access.

• Anonymous access allows a user to transfer files without having


an account on the remote system.
• TheWorldWideWeb uses anonymous file exchange almost
exclusively.
• DFS involves a much tighter integration between the machine that
is accessing the remote files and the machine providing the files.
File sharing-Remote File Systems
Remote File
Systems

Client –Server Distributed


Information Failure Modes
Model
Systems
File sharing-Remote File Systems-
Client Server Model
• The machine containing the files is the server, and the machine
seeking access to the files is the client.
• The server can serve multiple clients, and a client can use multiple
servers, depending on the implementation details of a given
client–server facility.
• Example:
– NFS is standard UNIX client-server file sharing protocol
– CIFS is standard Windows protocol
– Standard operating system file calls are translated into remote calls
File sharing -Distributed Information Systems
• Distributed Information Systems (distributed naming services) such as
LDAP, DNS, NIS, Active Directory implement unified access to
information needed for remote computing
• Examples:
• Other distributed information systems provide user
name/password/user ID/group ID space for a distributed facility.
• UNIX systems have employed a wide variety of distributed information
methods.
• Sun Microsystems introduced yellow pages (since renamed network
information service, or NIS), and most of the industry adopted its use.
File Sharing -Failure Modes
• All file systems have failure modes
– For example corruption of directory structures or other non-user data,
called metadata
• Remote file systems add new failure modes, due to network
failure, server failure
• Recovery from failure can involve state information about status
of each remote request
• Stateless protocols such as NFS v3 include all information in each
request, allowing easy recovery but less security
File Sharing-Consistency Semantics
• Consistency semantics represent an important criterion for evaluating
any file system that supports file sharing.
• These semantics specify how multiple users of a system are to access a
shared file simultaneously.
• In particular, they specify when modifications of data by one user will
be observable by other users.
• These semantics are typically implemented as code with the file
system.
• Consistency semantics are directly related to the process
synchronization algorithms
File sharing - Consistency Semantics
Consistency
Semantics

Session Semantics Immutable Shared


UNIX-Semantics
File Semantics
File Sharing-Consistency Semantics-
Unix Semantics
• The UNIX file system uses the following consistency semantics:
– Writes to an open file by a user are visible immediately to other users
who have this file open.
– One mode of sharing allows users to share the pointer of current location
into the file. Thus, the advancing of the pointer by one user affects all
sharing users. Here, a file has a single image that interleaves all accesses,
regardless of their origin.
File Sharing-Consistency Semantics-
Session Semantics
• The Andrew file system (OpenAFS) uses the following consistency
semantics:
– Writes to an open file by a user are not visible immediately to other users
that have the same file open.
– Once a file is closed, the changes made to it are visible only in sessions
starting later. Already open instances of the file do not reflect these
changes.

• According to these semantics, a file may be associated


temporarily with several (possibly different) images at the same
time.
File Sharing-Consistency Semantics-Immutable-
Shared-Files Semantics
• A unique approach is that of immutable shared files.

• Once a file is declared as shared by its creator, it cannot be


modified. An immutable file has two key properties: its name may
not be reused, and its contents may not be altered.
• Thus, the name of an immutable file signifies that the contents of
the file are fixed.
• The implementation of these semantics in a distributed system
File Protection-Objective
• When information is stored in a computer system, we want to keep it safe from
physical damage (the issue of reliability) and improper access (the issue of
protection).
• 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
Protection
• Protection can be provided in many ways. Protection mechanisms
provide controlled access by limiting the types of file access that can be
made.
– 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.
Protection-Access Control
• The most general scheme to implement identity dependent
access is to associate with each file and directory an access-
control list (ACL) specifying user names and the types of access
allowed for each user.
• When a user requests access to a particular file, the operating
system checks the access list associated with that file.
• If that user is listed for the requested access, the access is
allowed. Otherwise, a protection violation occurs, and the user
job is denied access to the file.
Protection-Access Control
• Mode of access: read(4 bits), write(2 bits), execute(1 bit)

• Three classes of users on Unix / Linux(Default Permission)


RWX
a) owner access 7 ⇒ 111
RWX
b) group access 6 ⇒ 110
RWX
c) public access 1 ⇒ 001
To change Permissions the following commands are used :
• Chmod: Change Mode(For files)
• Chown: Change Owner(For Files)
• Chgroup: Change group permission(For files)
Protection-Access Control-Control List
Management
• Example:
Windows users typically manage access-
control lists via the GUI. Figure shows a file-
permission window on Windows 7 NTFS file
system. In this example, user “guest” is
specifically denied access to the file
ListPanel.java
Unix File Permission Example
• While using ls -l command, it displays various information related to file permission as
follows :

• The first column represents different access modes, i.e., the permission associated with a file
or a directory.
• The permissions are broken into groups of threes, and each position in the group denotes a
specific permission, in this order: read (r), write (w), execute (x) −
• The first three characters (2-4) represent the permissions for the file's owner. For example, -
rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.
• The second group of three characters (5-7) consists of the permissions for the group to
which the file belongs. For example, -rwxr-xr-- represents that the group has read (r) and
execute (x) permission, but no write permission.
• The last group of three characters (8-10) represents the permissions for everyone else. For
example, -rwxr-xr-- represents that there is read (r) only permission.
File System Implementation
File System Implementation
- File System Structure
- Directory Implementation
- File Allocation Methods
- Swap Space Management
Objectives of File System Implementation
• It helps to understand the file system structure

• It helps to apprehend about how the files and folders are


organized in the directory structure and its classification
• To describe the implementation of remote file systems

• To discuss block allocation and free-block algorithms and trade-


offs
• To describe the details of implementing local file systems and
directory structures
File-System Structure
• File structure
– Logical storage unit
– Collection of related information

• File system resides on secondary storage (disks)


– Provided user interface to storage, mapping logical to physical
– Provides efficient and convenient access to disk by allowing data to be stored, located retrieved easily

• Disk provides in-place rewrite and random access




I/O transfers performed in blocks of sectors (usually 512 bytes)

File control block – storage structure consisting of information about a file


• Device driver controls the physical device
• File system organized into layers
Layered File System
• application program asks for a file, the first request is directed to the
logical file system.

• The logical file system contains the Meta data information of the
file and directory structure and provides protection also. It translates
file name into file number, file handle, location by maintaining file
control blocks (inodes in UNIX)

• The logical blocks of files need to be mapped to physical blocks in


harddisk. This mapping is done by File organization module

• The basic file system - issues the commands to I/O control in order to
fetch those blocks. Basic file system given command like “retrieve
block 123” translates to device driver.

• I/O controls contain the codes by using which it can access hard
disk.
• These codes are known as device driversGiven commands like
“read drive1, cylinder 72, track 2, sector 10, into memory location
1060” outputs low-level hardware specific commands to hardware
controller
File System Implementation
• on-disk and in-memory structures

• On Disk Data Structures


• Boot Control Block - 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.

• Volume Control Block - Volume control block all the File control
information regarding that volume such as number Block
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.
In-Memory File System Structures
• 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.
Virtual File Systems
∙ Virtual File Systems, VFS, provide a common
interface to multiple different filesystem types. In
addition, it provides for a unique identifier ( vnode )
for files across the entire space, including across all
filesystems of different types. ( UNIX inodes are
unique only across a single filesystem, and certainly
do not carry across networked file systems. )
∙ The VFS in Linux is based upon four key object types:
o The inode object, representing an individual file
o The file object, representing an open file.
o The superblock object, representing a filesystem.
o The dentry object, representing a directory entry.
Directory Implementation
Directories need to be fast to search, insert, and delete, with a minimum of wasted disk space.

Linear List

• A linear list is the simplest and easiest directory structure to set up, but it does have some drawbacks.

• Finding a file ( or verifying one does not already exist upon creation ) requires a linear search.

• Deletions can be done by moving all entries, flagging an entry as deleted, or by moving the last entry into the newly vacant position.

• Sorting the list makes searches faster, at the expense of more complex insertions and deletions.

• A linked list makes insertions and deletions into a sorted list easier, with overhead for the links.

• More complex data structures, such as B-trees, could also be considered.

Hash Table

• A hash table can also be used to speed up searches.

• Hash tables are generally implemented in addition to a linear or other structure


Directory Structure
• A directory is a container that is used to contain folders and file. It
organizes files and folders into a hierarchical manner.
Directory Structure(Contd…)
• There are several logical structures of a directory, these are given
below.
1.Single-level directory

2.Two-level directory

3.Tree-structured directory

4.Acyclic graph directory

5.General graph directory structure


1. Single level Directory Structure
• Single level directory is simplest directory structure.In it all files are
contained in same directory which make 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 the unique name . if two users call their dataset test, then the
unique name rule violated.
Single level Directory Structure(Contd…)
Advantages:
• Since it is a single directory, so its implementation is very easy.
• If the files are smaller in size, searching will become faster.
• The operations like file creation, searching, deletion, updating are very
easy in such a directory structure.
Disadvantages:
• There may chance of name collision because two files can not have the
same name.
• Searching will become time taking if the directory is large.
• In this can not group the same type of files together.
2. Two-level directory
• As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each user.

• In the two-level directory structure, each user has there own user files directory (UFD). The
UFDs has similar structures, but each lists only the files of a single user. system’s master
file directory (MFD) is searches whenever a new user id=s logged in. The MFD is indexed
by username or account number, and each entry points to the UFD for that user.
Two-level directory (contd…)
Advantages:
• We can give full path like /User-name/directory-name/.
• Different users can have same directory as well as file name.

• Searching of files become more easy due to path name and


user-grouping.
Disadvantages:
• A user is not allowed to share files with other users.

• Still it not very scalable, two files of the same type cannot be
grouped together in the same user
3. 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 there own subdirectories and to organize on their files
accordingly
Tree-structured directory(contd…)
A tree structure is the most common directory structure. The tree has a
root directory, and every file in the system have a unique path.
Advantages:
1. Very generalize, since full path name can be given.
2. Very scalable, the probability of name collision is less.
3. Searching becomes very easy, we can use both absolute path as well as relative
Disadvantages:
1. Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
2. We can not share files.
3. It is inefficient, because accessing a file may go under multiple directories.


4. Acyclic graph directory
• An acyclic graph is a graph with no cycle and allows to share
subdirectories and files. The same file or subdirectories may be in two
different directories. It is a natural generalization of the tree-structured
directory.
• It is used in the situation like when two programmers are working on a
joint project and they need to access files. The associated files are
stored in a subdirectory, separating them from other projects and files
of other programmers, since they are working on a joint project so they
want the subdirectories to be into their own directories. The common
subdirectories should be shared. So here we use Acyclic directories.
• It is the point to note that shared file is not the same as copy file . If
any programmer makes some changes in the subdirectory it will reflect
in both subdirectories.
Acyclic graph directory(contd…)

Advantages:
• We can share files.
• Searching is easy due to different-different paths.
Disadvantages:
• We share the files via linking, in case of deleting it may create the problem,
• If the link is softlink then after deleting the file we left with a dangling pointer.
• In case of hardlink, to delete a file we have to delete all the reference associated
with it.
5. General graph directory structure
• In general graph directory structure, cycles are allowed within a
directory structure where multiple directories can be derived from
more than one parent directory.

• The main problem with this kind of directory structure is to


calculate total size or space that has been taken by the files and
directories
General graph directory structure(contd…)
Advantages:
• It allows cycles.

• It is more flexible than other directories structure.

Disadvantages:
• It is more costly than others.

• It needs garbage collection.


Allocation Methods
• An allocation method refers to how disk blocks are allocated for files:

• Contiguous allocation

• Linked allocation

• Indexed allocation
Contiguous Allocation
• Each file occupies a set of contiguous blocks on the disk

• Simple – only starting location (block #) and length (number of blocks)


are required

• Random access

• Wasteful of space (dynamic storage-allocation problem)

• Files cannot grow


Contiguous Allocation
• Mapping from logical to physical
– Block to be accessed = ! + starting address
– Displacement into block = R

Q
LA/512
R
Contiguous Allocation of Disk Space
Extent-Based Systems
• Many newer file systems (I.e. Veritas File System) use a modified
contiguous allocation scheme

• Extent-based file systems allocate disk blocks in extents

• An extent is a contiguous block of disks


– Extents are allocated for file allocation
– A file consists of one or more extents
Linked Allocation
Each file is a linked list of disk blocks: blocks may be scattered
anywhere on the disk.

block
pointer
=
Linked Allocation (Cont.)
• Simple – need only starting address

• Free-space management system – no waste of space

• No random access

• Mapping
Block to be accessed is the Qth block in the linked chain of blocks representing the file.
Displacement into block = R + 1
File-allocation table (FAT) – disk-space allocation used by MS-DOS and OS/2.
Q
LA/511
R
Linked Allocation
File-Allocation Table
Indexed Allocation
• Brings all pointers together into the index block

• Logical view

index table
Example of Indexed Allocation
Indexed Allocation (Cont.)
• Need index table
• Random access
• Dynamic access without external fragmentation, but have
overhead of index block
• Mapping from logical to physical in a file of maximum size of 256K
words and block size of 512 words. We need only 1 block for
index table Q
Q = displacement into index table LA/512
R
R = displacement into block
Indexed Allocation – Mapping (Cont.)
• Mapping from logical to physical in a file of unbounded length
(block size of 512 words)
• Linked scheme – Link blocks of index table (no limit on size)
Q1
Q1 = block of index table LA / (512 x 511)
R1 is used as follows: R1

Q2 = displacement into block of index table Q2


R1 / 512
R2 displacement into block of file:
R2
Indexed Allocation – Mapping (Cont.)
• Two-level index (maximum file size is 5123)
Q1 = displacement into outer-index Q1
LA / (512 x 512)
R1 is used as follows: R1

Q2 = displacement into block of index table


R2 displacement into block of file:
Q2

R1 / 512

R2
Indexed Allocation – Mapping (Cont.)

outer-index

index table file


Worksheet
Problem:1
Consider a file currently consisting of 100 blocks. Assume that the file-control
block (and the index block, in the case of indexed allocation)is already in memory.
Calculate how many disk I/O operations are required for contiguous, linked, and
indexed (single-level) allocation strategies, if, for one block, the following
conditions hold. In the contiguous-allocation case, assume that there is no room to
grow at the beginning but there is room to grow at the end. Also assume that the
block information to be added is stored in memory.
a. The block is added at the beginning.
b. The block is added in the middle.
c. The block is added at the end.
d. The block is removed from the beginning.
e. The block is removed from the middle.
Answer
Problem:2
File of 101 blocks, file positions already in memory, and block to add already in
memory. Every directory or index operation is done in memory. There is room
on the disk after the file but not before. How many operations to...
1.Add a block at the beginning
2.Add a block after the 51st block
3.Add a block at the end
4.Remove the beginning block5.Remove the 51st block
6.Remove the end block
Free-Space Management
Bit vector (n blocks)

0 ⇒ block[i] free
bit[i] =
1 ⇒ block[i] occupied

(number of bits per word) *


(number of 0-value words) +
offset of first 1 bit
Free-Space Management (Cont.)
• Bit map requires extra space
– Example:

block size = 212 bytes


disk size = 230 bytes (1 gigabyte)
n = 230/212 = 218 bits (or 32K bytes)
• Easy to get contiguous files
• Linked list (free list)
– Cannot get contiguous space easily
– No waste of space

• Grouping
• Counting
Free-Space Management (Cont.)
• Need to protect:
– Pointer to free list
– Bit map
– Must be kept on disk
– Copy in memory and disk may differ
– Cannot allow for block[i] to have a situation where bit[i] = 1 in memory and bit[i] = 0
on disk
– Solution:
– Set bit[i] = 1 in disk
– Allocate block[i]
– Set bit[i] = 1 in memory
Directory Implementation
• Linear list of file names with pointer to the data blocks
– simple to program
– time-consuming to execute

• Hash Table – linear list with hash data structure


– decreases directory search time
– collisions – situations where two file names hash to the same location
– fixed size
Linked Free Space List on Disk
Efficiency and Performance
• Efficiency dependent on:
– disk allocation and directory algorithms
– types of data kept in file’s directory entry

• Performance
– disk cache – separate section of main memory for frequently used blocks
– free-behind and read-ahead – techniques to optimize sequential access
– improve PC performance by dedicating section of memory as virtual disk,
or RAM disk
Free Space Management
Objectives :
• Operating system maintains a list of free disk spaces to keep track of all
disk blocks which are not being used by any file.
• Whenever a file has to be created, the list of free disk space is searched
for and then allocated to the new file.
• A file system is responsible to allocate the free blocks to the file
therefore it has to keep track of all the free blocks present in the disk.
• Hence there is need for understanding the methods available for
managing free space in the disk
Understanding the methods available for maintaining the free
spaces in the disk

•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 in Figure 1 (where green
blocks are allocated) can be represented by a bitmap of 16 bits
as: 0000111000000110.
Bitmap or Bit vector

• The block number can be calculated as:


(number of bits per word) *(number of 0-values words)
+ offset of bit first bit 1 in the non-zero word .

• 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 =
Bitmap or Bit vector
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.
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.
2.Linked List

• 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.
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:
• Address of first free disk block

• 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.
Swap-Space Management
• Swapping is a memory management technique used in multi-
programming to increase the number of process sharing the CPU.
• It is a technique of removing a process from main memory and
storing it into secondary memory, and then bringing it back into
main memory for continued execution.
• This action of moving a process out from main memory to
secondary memory is called Swap Out and the action of moving a
process out from secondary memory to main memory is
called Swap In.
Swap-Space Management
• Swap-space — virtual memory uses disk space as an extension of main
memory.
• Main goal for the design and implementation of swap space is to provide the best
throughput for VM system
• Swap-space use
– Swapping – use swap space to hold entire process image
– Paging –store pages that have been pushed out of memory
• Some OS may support multiple swap-space
– Put on separate disks to balance the load
• Better to overestimate than underestimate
– If out of swap-space, some processes must be aborted or system crashed
Swap-Space Location
• Swap-space can be carved out of the normal file system, or in a
separate disk partition.
• A large file within the file system: simple but inefficient
– Navigating the directory structure and the disk-allocation data structure takes time and
potentially extra disk accesses
– External fragmentation can greatly increase swapping times by forcing multiple seeks during
reading or writing of a process image
– Improvement
• Caching block location information in main memory
• Contiguous allocation for the swap file
– But, the cost of traversing FS data structure still remains
Swap-Space Location
• In a separate partition: raw partition
– Create a swap space during disk partitioning
– A separate swap-space storage manager is used to
allocate and de-allocate blocks
– Use algorithms optimized for speed, rather than storage
efficiency
– Internal fragment may increase
• Linux supports both approaches
Swap-space Management: Example
• Solaris 1
– Text-segment pages are brought in from the file system and are thrown away if
selected for paged out
• More efficient to re-read from FS than write it to the swap space
– Swap space: only used as a backing store for pages of
anonymous memory
• Stack, heap, and uninitialized data
• Solaris 2
– Allocates swap space only when a page is forced out of physical memory
• Not when the virtual memory page is first created.
Thank you

You might also like