Chapter 8: Memory Management
Chapter 8: Memory Management
[SGG7] Chapter 8
Main Memory
Provide a description of organizing memory hardware Introduce memory management techniques: paging and segmentation
Copyright Notice: The lecture notes are mainly based on Silberschatzs, Galvins and Gagnes book (Operating System Concepts, 7th ed., Wiley, 2005). No part of the lecture notes may be reproduced in any form, due to the copyrights reserved by Addison-Wesley. These lecture notes should only be used for internal teaching purposes at the Linkping University. Andrzej Bednarski, IDA Linkpings universitet, 2005
7.2
Background
Program must be brought into memory and placed within a process for it to be executed. Input queue collection of processes on the disk that are waiting to be brought into memory for execution. User programs go through several steps before being executed.
7.3
7.4
7.5
7.6
7.7
7.8
Dynamic Loading
Routine is not loaded until it is called Better memory-space utilization; unused routine is never loaded Useful when large amounts of code are needed to handle infrequently occurring cases No special support from the operating system is required (implemented through program design)
Dynamic Linking
Linking postponed until execution time Small piece of code, stub, used to locate the appropriate memoryresident library routine Stub replaces itself with the address of the routine, and executes the routine Operating system needed to check if routine is in processes memory address Dynamic linking is particularly useful for libraries
7.9
7.10
Swapping
A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution Backing store fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images Roll out, roll in swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows)
Swapping (Cont.)
Normally, a swapped out process will be swapped back into the same memory space it occupied previously. + Reason: dictated by address binding method. Binding at load or assembly time, then processes cannot be moved! Execution time binding: processes can be moved. Swapping is costly + E.g., user size of a process: 10 Mbyte; Disk performance: 40 Mbyte/sec Transfer time: 10 Mbyte/40Mbyte/sec = 250 ms Average latency: 8 ms Seek time: often zero ms Total: 2*(250+8) = 512 ms Quantum when using Round-Robin should be >> 512 milliseconds
7.12 Silberschatz, Galvin and Gagne 2005
7.11
Contiguous Allocation
Main memory usually into two partitions: + Resident operating system, usually held in low memory with interrupt vector + User processes then held in high memory Single-partition allocation + Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data + Relocation register contains value of smallest physical address; limit register contains range of logical addresses each logical address must be less than the limit register + Mapping from logical to physical addresses is done by MMU
7.13
7.14
Basic Hardware
7.15
7.16
7.17
Fragmentation
External Fragmentation total memory space exists to satisfy a request, but it is not contiguous Internal Fragmentation allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Reduce external fragmentation by compaction + Shuffle memory contents to place all free memory together in one large block + Compaction is possible only if relocation is dynamic, and is done at execution time + I/O problem Latch job in memory while it is involved in I/O Do I/O only into OS buffers
7.19 Silberschatz, Galvin and Gagne 2005
Example of Compacting
p1
p2 p3
pn
p4
7.20
p1
p1 p2 p3
p1
p1 p3
p2 p3 pn
p2 p3
p2
pn
p4
p4
p4
p4
7.21
7.22
Paging
Logical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 8192 bytes) Divide logical memory into blocks of same size called pages. Keep track of all free frames: frame table - maintained by OS To run a program of size n pages, need to find n free frames and load program Set up a page table to translate logical to physical addresses Internal fragmentation
7.23 Silberschatz, Galvin and Gagne 2005
7.24
Paging Examples
n offset
7.26 Silberschatz, Galvin and Gagne 2005
#page
Free Frames
Page-table base register (PTBR) points to the page table Page-table length register (PRLR) indicates size of the page table
In this scheme every data/instruction access requires two memory accesses: 1) one for the page table and 2) one for the data/instruction. The two memory access problem can be solved by the use of a special fast-lookup hardware cache called: + associative memory/register or + translation look-aside buffers (TLBs)
Before allocation
After allocation
7.27
7.28
Associative Register
Associative memory parallel search
Page # Frame #
Address translation (A, A) + If A is in associative register, get frame number out + Otherwise get frame number from page table in memory (TLB miss) If so, include frame in associative register (updating)
7.29
7.30
Memory Protection
Memory protection implemented by associating protection bit with each frame. Valid-invalid bit attached to each entry in the page table: + valid : indicates that the associated page is in the process logical address space, and is thus a legal page. + invalid : indicates that the page is not in the process logical address space.
Hit ratio percentage of times that a page number is found in the associative registers (e.g., =80%; normally between 80-98) Effective Access Time (EAT) EAT = ( + ) + (2 + )(1 ) = 120*0.8 + 220*0.2= 140 nanoseconds
7.31
7.32
7.33
7.34
page number
page offset
pi
10
p2
10
d
12
where pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table
7.35
7.36
Address-Translation Scheme
Address-translation scheme for a two-level 32-bit paging architecture
7.37
7.38
7.39
7.40
Shared Pages
Shared code + One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems). + Shared code must appear in same location in the logical address space of all processes Private code and data + Each process keeps a separate copy of the code and data + The pages for the private code and data can appear anywhere in the logical address space
7.41
7.42
Segmentation
Memory-management scheme that supports user view of memory 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,
7.43
7.44
Segmentation Architecture
Logical address consists of a two tuple: <segment-number, offset> Segment table maps two-dimensional physical addresses; each table entry has: + base contains the starting physical address where the segments reside in memory + limit specifies the length of the segment Segment-table base register (STBR) points to the segment tables location in memory Segment-table length register (STLR) indicates number of segments used by a program; segment number s is legal if s < STLR
7.45 Silberschatz, Galvin and Gagne 2005
Segmentation Hardware
7.46
7.47
7.48
Examples of Segmentation
Sharing of segments