Top Banner
CSCI 3431: OPERATING SYSTEMS Chapter 8 – Main Memory (Pgs 315 - 350)
26

Chapter 8 – Main Memory (Pgs 315 - 350). Overview Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Jan 02, 2016

Download

Documents

Rhoda Dixon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

CSCI 3431: OPERATING SYSTEMS

Chapter 8 – Main Memory (Pgs 315 - 350)

Page 2: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Overview

Everything to do with memory is complicated by the fact that more than 1 program can be in memory at a time

A. Program may not be at address 00000000h (linking/loading issues)

B. Virtual memory may be used (mapping/paging issues)

C. Components may be duplicated (caching issues)

Page 3: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Review - Hardware

Each process has a base address (start) and a limit (range) to specify its accessible addresses

The base and limit are held in registers

Trying to access memory outside of the allowable address space generates a "segmentation fault"

Only applies to user mode, kernel mode can access any address

Page 4: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Binding

Linking a symbolic address in a program to an actual memory location in main memory

Absolute Code: The compiler actually puts physical addresses in the program

Relocatable Code: The compiler uses offsets from some base which is determined at load time and the addresses are inserted by the loader

Both of these are limited, modern OSs use virtual addressing instead

Page 5: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Linking & Loading(Figure 8.3)

Page 6: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Address Translation

Logical (Virtual) Address: What actually is in the compiled program

Physical Address: An actual main memory location

Goal: Map (at run-time) the logical/virtual address space to the physical address space

Done by the OS's MMU (Memory Management Unit)

Page 7: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Dynamic Relocation

Virtual addresses are offsets Relocation register provides the base

address and is set when the program is loaded

MMU adds offset to base to get physical address

Page 8: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Dynamic Loading

Lets load program parts and pieces as they are needed

Speeds up initial loading Saves memory by not loading

unneeded parts Program can be bigger than main

memory if only the parts currently in use are loaded

Can be augmented with dynamic linking

Page 9: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Swapping

When we run out of main memory, we can temporarily store some program parts in a backing store (on disk) and thus free some memory space

When we need them again they are copied back into main memory

This movement between main memory and disk is called swapping

May need to be swapped back to the same location if dynamic relocation not used

Variants of swapping are widely used in OSs

Page 10: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Contiguous Memory Allocation Obsolete Simple, but limited Good for RTOS and Embedded OS if

the running applications change rarely and have known sizes

Treats entire program as a single entity

Can waste a lot of memory

Page 11: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Partitions

A section of memory that holds one process

Fixed Size: Process must fit into partition and extra space is unused

Variable Size: Partition is sized to process, but partitions must be fit together into memory : "dynamic storage allocation"

Page 12: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Allocation Strategies

1. First Fit2. Best Fit3. Worst Fit First fit is generally faster than Best fit and

produces similar results Worst fit is generally a bad idea First fit tends to waste about 50% of the

available memory due to fragmentation Compaction (i.e., defragmentation of main

memory) is slow but sometimes necessary

Page 13: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Paging

What OSs really do Avoids fragmentation, improves swapping Recent advances involve greater coupling /

cooperation between hardware and OS Physical memory is broken into fixed sized

blocks called frames Logical memory is broken in blocks of the

same size called pages Each page is put into a frame Only part of the last page is wasted Fixed sizes make allocation trivial

Page 14: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Figure 8.7

Page 15: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Basic Paging

Virtual Address = Page + Offset Page Table holds mapping of pages to

frames Lookup page to find frame base address Apply offset Page size is usually configurable to suit OS

usage Number of pages is defined by address bus

size Number of frames = memory size / page size

Page 16: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Examining Page Size

#include <stdio.h>

#include <windows.h>

int main(void) {

SYSTEM_INFO si;

GetSystemInfo(&si);

printf(

"The page size for this system is %u bytes.\n",

si.dwPageSize);

return 0;

}

Page 17: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Fragmentation

Maximum wasted space for a process is the page size minus one byte

Average wasted space for a process is half a page

Some systems use more than one page size

Most systems support (but rarely use) Huge Pages

Pages sizes growing as memory and process sizes are also increasing

Page 18: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Tables

Page Table: Maps a program page to a frame for address translation , usually there is a page table for each process, managed and held by the OS

Frame Table: Keeps track of how main memory is used (free/available), usually one frame table managed by the OS

Page 19: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Hardware Support

Efficiency of address translation is critical for good system performance

PTBR: Page Table Base Register – holds address (for this process) of the page table's location in main memory

TLB : Translation Lookaside Buffer – a cache of recently performed Page->Frame translations, small but fast!

ASID: Address Space IDentifier – permits TLB to be used for multiple processes, otherwise TLB must be flushed on context switch

Page 20: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Page Table Entries

Often store more than just the frame number

Valid Bit: Is this page part of the program (permits fixed sized page tables)

Use of a Valid Bit can be avoided by using a Page Table Length Register

Dirty Bit: Has this page been modified (saves needed to swap a page to disk)

Page 21: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Shared Pages

Re-entrant code is code that is never modified and can be used by several processes

Only need to load into memory once Most OSs do this to save

space/frames

Page 22: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Hierarchical Paging 64 bit address space, broken into

4KB blocks = HUGE page tables 12 Bits needed for 4 KB page/frame

(52bits for the page number!) Break it down into a tree of page

tables

Page 23: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Other Page Table Formats

Hash Tables: Use "Page" as input to a hash function to find the "bucket" of frames for that value (See Fig. 8.16)

Inverted Page Tables (See Fig. 8.17) One entry per frame One table for entire system Page + PID = Search Key Slow lookup, shared memory difficult

Page 24: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Segmentation

All pages are not really the same Code, shared code, data, shared data, heap data,

stack data, ... Divide program into segments that match

these differences Could then use some part of the address to

identify the segment and the rest for the address within the segment

Map to physical memory with a Segment Table Use hardware support: Segment Base,

Segment Limit

Page 25: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

Intel Chips (32 Bit)

Have both paging and segmentation support

Segmentation is mostly hardware based

Paging is mostly OS (software) based Pages either 4KB or 4 MB

Page 26: Chapter 8 – Main Memory (Pgs 315 - 350). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.

To Do:

Begin Assignment 2 in Lab Read Chapter 8 (pgs 315-350; this

lecture) Start reading Chapter 9 (next

lecture)