Os Unit 4 Modify
Os Unit 4 Modify
Memory Management
Logical address – is reference to memory location independent of the current assignment of data
to memory; a translation must be made to physical address before the memory access can be
achieved. It is generated by the CPU; also referred to as virtual address.
Physical address or Absolute Address is actual memory location in memory. It is the address
seen by the memory unit (HW).
Logical and physical addresses are the same in compile-time and load-time address-binding
schemes; logical (virtual) and physical addresses differ in execution-time address-binding
scheme.
A relative address is a particular example of logical address, in which the address is expressed as
a location relative to some known point, usually a value in a processor register.
Swapping requires a backing store. The backing store is commonly a fast disk. A job can be
swapped out and still be on the Ready Queue. So now, we can have more jobs on Ready Queue
than will fit in main memory. If a dispatcher selects a process which is not in main memory, that
process must be brought into memory. If binding is done at assembly or load time, then the
process cannot be loaded to a different memory location. If execution-time binding is being used
then the process can be loaded anywhere in the memory.
Memory Mapping and Protection: The relocation register contains the value of the smallest
physical address; the limit register contains the range of logical addresses (for example, each
relocation= 100040 and limit = 74600). With relocation and limit registers, each logical address
must be less than the limit register; the MMU maps the logical address dynamically by adding the
value in relocation register. The mapped address is sent to memory. When the CPU scheduler
selects a process for execution, the dispatcher loads the relocation and limit registers as a part of
context switching.
Memory Allocation:
Fixed sized partitions (Static Partitions): One of the simplest methods for allocating memory is
divide memory into several fixed-sized Partitions. Each partition may contain exactly one
process. Thus, the degree of multiprogramming is bound by number of partitions. In this, multi
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. This method is used in batch processing operating system. In the fixed-partition
scheme, the operating system keeps a table indicating which parts of memory are available and
which are occupied.
B. Unequal Size Partitions: The maximum size of program that can be loaded into
memory is 16M. Smaller programs can be placed in smaller partitions, reducing
internal fragmentation.
Placement Algorithm: If process size is equal to partition size process is allocated to same
partition. If process size is not equal to partition size, the process is assigned to the smallest
partition within which it will fit.
Fixed Partitioning Problems: If a program does not fit in a partition it cannot be executed. Any
program, no matter how small, occupies an entire partition. This results in internal
fragmentation. The number of active processes is limited by the system. A large number of very
small processes will not use the space efficiently.
Fixed Partitioning Strengths: It is very simple to implement; little operating system overhead.
First-fit: allocates the first hole that is big enough. Searching can start either at the beginning of
the set of holes or where the previous first-fit search ended. The search stops as soon as it finds a
free hole that is large enough.
Best fit: Allocate a smallest hole that is big enough. This strategy produces the smallest leftover
hole.
Worst fit: Allocate the largest hole. This strategy produces the largest leftover hole, which may
be more useful than the smaller leftover hole from a best-fit approach.
Fig 9: Example Memory configuration before and after Allocation of 16M Byte block
Paging:
Paging is a memory-management scheme that permits the physical address of a process to be
noncontiguous. Divide logical memory (programs) into blocks of same size called pages. Divide
physical memory into equal sized partitions called frames (size is power of 2, between ½K and
8K). The size of the page and frame must be equal.
When a process is to be executed, its pages are loaded into any available memory frames from the
disk. Page table is used to map the page number to corresponding frame number. The page table
contains the base address of the page (frame) in memory. Page number will be used as index for
the page table. Whenever program is loaded into memory the corresponding frame number is
updated to page table. In the fig 11, page 0 is loaded into frame 1, page 1 is loaded into frame 4,
page 2 is loaded into frame 3 and page 3 is loaded into frame 7. The frame numbers are updates to
page table. If a frame is allocated to one process it cannot be allocated to another process.
CPU generates a logical address; every address generated by the CPU is divided into two parts: a
page number (p) and a page offset (d). The page number is used as index for the page table. The
page number is replaced with corresponding frame number as shown in fig 10. The
MMU(memory management unit) converts logical address into physical address. The base
address + offset is sent to MMU which is mapped to the corresponding physical page.
Assume that, the size of process P1 is 10KB and frame size of memory is 4 KB. Now, process P1
requires 3 frames. The process P1 occupies 2KB of third frame. The remaining 2KB cannot be
allocated to any other process. This hole is called internal fragmentation.
In fig 12, the physical memory size is 28 bytes. So, it requires five bits to represent each bit(2 5).
The size of page is 4 bytes. So, it requires 2 bits to represent 4 bytes (2 2). So, d= 2 bits. 25 - 22 = 3
bits required page number.
Hardware Support:
The hard ware implementation of page table can be done in several ways. In the simplest case, the
page table is implemented as a set of dedicated registers. These registers should be built with
very high speed logic. The use of registers for page table is satisfactory if the page table is
reasonably small(256 entries). Most contemporary computers, however, allow the page table to be
very large(1 million entries). The use of fast registers for such a large page table is not feasible.
Rather, page table is kept in main memory, and a page-table base register (PTBR) points to the
page table.
The problem with this approach is the time required to access a user memory location. In this
scheme two memory references are required to access a byte. Thus, memory access is slowed by a
factor of 2. This delay would be intolerable under most circumstances.
The solution to this problem is to use a special small fast-lookup hardware cache, called a
Translation look-aside buffer(TLB). The TLB is associative high-speed memory. Each entry in
the TLB consists of two parts: a key(or tag) and a value. When the associative memory is
presented with an item, the item is compared with all keys simultaneously. If the item is found,
the corresponding value filled is returned. The search is fast; the hardware however, is expensive.
Typically, the number of entries in a TLB is small, often numbering between 64 and 1,024.
The TLB is used with page tables in the following way. The TLB contains only a few of the
page-table entries. When a logical address is generated by the CPU, Its page number is presented
to the TLB. If the page number is found, its frame number is immediately available and is used to
access memory. The whole task may take less than 10 percent longer than it would if an
unmapped memory reference were used.
Protection:
One bit can be defined a page to be read-write or read-only. Every reference to memory goes
through the page table to find the frame number. At the same time physical address being
computed, the protection bits can be checked to verify that no writes are being made to a read-
only page. An attempt to write to a read-only page causes a hardware trap to the operating system.
One additional bit is generally attached to each entry in the page table: a Valid-invalid bit. When
this bit is set to “valid”, the associated page is in the process’s logical address space and is thus a
legal page. When the bit is set to “invalid “, the page is not in the process’s logical address space.
As shown fig 15, program has 0-5 pages. An attempt to generate an address in page 6 or 7,
however, will find that the valid-invalid bits is set to invalid, and the computer tarp to OS.
10 | P a g e Memory Management
Unit – IV Operating Systems
Shared Pages:
An advantage of paging is the possibility of sharing common code. This consideration is
particularly important in a time-sharing environment. Consider a system that supports 40 users,
each of whom executes a text editor. If the text editor consists of 150KB of code and 50 KB of
data space, we need 8,00KB to support 40 users. Assume each page size is 50KB. If the code is
reentrant code, pages can be shared. As shown in fig 16, three page editors share the three code
pages and each process has separate data page.
Reentrant code is non-self-modifying code; it never changes during execution. Thus two or
processes can execute the same code at the same time.
Only, one copy of editor needs to be kept in physical memory. Each user’s page table maps onto
same physical copy of the editor, but the data pages are mapped onto different frames. Thus, to
support 40 users, we need only one copy of the editor (150KB), plus 40 copies of the 50KB of
data space per user. The total space required is now 2,150KB instead of 8,00KB.
Hierarchical Paging:
Most modern computer systems support a large logical address space (232 or 264). In such case
page table itself becomes excessively large. For example, consider a system with a 32-bit logical
address space; if page size in such a system is 4KB (2 12), then the page table consists of one
million entries. Assume each entry consists of 4 bytes; each process may need up to 4MB of
physical address space for the page table alone. Clearly, we would not want to allocate the page
table contiguously in main memory. One simple solution to this problem is to divide the page
table into smaller pieces.
Two level Paging algorithm:
11 | P a g e Memory Management
Unit – IV Operating Systems
For a 32-bit machine with a page size of 4KB, a logical address is divided into page number
consisting of 20 bits and a page offset consisting of 12 bits. The page number is further divided
into a 10-bit page number and 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 outer
page table. This scheme is also known as forward-mapped page table. The address translation
scheme is mentioned in fig 18.
12 | P a g e Memory Management
Unit – IV Operating Systems
For a system with a 64-bit logical address, a two-level paging scheme is no longer appropriate.
Assume page size is 4KB. If we adopt two level paging scheme
Segmentation:
13 | P a g e Memory Management
Unit – IV Operating Systems
1. The code
2. Global Variables
3. The heap, from which memory is allocated
4. The stacks used by each thread
5. The standard C library
Hardware:
The segment table is used to map the two-dimensional logical address into one
dimensional physical address. Each entry in the segment table has a segment base and
segment limit. The segment base contains the starting physical address where the segment
resides in memory, whereas the segment limit specifies the length of the segment.
14 | P a g e Memory Management
Unit – IV Operating Systems
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
logical address must be between 0 and the segment limit. If it is not, we trap to the
operating system. When an offset is legal, it is added to the segment base to produce the
address in physical memory of the desired byte.
Fig 22 have five segments numbered through from 0 to through 4. The segments are
stored in physical memory as shown. The segment table has separate entry for each
segment giving the beginning address of the segment in physical memory (or base) and
the length of the segment (or limit). For example (fig: 22), segment 2 is 400 bytes long
and beginning at location 4300. Thus, 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 + 852=
4052. A reference to byte 1222 of segment 0 would result in a trap to OS, as this segment
is only 1000 bytes long.
Fig 1: Diagram showing virtual memory that is larger than physical memory
The part of the program is in main memory and remaining part of memory will be in
secondary memory as shown in Fig 1. The virtual memory allows files and memory to be
shared by two or more processes through page sharing.
16 | P a g e Memory Management
Unit – IV Operating Systems
never accessed are never loaded into memory. Demand paging uses Lazy swapper. A
Lazy swapper never swaps a page into memory unless that page will be needed.
Divide logical memory (programs) into blocks of same size called pages. Divide physical
memory into equal sized partitions called frames (size is power of 2, between ½K and
8K). The size of the page and frame must be equal. When a process is to be executed,
some of its pages are loaded into any available memory frames from the disk. Page table
is used to map the page number to corresponding frame number. The page table contains
the base address of the page (frame) in memory. Page number will be used as index for
the page table. Whenever program is loaded into memory the corresponding frame
number is updated to page table.
Fig : Page table when some pages are not in main memory.
With each page table entry a valid–invalid bit is associated. If valid-invalid bit is ‘v’, the
associated page is both legal and in-memory. If the valid-invalid bit is ‘i’, the page is not
valid or is valid but currently on the disk
17 | P a g e Memory Management
Unit – IV Operating Systems
CPU generates a virtual address and it contains page number and offset. The page number
is used as index of the page table. If its corresponding valid-invalid bit is ‘v’, then physical
address is generated by replacing page number in virtual address with corresponding
frame number. If the corresponding valid-invalid bit is ‘i’ then page fault is generated.
The procedure for handling page fault:
1) Check memory access is valid or invalid.
2) If the reference is invalid terminate the process. If it is valid and valid-invalid bit
is ‘i’, page fault is generated and trap to Operating system.
3) Find the free frame in the main memory and requested page on the disk.
4) Load the requested page into free frame.
5) Update page table with the corresponding frame number.
6) Restart the defaulted instruction.
Page fault service time = 8 milli seconds ( 8,000,000 nano seconds) and memory access
time is 200 nano seconds.
Effective access time = ( 1 – p) X (200) + p X 8 milliseconds.
= ( 1 – p) X 200 + p X 8,000,000
= 200 + 7,999,800 X p
The effective access time is directly proportional to the page-fault rate.
Copy-on-write:
Fork is a system call creates a child process as a duplicate of its parent. fork()
worked by creating a copy of the parent’s address space for the child, duplicating the
pages belonging to the parent. Instead of that, a copy-on-write technique can be used
to share same pages by parent and child. These shared pages are marked as copy-on-
write pages, meaning that if either process writes to a shared page, a copy of shared
page created. Copy-on-write allows more efficient process creation as only modified
pages are copied.
19 | P a g e Memory Management
Unit – IV Operating Systems
20 | P a g e Memory Management
Unit – IV Operating Systems
Whenever page fault occurs:
1. Find the requested page on the disk.
2. Find the free frame in the memory.
a) If there is a free frame, use it.
b) If there is no free frame use page replacement algorithm to find a victim
frame.
c) Write the victim frame to the disk; change the page and frame tables
accordingly.
3. Load the requested page into free frame.
4. Update page table with the corresponding frame number and set valid-invalid
bit = ‘V’.
5. Restart the instruction that caused the page fault.
The page replacement algorithm finds out the victim page. 1) If there are any
modifications on that page, the victim page will be written on to the disk. 2) The
corresponding page entry values in the page table are modified ( i.e frame number
=0, and valid-invalid bit = ‘i’ ). 3) The requested page will be loaded into vacated
frame in the memory 4) Reset the page table for new page.
21 | P a g e Memory Management
Unit – IV Operating Systems
22 | P a g e Memory Management
Unit – IV Operating Systems
Notice that number of faults for four frames (ten) is greater than the number of faults
for three frames (nine). This most unexpected result is known as Belady’s anomaly:
for some page-replacement algorithms, the page fault rate may increase as number of
allocated frames increases.
Optimal Page Replacement:
One result of the discovery of Belady’s anomaly was the search for an Optimal
page-replacement algorithm. An optimal page-replacement algorithm has the lowest
page-fault rate of all algorithms and will never suffer from Belady’s anomaly.
Optimal algorithm simply replaces the page that will not be used for the longest
period of time. Use of optimal page-replacement algorithm guarantees the lowest
possible page fault rate for a fixed number of frames.
Fig 14: Use of a stack to record the most recent page references.
LRU-Approximation Page Replacement Algorithm:
Some systems provide few computer systems provide sufficient hardware support for
true LRU page replacement. Some systems provide no hardware support, and other
page replacement algorithm must be used. Many systems provide reference bit for
each page entry in the page table. Initially all bits are cleared (set to 0) by operating
system. As a user process executes, the bit associated with each page reference is set
(to 1) by the hardware.
Additional-Reference-Bits Algorithm:
We can keep an 8-bit byte for each page in a table in memory. At regular intervals (
every 100 milliseconds), a timer interrupt transfers control to the operating system.
The operating system shifts the reference bit for each page into the high-order bit of
its 8-bit byte, shifting the other bits right by 1 bit discarding low-order bit. If the shift
register contains 00000000, for example, then the page has no been used for eight
time periods; a page that is used at least once in each period has a shift register value
of 11111111. A page with a history register value of 10000000 has been used more
recently than one with the value of 01111111. The page with the lowest 8-bit byte
number is the LRU page, and it can be replaced.
Second-Chance Algorithm or Clock algorithm:
It is like a FIFO replacement algorithm. When a page has been selected, we inspect
its reference bit. If the value is 0, we proceed to replace this page; but if the reference
bit is set to 1, we give the page a second chance and move on to select the next FIFO
page. If a page is used often enough to keep its reference bit set, it will never be
replaced.
One way to implement the second-chance algorithm is as circular queue. A pointer
indicates that which page to be replaced next as shown in fig 13. When a frame is
needed the pointer advances until it finds a page with a 0 reference bit. As it
advances it clears reference bits. Once victim page is found, the page is replaced, and
the new page is inserted in the circular queue in that position. Second-chance
replacement degenerates to FIFO replacement if all bits are set.
24 | P a g e Memory Management
Unit – IV Operating Systems
26 | P a g e Memory Management
Unit – IV Operating Systems
This model uses a parameter, Δ, to define working set window. The idea is to
examine the most recent Δ page references. The set of pages in the most recent Δ
page references is the working set.
If the page is in active use, it will be in the working set. If it is no longer being used,
it will drop from the working set Δ time after its last reference. Thus, the working set
is approximation of the program’s locality.
For example, given the sequence of memory references shown in fig 15, if Δ = 10
memory references, then the working set at time t1 is { 1,2,5,6,7}. By time t2, the
working set has changed to {3,4}. The accuracy of working set depends on the
section of Δ. If Δ is too small, it will not encompasses the entire locality; if Δ is too
large, it may overlap several localities.
28 | P a g e Memory Management