Unit IV-OS
Unit IV-OS
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 thevalue 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.
A. Equal-size partitions: Main memory is divided into a number of static partitions at a system
generation time. Any process whose size is less than or equal to the partition size can be
loaded into an available partition. The operating system can swap a processout of a partition,
if none are in a ready or running state. The maximum size of program that can be executed is
8M.
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.
Compaction: One technique for overcoming external fragmentation is COMPACTATION. From time
to time, the operating system shifts the processes so that they are contiguous and sothat all of the free
memory is together in one block. For example, in fig (8.h), compaction will result in a block of free
memory of length 16M. The difficulty with compaction is that it is time consuming procedure and
wasteful of processor time. The compaction implies the need for a dynamic relocation capability.
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 thedisk.
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 topage 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(25). The size
of page is 4 bytes. So, it requires 2 bits to represent 4 bytes (22). 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, thepage
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 bevery 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 inthe 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.
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 (212), 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:
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.
Segmentation:
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.
Fig 22 have five segments numbered through from 0 to through 4. The segments arestored
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 longand 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.
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
Page fault service time = 8 milli seconds ( 8,000,000 nano seconds) and memory accesstime 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 usedto 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.
The page replacement algorithm finds out the victim page. 1) If there are anymodifications
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.
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 shiftregister 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 referencebit 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, andthe new page is inserted
in the circular queue in that position. Second-chance replacement degenerates to FIFO
replacement if all bits are set.