






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































Memory management is the functionality of an operating system which handles or manages primary memory. Memory management keeps track of each and every memory location either it is allocated to some process or it is free. It checks how much memory is to be allocated to processes. It decides which process will get memory at what time. It tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.
Memory management provides protection by using two registers, a base register and a limit register. The base register holds the smallest legal physical memory address and the limit register specifies the size of the range. For example, if the base register holds 300000 and the limit register is 1209000, then the program can legally access all addresses from 300000 through 411999
Goals of Memory Management
Multiprogramming
To overcome the problem of underutilization of CPU and main memory, the multiprogramming was introduced. The multiprogramming is interleaved execution of multiple jobs by the same computer. In multiprogramming system, when one program is waiting for I/O transfer, there is another program ready to utilize the CPU. SO it is possible for several jobs to share the time of the CPU. But it is important to note that multiprogramming is not defined to be the execution of
base, subdividing the transient area to hold several independent programs and adding resource management routines to the operating system's basic functions.
STORAGE HIERARCHY The wide variety of storage systems in a computer system can be organized in a hierarchy according to their speed and their cost. The higher levels are expensive but fast. As we move down the hierarchy, the cost per bit decreases, whereas the access time increases. The reason for using the slower memory decives is that they are cheaper than the faster ones. Many early storage devices, including paper tape and core memories, are found only in museums now that magnetic tape and semiconductor memory have become faster and cheaper. The following figure shows the storage-device hierarchy.
Storage-device hierarchy
Dynamic Loading In dynamic loading, a routine of a program is not loaded until it is called by the program. All routines are kept on disk in a re-locatable load format. The main program is loaded into memory and is executed. Other routines methods or modules are loaded on request. Dynamic loading makes better memory space utilization and unused routines are never loaded.
Dynamic Linking
Linking is the process of collecting and combining various modules of code and data into a executable file that can be loaded into memory and executed. Operating system can link
system level libraries to a program. When it combines the libraries at load time, the linking is called static linking and when this linking is done at the time of execution, it is called as dynamic linking.
In static linking, libraries linked at compile time, so program code size becomes bigger whereas in dynamic linking libraries linked at execution time so program code size remains smaller.
Logical versus Physical Address Space
An address generated by the CPU is a logical address whereas address actually available on memory unit is a physical address. Logical address is also known a Virtual address. Virtual and physical addresses are the same in compile-time and load-time address-binding schemes. Virtual and physical addresses differ in execution-time address-binding scheme.
The set of all logical addresses generated by a program is referred to as a logical address space. The set of all physical addresses corresponding to these logical addresses is referred to as a physical address space.
The run-time mapping from virtual to physical address is done by the Memory Management Unit (MMU) which is a hardware device. MMU uses following mechanism to convert virtual address to physical address.
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of main memory to a backing store, and then brought back into memory for continued execution. Backing store is a usually a hard disk drive or any other secondary storage which fast in access and large enough to accommodate copies of all memory images for all users. It must be capable of providing direct access to these memory images.
available. If a new hole is adjacent to other holes, we merge the adjacent holes to form a larger one.
Selection of a hole to hold a process can follow the following algorithms:
Physical memory Best fit^
Worst fit
First fit
Simulations have shown that both first-fit and best-fit are better than worst-fit in terms of decreasing both time and storage utilization. Neither first-fit nor best-fit is clearly better in terms of storage utilization, but first-fit is generally faster.
External fragmentation exists when enough total memory space exists to satisfy a request, but it is not contiguous. In other words, there are a number of small holes none of which is large enough to satisfy a request, but the sum of their sizes is greater than or equal to the request size. Statistical analysis of first-fit reveals that, even with some optimization, given N allocated blocks, another 0.5N blocks will be lost due to fragmentation. This property is known as the 50- percent rule.
The problem of fragmentation can be solved by compaction. The goal is to shuffle the memory contents to place all free memory together in one large block. For a relocated process to be able to execute in its new location, all internal addresses (e.g., pointers) must be relocated. If the relocation is static and is done at assembly or load time, compaction cannot be done. Compaction is possible only if relocation is dynamic and is done at execution time. If addresses are relocated dynamically, relocation requires only moving the program and data, and then changing the base register to reflect the new base address.
There may be many compaction algorithms:
A. Simply move all processes toward one end of the memory; all holes move in the other direction producing one large hole of available memory.
B. Create a large hole big enough anywhere to satisfy the current request.
Swapping can be combined with compaction. A process can be rolled out and then rolled in again. When the process is to be rolled in again, if static relocation is used, the process must be rolled into the exact memory locations it occupied before it was rolled out. Otherwise, if dynamic relocation (such as with base and limit registers) is used, then a process can be rolled into different memory locations, so that the holes are compacted together.
Internal Fragmentation
Consider a hole of 2002 bytes. And let us say this is the only hole available at the moment. Suppose the next process requests 2000 bytes. If we allocate exactly the requested block, we are left with a hole of 2 bytes. The overhead to keep track of this hole will be substantially larger
The page size which is defined by the hardware is typically a power of 2 varying between 512 bytes and 8192 bytes. If the size of a logical address space is 2m, and a page size is 2n addressing units (bytes or words), then the high-order m-n bits of a logical address designate the page number, and the n low-order bits designate the page offset. Thus the logical address is as follows: page number page offset
m-n n
Paging eliminates external fragmentation altogether but there may be a little internal fragmentation. Following figure show the paging table architecture.
The logical addresses produced by the user program are translated into physical addresses. This translation is controlled by the operating system. When a process arrives to be executed, its size, expressed in pages is examined. Each user page needs one frame. If the required numbers of frames are available, they are allocated to this process. The first page of the process is loaded into one of the allocated frames, and the frame number is put into the page table for this process. The next page is loaded into another frame, and the frame number is similarly put into the page table and so on.
To keep track of the frames which are free and which are not, the operating system maintains a frame table, which has an entry for each of the physical frames. The entry for a frame tells whether this frame is allocated or free and, if it is allocated, to which page of which process or processes. The operating system maintains a copy of the page table for each process, just as it maintains a copy of the instruction counter and register counters. This copy is used to translate
p
logical addresses to physical addresses whenever the operating system must map a logical address to a physical address manually. It is also used by the CPU dispatcher to define the hardware page table when a process is to be allocated the CPU.
A page is a sequence of N bytes where N is a power of 2. These days, page sizes are at least 4K in size and maybe as large as 64 K or more. Let's assume that we have 1M of RAM. RAM is also called physical memory. We can subdivide the RAM into 4K pages. Thus 1M / 4K = 256 pages. Thus, our RAM has 256 physical pages, we are holding 4K.
Let's assume we have 10 M of disk. Thus, we have 2560 disk pages. In principle, each program may have up to 4 G of address space. Thus, it can, in principle, access 2^20 virtual pages. In reality, many of those pages are considered invalid pages.
Multilevel Paging
Most computer systems support a very large logical address space (2^32 to 2^64 ). In such a case, the page table itself becomes very very large. E.g., consider a 32-bit logical address space. If the page size is 4K bytes (2^12 ), then a page table may consist of up to (2^32 / 2^12 ) = 1 million entries. If each entry consists of 4 bytes, each process may need 4 megabytes of physical address alone for the page table. We would not want to allocate the page table contiguously in memory.
One way of getting around this problem is to use a two-level paging scheme, in which the page table itself is also paged. Considering the above example, the logical address could be now divided into a page number consisting of 20 bits, and an offset of 12 bits. This page number could again be divided into a 10-bit page number and a 10-bit offset.
Disadvantages:
Inverted Page Table
To overcome the disadvantage of page tables given above, an inverted page table could be used. An inverted page table has one entry for each (frame) of memory. Each entry consists of the logical (or virtual) address of the page stored in that memory location, with information about
is made on logical address, the whole table may need to be searched. To overcome this, we may use a hash table on the inverted page table to limit the search. To improve performance even more, we can use associative memory registers to hold recently located entries. These registers are searched first before the hash table is searched.
Protection
Memory protection in a paged environment is accomplished by protection bits that are associated with each frame. Normally, they are kept in the page table. One bit can define a page to be read-and-write or read-only.
Shared Pages
Another advantage of paging is the possibility of sharing common code. Consider a system that supports 40 users, each of which executes a text editor. If the text editor consists of 150K of code and 50K of data space, then we would need 8000K to support the 40 users. If the code is reentrant (non-self-modifying), then it can be shared between the 40 users.
SEGMENTATION
With paging, the user’s view of memory is separated from the actual physical memory. The user’s view of memory is mapped into physical memory. The user or programmer does not think of memory as a linear array of bytes. Rather the user prefers to view memory as a collection of variable-sized segments, with no necessary ordering among segment.
Segmentation is a memory-management scheme that suggests that a logical address space be divided into a collection of segments. Each segment has a name and a length. Addresses
specify both the segment name and the offset within the segment. The user therefore specifies each address by two quantities: a segment name (or segment number) and an offset.
Therefore logical addresses consist of two tuples:
<segment-number, offset>.
Normally, the compiler automatically constructs segments reflecting the input program. A Pascal compiler might create separate segments for (1) the global variables; (2) the procedure call stack, to store parameters and return addresses; (3) the code portion of each procedure or function. A FORTRAN compiler might create a separate segment for each common block. Arrays might be assigned separate segments. The loader would assign segment numbers to all these.
Segmentation Hardware
The difference between paged-segmentation and pure segmentation is that the segment-table entry contains not the base address of the segment, but rather the base address of a page table for this segment. There is now a separate page table for each segment. And because of introducing paging there may be the problem of internal fragmentation while external fragmentation is completely eliminated.
Over-allocating will show up in the following way. While a user process is executing, a page fault ocuures. The hardware traps to the operating system, which checks its internal table to that this is a page fault and not an illegal memory access. The operating system finds out where the page is in disk but does not find a free frame on the free-frame list; all memory is in use. At this point a technique called page replacement could be used.
The basic approach of page replacement is: If no frame is free, we find one which is currently being used and free it. We can free a frame by writing its contents to swap space, and changing the page table and all other tables. The freed frame can now be used to hold the page for which the process faulted.
If no frames are free, two page transfers (one out and one in) are required. This effectively doubles the page-fault service time and will increase the effective access time accordingly. This overhead can be reduced by the use of a modify (dirty) bit. Each page may have a modify bit associated with ir in the hardware. The modify bit for a page is set by the hardware whenever any word or bytes in the page is modified. When we select a page for replacement, we examine it’s modify bit. If it is set, it means the page has been modified since it was read from the disk. In this case, we must write that page to the disk. If the modify bit is not set, if the copy of this page
on the disk has not been overwitten (by some other page, for example), we can avoid writing the memory page to the disk.
PAGE REPLACEMENT ALGORITHMS
There are many page-replacement algorithms. We select a particular replacement algorithm based on the one with the lowest page-fault rate.
An algorithm is evaluated by running it on a particular string of memory references (called a reference string) and computing the number of page-faults. Reference strings are generated artificially (by a random-number generator, for example) or by tracing a given system and recording the address of each memory reference. For simplicity we consider only the page number of each address. And if we have a reference to page p, then any immediately following references to page p will never cause a page fault.
E.g., if we trace a particular process, we might record the following address sequence 0100, 0432, 0101, 0612, 0102, 0103, 0104, 0101, 0611, 0102, 0103 which, at 100 bytes per page, is reduced to the following reference string 1, 4, 1, 6, 1, 6, 1
FIFO Algorithm When a page must be replaced, the oldest page in the memory is chosen. This can be implemented by using a FIFO queue. The queue holds all the pages in the memory. We replace the page at the head of the queue. When a page is brought into memory, we insert it at the tail of the queue. Reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 With number of frames = 3, the number of page faults is 15.
Because of the above disadvantage, this algorithm is mainly used for comparison studies, i.e., to see whether some algorithm can perform as good as the optimal algorithm.
3. LRU (Least Recently Used) Algorithm
Replace the page that has not been used for the longest period of time.
For the same test set, the number of page faults produced by this algorithm is 12. This algorithm is considered to be quite good and is often used. It can be implemented in two ways:
The LRU algorithm does not suffer from Belady’s anomaly. There is a class of algorithms called stack algorithms that can never exhibit Belady’s anomaly. A stack algorithm is one for which it can be shown that the set of pages in memory for n frames is always a subset of the set of pages that would be in memory with n+1 frames. For the LRU algorithm, the set of pages in memory would be the n most recently used pages. If the number of frames is increased, these n pages will still be the most recently referenced and so will still be in the memory.
Counting Algorithms: These types of algorithms keep a counter to count the number of references that have been made to each page.
4. LFU (Least Frequently Used) Algorithm
Replace the least frequently used page Disadvantages : This algorithm suffers from the situation in which a page is used heavily during the initial phase of a process, but then is never used again. Since it was used heavily, it has a large count and remains in memory even though it is no longer needed.
5. MFU (Least Frequently Used) Algorithm
Replace the most frequently used page
This algorithm is based on the argument that the page with the smallest count was probably just brought in and has yet to be used. Neither MFU nor LFU replacement is common. Their implementation is fairly expensive.
VIRTUAL MEMORY
Virtual memory is a technique that allows the execution of processes that may not be completely in memory. In many cases, in the course of execution of a program, some part of the program may never be executed. E.g., code in programs that handle unusual error conditions will not be executed (needed) most of the time. And even if the whole program is needed, it may not all be needed at the same time.
The advantages of virtual memory are: