OS Unit4
OS Unit4
UNIT-IV
UNIT-IV:
• Memory Management: Memory
Management Swapping, Contiguous Memory
Allocation, Paging, Page-Table Structure,
Segmentation, Virtual Memory, Demand
Paging, Page-Replacement Algorithms, Frames
Allocation, Thrashing.
• File System Interface: File Concepts, Access
Methods and Directory Structure, File System
Mounting, File Sharing and Protection,
Allocation Methods, Free-Space Management,
Efficiency and Performance
• This bits are maintained in the page table, so during finding the frame
number in page table protection bits are also checked.
• There is one type of protection bits i.e., associated with the page i.e.,
called as Valid-Invalid bit
Valid-invalid bit attached to each entry in the page table:
– “valid” indicates that the associated page is in the process logical
address space.
– “invalid” indicates that the page is not in the process logical address
space.
Valid (v) or Invalid (i) Bit In A Page Table
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 inter process communication if sharing of
read-write pages is allowed
• Since the page table is paged, the page number is further divided into:
– a page number(p1)
– A page number(p2)
– a 12-bit page offset
• The logical address generated by the CPU is divided into two parts:
1. Page number
2. Offset/displacement
Using Hash function, this page number will generate a hash table
For every corresponding hash page number hash page table will be
generated.
And for every Hash page number a linked list is maintained.
Hashed Page Tables(Cont.)
• In this linked list,each element contains (1) the page number (2) for
page number the corresponding frame number (3) a pointer to the
next node in the linked list.
• Whatever the address is generated by the CPU, i.e, first it will generate
the hash page table for that hash page number and the page number
should be matched with the linked list.
– If it is matched with the linked list is found, then it is going to take
the corresponding frame number to get the exact physical address.
– If it is not matched in that particular linked list, with the help of
pointer list it will check the next linked list.It is going on checking
until it gets the required page number.
Hashed Page Table
Inverted Page Table
• It is used when multiple programs are working at the same time in the
computer’s memory, In case of multiprogramming environment, page
number and the offset is not enough even it is going to tell the process
identification number(PID).
• In this, logical address generated by the CPU is divided into three parts:
1. Process Identification Number(PID)
2. Page Number
3. Offset
• With the help of page number and the offset, it is going to search the page
table at ith location i.e., ‘i’ is the frame number. The frame number index ‘i’
and this particular offset is added to get an exact physical address.
• In this way logical address will be mapped onto the physical memory.
Inverted Page Table Architecture
Segmentation
• Memory-management scheme that supports user view of memory.
• The program will be divided not into equal size fragments but depends
on module size.
• In order to overcome the drawback of paging we go for segmentation.
• A program is a collection of segments
– A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
User’s View of a Program
Logical View of Segmentation
1
1 4
3
2
4
• Allocation of Frames
• Thrashing
1. Causes of Thrashing
2. Working Set Model
3. Page Fault Frequency
Virtual Memory
• Virtual memory is a technique that allows the
execution of processes that are not completely in
memory.
• Advantages are :
• Programs can be larger than physical memory.
• It abstracts main memory into an extremely large,
uniform array of storage, separating logical memory as
viewed by the user from physical memory.
• 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.
Virtual Memory
• In fact, examination of real programs shows us that, in many
cases, the entire program is not needed to be put in main
memory. For instance, consider the following:
• Programs often have code to handle unusual error conditions.
Since these errors seldom, 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.
Virtual Memory -Introduction
• Virtual memory involves the separation of logical
memory as perceived by users 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;
he/she can concentrate instead on the problem to be
programmed.
Diagram showing Virtual Memory large than Physical
Memory
Demand Paging
• Demand paging : Pages are loaded when they are demanded
during program execution, pages never accessed are never
loaded.
• A demand-paging system is similar to a paging system with
swapping where processes reside in secondary memory (usually a
disk).
• When we want to execute a process, we swap it into memory.
Rather than swapping the entire process into memory, we use a
lazy swapper.
• A lazy swapper never swaps a page into memory unless that page
will be needed. A swapper manipulates entire processes, whereas
a pager is concerned with the individual pages of a process. We
thus use “pager,” rather than “swapper,” in connection with
demand paging.
Demand Paging
Page table when some pages are not in main memory
Page Fault
• What happens if the process tries to access a page that was not
brought into memory?
• Access to a page marked invalid causes a page fault.
• The paging hardware, in translating the address through the page
table, will notice that the invalid bit is set, causing a trap to the
operating system.
• This trap is the result of the operating system’s failure to bring the
desired page into memory.
Procedure for handling Page Fault
The procedure for handling this page fault is:
1. Check the page table to determine whether the reference was
a valid or an invalid memory access.
2. If the reference was invalid, we terminate the process. If it was
valid but not in main memory, we now load it in.
3. We find a free frame by taking one from the free-frame list.
4. We schedule a disk operation to read the desired page into the
frame.
5. When the disk read is complete, we modify the internal
reference bit in page table.
6. We restart the instruction that was interrupted by the trap.
Steps in Handling a Page Fault
Page Replacement
What happens if the main memory is full and there are
no free frames? --------- Solution is Page replacement.
1) Find the location of the desired page on the disk.
2. Find a free frame:
a. If there is a free frame, use it.
b. If there is no free frame, use a page-replacement algorithm to
select a victim frame.
c. Write the victim frame to the disk; change the page and frame
tables accordingly.
3. Read the desired page into the newly freed frame; change the
page and frame tables.
4. Continue the user process from where the page fault occurred.
Page Replacement
15 page faults
• Belady found that, In FIFO page replacement
algorithm, the number of page faults will get
increased with the increment in number of frames.
• This is an Anomaly called as Belady's Anomaly.
Belady’s Anomaly
Optimal Page Replacement Algorithm
• This algorithm has the lowest page-fault rate
of all algorithms and will never suffer from
Belady’s anomaly. Such an algorithm is called
OPT or MIN.
• Principle is : Replace the page that will not
be used for the longest period of time.
• The optimal page-replacement algorithm is
difficult to implement, because it requires
future knowledge of the reference string.
• As a result, the optimal algorithm is used
mainly for comparison studies.
Optimal Page Replacement Algorithm
• Reference string:
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time
per process)
9 page faults
LRU(Least Recently Used) Page Replacement
algorithm
• LRU replacement associates with each page
the time of that page’s last use.
• When a page must be replaced, LRU chooses
the page that has not been used for the
longest period of time.
• We can think of this strategy as the optimal
page-replacement algorithm looking backward
in time, rather than forward.
LRU Page Replacement Algorithm
• Reference string:
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time
per process)
12 page faults
LRU Page Replacement algorithm
• LRU page-replacement algorithm may require
substantial hardware assistance.
• Two implementations are possible:
a)Counters
b)Stack
• Like optimal replacement, LRU replacement
does not suffer from Belady’s anomaly.
• Both belong to a class of page-replacement
algorithms, called stack algorithms, that can
never exhibit Belady’s anomaly.
LRU Algorithm (Cont.)
• Counter implementation
– Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
– When a page needs to be changed, look at the counters to find
smallest value
• Search through table needed
• Stack implementation
– Keep a stack of page numbers in a double link form:
– Page referenced:
• move it to the top
– But each update more expensive
• LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
LRU Approximation Algorithms
• LRU needs special hardware and still slow
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced bit set to 1
– Replace any with reference bit = 0 (if one exists)
• Second-chance algorithm
– Generally FIFO, plus hardware-provided reference bit
– Clock replacement
– If page to be replaced has
• Reference bit = 0 -> replace it
• reference bit = 1 then:
– set reference bit 0, leave page in memory
– replace next page
Second-Chance (clock) Page-Replacement Algorithm
Counting Algorithms
• Keep a counter of the number of references that
have been made to each page
– Not common