OS Unit 4
OS Unit 4
Memory Management
Basics of Memory Management
Main Memory
– 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.
• 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
⮚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 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.)
• 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
• 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
3. It does not utilize the main memory effectively. It utilizes the main memory effectively.
First-fit and best-fit memory allocation does not suffer from internal First-fit and best-fit memory allocation suffers from external
fragmentation. fragmentation.
Segmentation
Segmentation
• Memory-management scheme that supports user view of memory.
• The programmer talks about “the stack,” “the math library,” and “the
main program” without caring what addresses in memory these
elements occupy.
• 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
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
• 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
• Virtual page numbers are compared in this chain searching for a match
• If a match is found, the corresponding physical frame is extracted
• Rather than each process having a page table and keeping track of all
possible logical pages, track all physical pages
• 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
memory.
Virtual memory – involves the separation of logical memory as perceived by users from physical memory.
⮚ Programs often have code to handle unusual error conditions. Since these errors seldom, if ever, occur in
⮚ Arrays, lists, and tables are often allocated more memory than they actually need. An array may be declared
⮚ 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.
⮚ 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
⮚ 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.
⮚ Demand paged virtual memory, pages are loaded only when they are
needed during program execution.
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.
4. Check that the page reference was legal and determine the location of the page on the disk
1. Wait in a queue for this device until the read request is serviced
8. Save the registers and process state for the other user
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
⮚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
⮚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.
⮚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
⮚ 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.
⮚ 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
4* 7* 6* 1*
7 6 1 2
6 1 2 7
Initial Queue Queue after first replacement Queue after third replacement
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 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’
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
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’
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
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’
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
⮚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
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
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
⮚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
⮚The system can detect thrashing by evaluating the level of CPU utilization as
compared to the level of multiprogramming.
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
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.
• 1956
• 50 x 24” platters
• Mainly used for backup, storage of infrequently-used data, transfer medium between systems
• 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.
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:
• Throughput increases
Disadvantages:
• Can cause Starvation for a request if it has higher seek time as compared to incoming requests
• 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
• 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
• 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
• 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
• 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)
• 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
• 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 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
• 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.
Hash Table
2.Two-level directory
3.Tree-structured directory
• 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.
• 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.
Disadvantages:
• It is more costly than others.
• Contiguous allocation
• Linked allocation
• Indexed allocation
Contiguous Allocation
• Each file occupies a set of contiguous blocks on the disk
• Random access
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
block
pointer
=
Linked Allocation (Cont.)
• Simple – need only starting address
• 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
R1 / 512
R2
Indexed Allocation – Mapping (Cont.)
outer-index
0 ⇒ block[i] free
bit[i] =
1 ⇒ block[i] occupied
• 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
• 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
• 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.
• 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