Storage Methods in File Management
Storage Methods in File Management
&
Virtual Memory
Unit - 4
A Presentation by
Dr Maj Ravindra Babu Kallam
Prof &Head CSE, KITS(S)
Topics to be covered:
• Logical versus Physical Address Space,
Swapping,
• Contiguous Allocation
• Paging, Segmentation
• Segmentation with Paging
• Virtual memory
– Demand Paging, Page fault, Page Replacement
– Thrashing
– Page Replacement Algorithms.
Operating system 4th Unit by Dr Maj R
2
B Kallam
Word Transfer Block Transfer
Secondary
Cache Main
CPU Memory Memory
Memory
Main Memory
Electronic Disk
Optical Disk
Secondary Memory
Magnetic Disk
Magnetic tapes
Operating system 4th Unit by Dr Maj R
4
Storage Device Hierarchy
B Kallam
Memory Management Requirements:
• Relocation:
– Programmer does not know where the program will be placed in memory
when it is executed .
– While the program is executing, it may be swapped to disk due to several
reasons and returned to main memory at a different location is called
relocation.
• Protection:
– Processes should not be able to reference memory locations in another
process without permission.
– It is also one of the major requirement in Memory management.
– Memory protection requirement must be satisfied by the processor
(hardware) rather than the operating system (software)
Disadvantage:
-It suffers with the internal fragmentation
P# F# P# F#
0 5 0 3
1 6 1 4
2 7 2 9
3 8 3 10
Process table C Process table D
Operating system 4th Unit by Dr Maj R
19
B Kallam
Logical to Physical address conversions:
Logical to Physical address translation in paging.
– Logical Address: It is a temporary address; a reference to the memory
location independent of the current assignment of data to memory; a
translation must be made to a physical address before the memory access.
– Physical Address: The absolute location of a unit of data in memory.
Extract the page number as the leftmost n bits of the logical address.
Use the page number as an index into the process page table to find the
frame number, k .
Finally, The physical address is easily constructed by appending the frame
number to the offset.
Physical address (Main memory) is of 228 bits, because the page size is 4Kbytes each, then the
frame size is also 4Kbytes which can be written as is 212 . With this the offset value =12 and the
frame number is in 28-12=16bits=2Bytes.
With this the frame number in the page table is of 2bytes and the page table is having 220 entries,
Hence the total buffer required for 1 page table to store is 220 X2Bytes= 2MB, where as the size of
the frame is only 4KB.
So we cannot fit the page table in a single frame. 23
Operating system 4th Unit by Dr Maj R
24
B Kallam
• To solve this problem a page table can be divided in to multiple level.
• In our example, the page number is further divided into a 9-bit page
number (2MB/4KB= 221/212=29)(Outer page table) and a 11-bit page offset
(220-29=211)or (4KB/2B)(Inner page table). Thus, a logical address is as
follows:
page number Page Offset
P1 P2 d
09 11 12
• Where P1 is an index into the outer page table, and P2 is the displacement
within the inner page table. The address-translation scheme for this
architecture is shown in Figure below.
• Virtual paging.
– Page fault
– Fetch Policy
Pre paging
Demand Paging
– Thrashing
Operating system 4th Unit by Dr Maj R
30
B Kallam
Virtual Memory:
• One of the major resources in the computer is memory, to utilize the
memory in efficient way and to increase the CPU utilization in multiuser
OS, we need to share the Memory for more number of users.
• In the process of evaluation memory allocation, we have learned how to
divide our process and memory into multiple partitions using paging and
segmentation.
• As ours is a control flow computer, the CPU Executes instructions in
continues fashion based on the program counter one after another. With this
it is conformed that we need not to fetch the total pages of a process in to
the memory.
• With this approach, each process is allowed to bring only a fixed number
of pages in to the memory. Hence we can accommodate more number of
processes in the main memory.
• While executing a process, If at all, if the required page is not available in
the main memory, it may leads to page fault, then CPU demands for that
page and it is known as demand paging.
Operating system 4th Unit by Dr Maj R
31
B Kallam
• Using swapping we need to bring the required page in to the
memory by sending one of the available pages out, if again
page fault occurs, immediately after the previous swapping
and it continuous further it may lead to Thrashing.
• To avoid thrashing we need to use the page replacement
algorithms and should find the best algorithm with minimum
page faults.
• An end user or computer operator may not to know what
really happening inside the computer, because he could able
to execute all processes concurrently, he may think that, he is
having sufficient memory inside the computer. But in reality
which is not there. This Illusion in related to memory is called
Virtual memory, and if we apply paging it is called Virtual
paging.
Operating system 4th Unit by Dr Maj R
32
B Kallam
Ex:
If we have two processes P1&P2; process P1 requires 100Kb
and P2 also requires 100Kb and if the total available space is
only 100Kb then how they will execute the programs in
parallel?.
To solve this problem by using paging technique we can divide
P1 into 2 pages and P2 into 2 pages each of 50Kb and also
memory into 2 frames of each 50Kb, by allocating 1 frame to
each process and using swapping technique we can execute
both the process in parallel and can get the results.
But the end users will think that, there is sufficient buffer
space that is 100Kb for each process because they could able
to execute the program. To fulfull above requirement we
should have 200Kb but in reality we have only 100Kb, this
approach is called Virtual memory, as we have used paging it
in turn called VirtualOperating
paging. system 4th Unit by Dr Maj R
33
B Kallam
Translation look aside buffer (TLB)
• In principle every virtual memory reference can
cause two physical memory accesses: one to fetch
the appropriate page table entry and one to fetch
the desired data.
• Thus a straight forward virtual memory scheme
would have a effect of doubling the memory access
time.
• To overcome this problem most virtual memory
schemes make use of a special high speed cache for
page table entries, usually called a TLB.
• This cache function in the same way as a memory
cache and contains those page tables entries that
have been most recently
Operating systemused.
4th Unit by Dr Maj R
B Kallam
34
• Given a virtual address, the processer will first examine the
TLB. If the desired page table entry is present ( TLB hit), then
the frame number is retrieved and the real address is formed .
• If the desired page table entry is not found (TLB miss), then
the processor uses the page number to index the process
page table in the main meory and examine the corresponding
page table entry.
• If the “present bit” is set, then the page is in the main
memory, and the processer can retrieve the frame number
from the page table entry to form the real address. The
processor also updates the TLB to include this new page table
entry.
• Finally if the “ Present bit” is not set, then the desired page
not in main memory and a memory access fault, called a page
fault, is issued.
Operating system 4th Unit by Dr Maj R
35
B Kallam
Operating system 4th Unit by Dr Maj R
36
B Kallam
Segmentation:
• It is similar to dynamic partitioning
• A user program can be subdivided using segmentation, in
which the program and its associated data are divided into a
number of segments .
• It is not required that all segments of all programs be of the
same length, although there is a maximum segment length.
• Due to the use of unequal-size segments, segmentation is
similar to dynamic partitioning.
• The difference, compared to dynamic partitioning, is that with
segmentation a program may occupy more than one partition,
and these partitions need not be contiguous.
Ex 2:
– Segmentation fault
– Fetch Policy
–Pre Segmentation
–Demand Segmentation
– Placement Policy
–First Fit
–Next Fit
–Best Fit
Operating system 4th Unit by Dr Maj R
42
B Kallam
Page replacement algorithms:
FIFO:
• This policy treats the page frames allocated to a process as a circular
buffer, and pages are removed in round-robin style.
• All that is required is a pointer that circles through the page frames of
the process.
• With this approach when a page must be replaced, the oldest page is
chosen for replacement.
Optimal:
• This policy selects for replacement that page for which the time to the
next reference is longest.
• Use of this page-replacement algorithm guarantees the lowest
possible page fault rate for a fixed number of frames
• This policy is impossible to implement, because it would require the
OS to have perfect knowledge of future events.
Operating system 4th Unit by Dr Maj R
43
B Kallam
LRU (Least Recently Used):
• This policy replaces the page in memory that has not been
referenced for the longest time.
• The problem with this approach is the difficulty in
implementation.
• One approach would be to tag each page with the time of its
last reference; this would have to be done at each memory
reference, both instruction and data.
• It creates lot of overhead and expensive.
Ex1: Let us assume that only three frames (fixed resident set size) are
allocated for a process, the execution of the process requires
reference to five distinct pages and the page address stream formed
by executing the program is
2 3 2 1 5 2 4 5 3 2 5 2. Apply FIFO, Optimal and LRU page
replacement algorithms and the number of page faults.
Operating system 4th Unit by Dr Maj R
44
B Kallam
Operating system 4th Unit by Dr Maj R
F = page fault occurring after the frame allocation is initially filled
B Kallam
45
Ruff Page
2 3 2 1 5 2 4 5 3 2 5 2
2 2 2 2
3 3 3
– Counting Algorithms:
• LFU Algorithm
• MFU Algorithm v
• The OS shifts the reference bit for each page into the higher order bit of its 8
bit byte, shifting the other bits right 1 bit, discarding the lower order bit.
• These 8 bit shift register contain the history of page use for the last eight time
periods.
• If the shift register contains 00000000, then the page has not been used for
eight time periods.
• The page that is used at least once each period would have a shift register
value of 11111111.
• A page with a history register value of 11000100 has been used more recently
than has one with 01110111. If we interpret these 8 bit bytes as unsigned
integers, the page with the lowest number is the LRU page, and it can be
replaced. Operating system 4th Unit by Dr Maj R
49
B Kallam
Example:
Snapshot -1
When a page has to be replaced, the first page encountered in the lowest
non empty class is chosen.
• Example:
– Process-1 needs 10 frames
– Process – 2 needs 127 frames
– Available is only 62 frames
– With this approach each will get 31 frames
– The process-1 does not need more than 10 frames, so the
other 21 are strictly wasted.
S= Si
Then, if the total number of available frames is m, we allocate ai
frames to process Pi, where ai is approximately
ai = (Si / S ) X m.
For previous example:
process-1 gets (10/137) X 62 = 4
process-2 gets (127/ 137) X 62 = 57
In this way both processes share the available frames according
to their needs.
Operating system 4th Unit by Dr Maj R
58
B Kallam
Global Versus Local Allocation
• Global replacement – process selects a replacement frame
from the set of all frames; one process can take a frame from
another.
• Local replacement – each process selects from only its own set
of allocated frames.