55
Silberschatz, Galvin and Gagne 2002 9.1 Operating System Concepts Chapter 9: Memory Management Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

Chapter 9: Memory Management

  • Upload
    xanthe

  • View
    20

  • Download
    1

Embed Size (px)

DESCRIPTION

Chapter 9: Memory Management. Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging. Background. a program resides on a disk as a binary executable file Program must be brought into memory and placed within a process for it to be run. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.1Operating System Concepts

Chapter 9: Memory Management

Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

Page 2: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.2Operating System Concepts

Background

a program resides on a disk as a binary executable file

Program must be brought into memory and placed within a process for it to be run.

As the process is executed, it accesses instructions and data from memory. Eventually, the process terminates, and its memory space is declared available.

Input queue – collection of processes on the disk that are waiting to be brought into memory to run the program.

User programs go through several steps before being run.

Page 3: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.3Operating System Concepts

Binding of Instructions and Data to Memory

Compile time: If you know at compile time where the process will reside in memory, then absolute code can be generated. For example, if you know a priori that a user process resides starting at location R, then the generated compiler code will start at that location and extend up from there. If, at some later time, the starting location changes, then it will be necessary to recompile this code.

Classically, the binding of instructions and data to memory addresses can happen at three different stages.

Page 4: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.4Operating System Concepts

Binding of Instructions and Data to Memory (cont…)

Load time: If it is not known at compile time where the process will reside in memory, then the compiler must generate relocatable code. In this case, final binding is delayed until load time. If the starting address changes, we need only to reload the user code

Execution time: If the process can be moved during its execution from one memory segment to another, then binding must be delayed until run time.

Classically, the binding of instructions and data to memory addresses can happen at three different stages. (Cont…)

Page 5: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.5Operating System Concepts

Multistep Processing of a User Program

Page 6: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.6Operating System Concepts

Logical vs. Physical Address Space

An address generated by the CPU is commonly referred to as a logical address, whereas an address seen by the memory unit-that is, the one loaded into the memory-address register of the memory-is commonly referred to as a physical address.

The compile-time and load-time address-binding methods generate identical logical and physical addresses. However, the execution-time address binding scheme results in differing logical and physical addresses. In this case, we usually refer to the logical address as a virtual address.

Page 7: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.7Operating System Concepts

Memory-Management Unit (MMU)

Hardware device that maps virtual to physical address.

In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory.

The user program deals with logical addresses; it never sees the real physical addresses.

Page 8: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.8Operating System Concepts

Dynamic relocation using a relocation register

Page 9: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.9Operating System Concepts

Dynamic Loading In our discussion so far, the entire program and data of a process

must be in physical memory for the process to execute.. To obtain better memory-space utilization, we can use dynamic loading. With dynamic loading, a routine is not loaded until it is called.

All routines are kept on disk in a relocatable load format. The main program is loaded into memory and is executed. When a routine needs to call another routine, the calling routine first checks to see whether the other routine has been loaded. If not, the relocatable linking loader is called to load the desired routine into memory and to update the program's address tables to reflect this change. Then, control is passed to the newly loaded routine

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.

Page 10: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.10Operating System Concepts

Dynamic Linking

Some operating systems support only static linking, in which system language libraries are treated like any other object module and are combined by the loader into the binary program image.

In dynamic linking the Linking postponed until execution time.

With dynamic linking, a stub is included in the image for each library-routine reference. This stub is a small piece of code that indicates how to locate the appropriate memory-resident library routine, or how to load the library if the routine is not already present. 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.

Page 11: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.11Operating System Concepts

Overlays

Keep in memory only those instructions and data that are needed at any given time.

Needed when process is larger than amount of memory allocated to it.

Page 12: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.12Operating System Concepts

Overlays for a Two-Pass Assembler

Page 13: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.13Operating System Concepts

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.

Page 14: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.14Operating System Concepts

Schematic View of Swapping

Page 15: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.15Operating System Concepts

Contiguous Allocation

Main memory usually into two partitions:Resident operating system, usually held in low memory .User processes then held in high memory.

Single-partition allocationRelocation-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.

Page 16: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.16Operating System Concepts

Hardware Support for Relocation and Limit Registers

Page 17: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.17Operating System Concepts

Contiguous Allocation (Cont.)

Multiple-partition allocationHole – block of available memory; holes of various size

are scattered throughout memory.When a process arrives, it is allocated memory from a

hole large enough to accommodate it.Operating system maintains information about:

a) allocated partitions b) free partitions (hole)

OS

process 5

process 8

process 2

OS

process 5

process 2

OS

process 5

process 2

OS

process 5

process 9

process 2

process 9

process 10

Page 18: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.18Operating System Concepts

Dynamic Storage-Allocation Problem

First-fit: Allocate the first hole that is big enough. Best-fit: Allocate the smallest hole that is big enough;

must search entire list, unless ordered by size. Produces the smallest leftover hole.

Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.

How to satisfy a request of size n from a list of free holes.

First-fit and best-fit better than worst-fit in terms of speed and storage utilization.

Page 19: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.19Operating System Concepts

Fragmentation

External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous.

Internal Fragmentation – The general approach is to break the physical memory into fixed-sized blocks, and allocate memory in unit of block sizes. With this approach, 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.

Page 20: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.20Operating System Concepts

Paging

Paging is a memory-management scheme that permits the physical-address space of a process to 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 16 MB per page).

Divide logical memory into blocks of same size called pages. Keep track of all free frames. To run a program of size n pages, need to find n free frames and

load program. From the backing store. The backing store is divided into fixed-sized blocks that are of the same size as the memory frames.

Set up a page table to translate logical to physical addresses.

Page 21: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.21Operating System Concepts

Address Translation Scheme

Every Address generated by CPU is divided into:Page number (p) – used as an index into a

page table which contains base address of each page in physical memory.

Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit.

Page 22: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.22Operating System Concepts

Address Translation Architecture

Page 23: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.23Operating System Concepts

Paging Example

Page 24: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.24Operating System Concepts

logical address

where p is an index into the page table and d is the displacement within the page.

An important aspect of paging is the clear separation between the user's view of memory and the actual physical memory.

The user program views that memory as one single contiguous space, containing only this one program. In fact, the user program is scattered throughout physical memory, which also holds other programs.

Page 25: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.25Operating System Concepts

Paging Example

consider the memory in Figure 9.8. Using a page size of 4 bytes and a physical memory of 32 bytes (8 pages), we show how the user's view of memory can be mapped into physical memory.

Logical address 0 is page 0, offset 0. Indexing into the page table, we find that page 0 is in frame 5. Thus, logical address 0 maps to physical address 20 (= (5 x 4) + 0).

Logical address 3 (page 0, offset 3) maps to physical address 23 (= (5 x 4) + 3). Logical address 4 is page 1, offset 0;

according to the page table, page 1 is mapped to frame 6. Thus, logical address 4 maps to physical address 24 (= (6 x 4) + 0). Logical address 13 maps to physical address 9.

Page 26: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.26Operating System Concepts

Paging Example

Page 27: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.27Operating System Concepts

Free Frames

Before allocation After allocation

Page 28: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.28Operating System Concepts

Implementation of Page Table

Page table is kept in main memory. 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. One for the page table and 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 or translation look-aside buffers (TLBs)

Page 29: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.29Operating System Concepts

TLB

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, it is compared with

all keys simultaneously. If the item is found, the corresponding value field is returned.

The search is fast; the hardware, however, is expensive. 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.

If the page number is not in the TLB (known as a TLB miss), a memory reference to the page table must be made. When the frame number is obtained, we can use it to access memory .

In addition, we add the page number and frame number to the TLB, so that they will be found quickly on the next reference. If the TLB is already full of entries, the operating system must select one for replacement.

Replacement policies range from least recently used (LRU) to random.

Page 30: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.30Operating System Concepts

Paging Hardware With TLB

Page 31: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.31Operating System Concepts

Effective Access Time

The percentage of times that a particular page number is found in the TLB is called the hit ratio.

An 80-percent hit ratio means that we find the desired page number in the TLB 80 percent of the time.

If it takes 20 nanoseconds to search the TLB, and 100 nanoseconds to access memory, then a mapped memory access takes 120 nanoseconds when the page number is in the TLB.

If we fail to find the page number in the TLB (20 nanoseconds), then we must first access memory for the page table and frame number (100 nanoseconds), and then access the desired byte in memory (100 nanoseconds), for a total of 220 nanoseconds.

To find the effective memory-access time, we must weigh each case by its probability:

effective access time = 0.80 x 120 + 0.20 x 220 = 140 nanoseconds 40-percent slowdown in memory access time (from 100 to 140 nanoseconds)..

Page 32: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.32Operating System Concepts

Memory Protection

Normally, protection bits are kept in the page table. One bit can define a page to be read-write or read-only. Every reference to memory goes through the page table to find the correct frame number. At the same time that the physical address is being computed, the protection bits can be checked to verify that no writes are being made to a read-only page..

One more bit is generally attached to each entry in the page table: a validinvalid bit. When this bit is set to "valid," this value indicates that the associated page is in the process' logical-address space, and is thus a legal (or valid) page.

If the bit is set to "invalid," this value indicates that the page is not in the process' logical-address space. Illegal addresses are trapped by using the validinvalid bit.

The operating system sets this bit for each page to allow or disallow accesses to that page.

Page 33: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.33Operating System Concepts

Valid (v) or Invalid (i) Bit In A Page Table

Page 34: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.34Operating System Concepts

Page Table Structure

Hierarchical Paging

Hashed Page Tables

Inverted Page Tables

Page 35: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.35Operating System Concepts

Hierarchical Page Tables

Break up the logical address space into multiple page tables.

A simple technique is a two-level page table.

Page 36: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.36Operating System Concepts

Two-Level Paging Example

A logical address (on 32-bit machine with 4K page size) is divided into: a page number consisting of 20 bits. a page offset consisting of 12 bits.

Since the page table is paged, the page number is further divided into: a 10-bit page number. a 10-bit page offset.

Thus, a logical address is as follows:

where pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table.

page number page offset

pi p2 d

10 10 12

Page 37: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.37Operating System Concepts

Two-Level Page-Table Scheme

Page 38: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.38Operating System Concepts

Address-Translation Scheme

Address-translation scheme for a two-level 32-bit paging architecture

Page 39: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.39Operating System Concepts

Inverted Page Table

One entry for each real page of memory. Entry consists of the virtual address of the page stored in that real

memory location, with information about the process that owns that page. Thus, only one page table is in the system, and it has only one entry for each page of physical memory. When a memory reference occurs, part of the virtual address,

consisting of <process-id, pagenumber>, is presented to the memory subsystem. The inverted page table is then searched for a match. If a match is found-say, at entry i-then the physical address <i, offset> is generated. If no match is found, then an illegal address access has been attempted.

Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs. Because the inverted page table is sorted by a physical

address, but lookups occur on virtual addresses, the whole table might need to be searched for a match.

Page 40: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.40Operating System Concepts

Inverted Page Table Architecture

Page 41: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.41Operating System Concepts

Shared Pages

Another advantage of paging is the possibility of sharing common code.

Shared codeOne copy of read-only 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.

Page 42: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.42Operating System Concepts

Shared Pages

Consider a system that supports 40 users, each of whom executes a text editor.

If the text editor consists of 150 KB of code and 50 KB of data space, we would need 8,000 KB to support the 40 users. If the code is not shared, however, it can be shared, as shown in Figure 9.16. Here we see a three-page editor-each page of size 50 KB;

the large page size is used to simplify the figure-being shared among three processes. Each process has its own data page.

Page 43: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.43Operating System Concepts

Shared Pages Example

Page 44: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.44Operating System Concepts

Segmentation

Memory-management scheme that supports user view of memory. Consider how you think of a program when you are writing it. You

think of it as a main program with a set of subroutines, procedures, functions, or modules.

There may also be various data structures: tables, arrays, stacks, variables, and so on.

Each of these modules or data elements is referred to by name. You talk about "the symbol table," "function " "the main program,“ without caring what addresses in memory these elements occupy.

You are not concerned with whether the symbol table is stored before or after the function. Each of these segments is of variable length;

Page 45: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.45Operating System Concepts

Segmentation

Segmentation is a memory-management scheme that supports this user view of memory.

A logical-address space is a collection of segments. Each segment has a name and a length.

The addresses specify both the segment name and the offset within the segment.

The user therefore specifies each address by two quantities: a segment name and an offset.

Page 46: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.46Operating System Concepts

User’s View of a Program

Page 47: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.47Operating System Concepts

Logical View of Segmentation

1

3

2

4

1

4

2

3

user space physical memory space

Page 48: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.48Operating System Concepts

Segmentation Architecture

For simplicity of implementation, segments are numbered, 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 table’s location in memory.

Segment-table length register (STLR) indicates number of segments used by a program;

segment number s is legal if s < STLR.

Page 49: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.49Operating System Concepts

Segmentation Architecture (Cont.)

Sharing. shared segments same segment number

Allocation. first fit/best fit external fragmentation

Page 50: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.50Operating System Concepts

Segmentation Architecture (Cont.)

Protection. With each entry in segment table associate: validation bit = 0 illegal segment

read/write/execute privileges Protection bits associated with segments; code sharing occurs

at segment level. Since segments vary in length, memory allocation is a dynamic

storage-allocation problem. A segmentation example is shown in the following diagram

Page 51: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.51Operating System Concepts

Segmentation Hardware

Page 52: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.52Operating System Concepts

Example of Segmentation

Page 53: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.53Operating System Concepts

Sharing of Segments

Page 54: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.54Operating System Concepts

Segmentation with Paging – MULTICS

The MULTICS system solved problems of external fragmentation and lengthy search times by paging the segments.

Solution differs from pure segmentation in 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.

Page 55: Chapter 9:  Memory Management

Silberschatz, Galvin and Gagne 20029.55Operating System Concepts

MULTICS Address Translation Scheme