silberschatz and galvin 1999 8.1 operating system concepts module 8: memory management background...

41
Operating Silberschatz and Galvin1999 8.1 Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

Upload: kimberly-wiggins

Post on 03-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.1

Module 8: Memory Management

• Background

• Logical versus Physical Address Space

• Swapping

• Contiguous Allocation

• Paging

• Segmentation

• Segmentation with Paging

Page 2: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.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.

Page 3: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Why Memory management ?

• To ensure protection of different processes from each other (so that they do not interfere with each other’s operations).

• To place the programs in memory (such that memory is utilized to its fullest extent.

Page 4: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.4

Binding of Instructions and Data to Memory

• Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes.

• Load time: Must generate relocatable code if memory location is not known at compile time.

• Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers).

Address binding of instructions and data to memory addresses canhappen at three different stages.

Page 5: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.5

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.

Page 6: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.6

Dynamic Linking

• Linking postponed until execution time.

• Small piece of code, stub, used to locate the appropriate memory-resident 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.

Page 7: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.7

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.

• Implemented by user, no special support needed from operating system, programming design of overlay structure is complex

Page 8: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.8

Logical vs. Physical Address Space

• The concept of a logical address space that is bound to a separate physical address space is central to proper memory management.

– Logical address – generated by the CPU; also referred to as virtual address.

– Physical address – address seen by the memory unit.

• 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.

Page 9: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.9

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 10: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Relocation register

Relocation register is a special register in the CPU used for program relocation means mapping of logical addresses used by the program to physical addresses of the system’s main memory

Page 11: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Program relocation

Page 12: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.12

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 and Microsoft Windows.

Page 13: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.13

Schematic View of Swapping

Page 14: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Storage Allocation and Management Techniques

The Storage allocation can be of two types:

(i) Contiguous storage allocation.

(ii) Non-contiguous storage allocation.

Page 15: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

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.

Page 16: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Contiguous Storage Allocation

► Contiguous storage allocation implies that a program’s data and instructions are assured to occupy a single contiguous memory area.

► It is further subdivided into Fixed-partition storage allocation strategy and variable-partition storage allocation strategy.

Page 17: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.17

Contiguous Allocation (Cont.)

• Multiple fixed partition allocation

– Hole – 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: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.18

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 entier 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: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

• Problems

• Internal fragmentation

• External fragmentation

Page 20: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.20

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.

Page 21: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.21

Multiple variable partition allocation

• Memory only divide into to tow area

1- OS area

2- user area

See example :

• Problem is has external fragmentation but minimum internal fragmentation

• Reduce external fragmentation by compaction

Shuffle memory contents to place all free memory together in one large block.

Page 22: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.22

PagingPaging

• Logical address space of a process can be noncontiguous; Logical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is process is allocated physical memory whenever the latter is available.available.

• Divide physical memory into fixed-sized blocks called frames Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 8192 bytes).(size is power of 2, between 512 bytes and 8192 bytes).

• Divide logical memory into blocks of same size called pages.Divide logical memory into blocks of same size called pages.

• Keep track of all free frames.Keep track of all free frames.

• To run a program of size n pages, need to find To run a program of size n pages, need to find nn free frames free frames and load program.and load program.

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

• Internal fragmentation.Internal fragmentation.

Page 23: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.23

Address Translation Address Translation SchemeScheme

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

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

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

– Suppose Suppose – P : page size , L : logical address P : page size , L : logical address – Page number =L div PPage number =L div P– Page Offset = L mode PPage Offset = L mode P– Physical address = (f-1)*page number +Offset Physical address = (f-1)*page number +Offset

Page 24: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.24

Address Translation Address Translation Architecture Architecture

Page 25: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.25

Paging Example Paging Example

Page 26: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.26

Paging Example Paging Example

Page 27: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.27

It works as:It works as:

Translate logical address 13 = 01101 Translate logical address 13 = 01101 in binaryin binary

Offset = 01 in binary or 1 in decimal Offset = 01 in binary or 1 in decimal

Page no. = 011 in binary or 3 in Page no. = 011 in binary or 3 in decimaldecimal

Physical address = Physical address = pageframe*pagesize + offset i.e. pageframe*pagesize + offset i.e. 2*4+1 = 9 (which is the real address 2*4+1 = 9 (which is the real address of page containing character “n” in of page containing character “n” in the main memory)the main memory)

Page 28: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.28

Memory ProtectionMemory Protection

• Memory protection implemented by Memory protection implemented by associating protection bit with each frame.associating protection bit with each frame.

• Valid-invalidValid-invalid bit attached to each entry in bit attached to each entry in the page table:the page table:

– ““valid” indicates that the associated valid” indicates that the associated page is in the process’ logical address page is in the process’ logical address space, and is thus a legal page.space, and is thus a legal page.

– ““invalid” indicates that the page is not invalid” indicates that the page is not in the process’ logical address space.in the process’ logical address space.

Page 29: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.29

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

Page 30: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.30

Shared PagesShared Pages

• Shared codeShared code– One copy of read-only (reentrant) code One copy of read-only (reentrant) code

shared among processes (i.e., text editors, shared among processes (i.e., text editors, compilers, window systems). compilers, window systems).

– Shared code must appear in same location in Shared code must appear in same location in the logical address space of all processes.the logical address space of all processes.

• Private code and data Private code and data – Each process keeps a separate copy of the Each process keeps a separate copy of the

code and data.code and data.– The pages for the private code and data can The pages for the private code and data can

appear anywhere in the logical address space.appear anywhere in the logical address space.

Page 31: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.31

Shared Pages Shared Pages ExampleExample

Page 32: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.32

SegmentationSegmentation

• Memory-management scheme that supports Memory-management scheme that supports user view of memory. user view of memory.

• A program is a collection of segments. A A program is a collection of segments. A segment is a logical unit such as:segment is a logical unit such as:

main program,main program,procedure, procedure, function,function,local variables, global variables,local variables, global variables,common block,common block,stack,stack,symbol table, arrayssymbol table, arrays

Page 33: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.33

Segmentation Segmentation Architecture Architecture

• Logical address consists of a two tuple:Logical address consists of a two tuple:

<segment-number, offset>,<segment-number, offset>,

• Segment tableSegment table – maps two-dimensional physical – maps two-dimensional physical addresses; each table entry has:addresses; each table entry has:

– base – contains the starting physical address base – contains the starting physical address where the segments reside in memory.where the segments reside in memory.

– limitlimit – specifies the length of the segment. – specifies the length of the segment.

• Segment-table base register (STBR)Segment-table base register (STBR) points to points to the segment table’s location in memory.the segment table’s location in memory.

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

Page 34: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.34

User’s View of a User’s View of a ProgramProgram

Page 35: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.35

Logical View of SegmentationLogical View of Segmentation

1

3

2

4

1

4

2

3

user space physical memory space

Page 36: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.36A. Frank - P. Weisberg

Address Translation in a Segmentation Address Translation in a Segmentation SystemSystem

Page 37: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.37

Address Translation in a Address Translation in a Segmentation SystemSegmentation System

Page 38: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.38

Example of Example of SegmentationSegmentation

Page 39: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.39

Segmentation Segmentation Architecture (Cont.)Architecture (Cont.)

•Relocation.Relocation.– dynamicdynamic– by segment table by segment table

•Sharing.Sharing.– shared segmentsshared segments– same segment number same segment number

•Allocation.Allocation.– first fit/best fitfirst fit/best fit– external fragmentationexternal fragmentation

Page 40: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.40

Sharing of segmentsSharing of segments

Page 41: Silberschatz and Galvin  1999 8.1 Operating System Concepts Module 8: Memory Management Background Logical versus Physical Address Space Swapping Contiguous

Operating System Concepts

Silberschatz and Galvin1999 8.41A. Frank - P. Weisberg

Address Translation in combined Address Translation in combined Segmentation/PagingSegmentation/Paging