Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having...

28
Operating Systems Part IV: Memory Management

Transcript of Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having...

Page 1: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Operating Systems

Part IV: Memory Management

Page 2: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Main Memory

Large array of words or bytes each having its own address

Several processes must be kept in main memory to improve utilization and system response: memory sharing

Memory management algorithms vary from simple approach to paging and segmentation strategies

Page 3: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Memory Basics

Types of memory addresses– Symbolic (e.g. program variables)– Relocatable (e.g. offset + 100)– Absolute (e.g. 255)

Address binding: Which data goes where. May be determined in any of the following phases– Compile time: compiler generates absolute code for

execution in a fixed address– Load time: compiler generates relocatable code, link-

loader computes relocation addresses during link time– Execution time: allows executing processes to move, by

simply changing values of segment registers.

Page 4: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Logical & Physical Addresses

Logical or virtual address – address generated by the CPU.

Physical address – address loaded into the memory-address register of RAM– Compile-time and load-time binding generate

identical logical and physical addresses– Execution-time binding generate differing logical

and physical addresses– Memory management unit – maps virtual to

physical addresses during runtime.

Page 5: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Improving Memory Utilization

Dynamic loading– Subroutines are stored on disk in relocatable format, and

are not loaded until called Dynamic linking

– Linking to subroutine libraries postponed until run-time; stub used as pointer to routine in a shared DLL.

– Conserves disk space – a single copy of library is shared by all executing processes. Versioning issue.

Overlays– Keeps only needed instructions in memory– Other instructions overwrite previously occupied address

when needed. May be implemented by programmer.

Page 6: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Swapping

Used if memory is no longer sufficient

The roll out (swap out to disk) and roll in (swap to MM) drastically increases time for context switch

Page 7: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Swapping

Needs a fast backing store (secondary memory) for efficient implementation

Waiting processes are good candidates to be swapped out to disk

Processes waiting on I/O should not be swapped out, since the I/O could be into their address space, or I/O should be done into OS buffers only.

Page 8: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Swapping

Modified version of swapping used in Unix. Swapping normally disabled. Enabled when memory runs out due to many running processes.In Linux, only R/W data segment needs to be swapped out, R/O code segment are just overwritten, since it can be reread from disk.

Microsoft Windows provides partial swapping - if not enough memory for a new program, current program is swapped out to disk. User determines swap rather than scheduler

Page 9: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Memory Protection

OS must protect itself from user processes, and must protect user processes from each other.

Each process is assigned a relocation register and a limit register.

Relocation register contains the value of the smallest physical address.

Logical address must be less than the limit register

Page 10: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

How memory protection works

Page 11: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Contiguous Allocation

Type of allocation where each process occupies only a single continuous block in main memory

Simple two-partition scheme– Low memory: usually contains resident O/S

since interrupt vectors are here– Rest of memory: for user processes

Page 12: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Contiguous Allocation

Multiple-Partition Algorithm– Simplest is multiple fixed partition

Each partition is allocated to a processMultiprogramming limited to number of partitions

– Dynamic partitioningStarts with one large memory block called a “hole”Processes arrive and are allocated a blockHoles become available as processes terminate

Page 13: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Contiguous Allocation

Multiple-Partition Algorithm– Dynamic Partitioning (cont’d)

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 14: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Contiguous Allocation

Dynamic storage allocation problem– looking for freed memory for waiting processes– set of holes searched to see w/c one to allocate

first-fit - allocate first hole big enough for process (search may start from beginning or end)

best-fit - allocate smallest hole that is big enough worst-fit - allocate largest hole -> produces largest leftover

(sometimes useful than smaller ones)

– first/best better in time/storage utilization, respectively– first-fit is generally faster

Page 15: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Contiguous Allocation

Fragmentation– External

Enough memory exists but not contiguous 50% rule - given N allocated blocks, the next 0.5N blocks will

be lost due to fragmentation (1/3 is unusable) when first-fit is used.

– Internal Allocate memory in fixed-sized units, say 4k blocks. Memory

actually allocated to a process may be slightly larger than requested memory. The difference is internal fragmentation.

Page 16: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Contiguous Allocation

Compaction– A solution to external fragmentation – shuffle memory contents

to place all free memory together in one large block.– Not always possible if relocatable addressing at execution time

is not supported– Algorithm 1: move processes to one end (expensive if many

processes are moved)– Algorithm 2: create big hole in the middle– Swapping may be combined w/ compaction

processes rolled out to backing store then back in– compaction not possible w/ disk due to slow access

Page 17: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Paging

Allows non-contiguous allocation -> solves external fragmentation

Logical memory– Fixed sized blocks called pages

Physical memory– Fixed-sized blocks called frames

Page table converts pages to frames, using address translation hardware– page# + offset -> frame# + offset, using page table

Backing store (swap partition in Linux)– same structure as logical memory

Page 18: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Paging Examples

Page 19: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Internal Fragmentation in Paging

External fragmentation is eliminated but internal fragmentation is not since processes rarely take up all the memory space allocated to them

Worst case:– Process needs (n pages + 1 byte)– Wasted space of (page size bytes – 1 byte)

Page 20: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Page Table Structure

Each O/S has own method for storing page tables -> most allocate a page table per process

Context-switch time increases with paging due to need to store page tables in PCB of each process

Each page table entry might include access type as r/o (constant data), r/w (variable data), or x/o (code), to implement a further level of memory protection

Page table structure: linear, multilevel hierarchical, hashed, inverted.

Page 21: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Shared Pages

Shared read-only/execute-only code– When several instances of a single program are

running, only one copy of the program code is stored in memory, but each instance has its own program data. Code must be reentrant.

Possibility of sharing common code is another advantage of paging

Similar to sharing of address space of a task by threads.

Page 22: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Shared Pages: An Example

Page 23: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Segmentation

Scheme divides logical memory into segments

More intuitive view of memory from user’s point of view (e.g. program divided into segments -- subroutines, procedures, data -- that have different length)

Page 24: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Logical View of Segmentation

1

3

2

4

1

4

2

3

user space physical memory space

Page 25: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Segmentation Hardware

Logical (2-dimensional) vs. physical (1-dimensional) -> mapping effected through a segment table

Entry consists of segment base (=starting address in physical memory) and limit (= length of segment)

Similar to concept of pages except segments do not have fixed length

Intel 80x86 is based on segmentation

Page 26: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Mapping Segments to Physical Memory

Page 27: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Segments and Fragmentation

Segmentation may cause external fragmentation

Happens when all free blocks are too small to accommodate a segment

Compaction may be used to solve the problem

Process may wait if segment cannot be found

Page 28: Operating Systems Part IV: Memory Management. Main Memory Large array of words or bytes each having its own address Several processes must be kept in.

Segmentation with Paging

Solves the external fragmentation problem of pure segmentation

Each segment is composed of several equal-sized pages