memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging •...

46
April 4 2005 Part of A3 course (by Theo Schouten) Biniam Gebremichael http://www.cs.ru.nl/~biniam/ Office: A6004 Memory Management

Transcript of memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging •...

Page 1: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

April 4 2005

Part of A3 course (by Theo Schouten)

Biniam Gebremichaelhttp://www.cs.ru.nl/~biniam/

Office: A6004

Memory Management

Page 2: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Contents

• Today: Memory management� Requirments� Fixed and dynamic partition

• Placement algorithms• Addressing

� Paging� Segmentation

• Next week[11/04]: Virtual memory• A week after [18/04] : Assignment

Page 3: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Reference

• “Operating Systems”, William Stallings,Prentice Hall, 4th ed. 2001, Chapter 7 and 8

• http://williamstallings.com/OS4e.html

Page 4: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Memory ManagementBring processes into main memory for execution

by the processor.

Memory• Actual Memory

� Main memory� Cache� Register

• Virtual Memory� Hard-Disk

Page 5: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Multi-programming v.Uni-programming

operating systemmemory area

User task memory area

User tasks memory area

Multi-programming allows many user tasks to run “concurrently” , but it brings several problems

with it. We really really need a good manager!!!

Page 6: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Structure of a task (Stallings Fig. 7.1)

Page 7: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

What do we want from a memory manager?1. Relocation: What is my address, is it

always the same?2. Protection: Am I safe from my neighbor3. Sharing: Can I share with my neighbor4. Logical Organization: Relating memory

address and actual program address?5. Physical Organization: storing?

Page 8: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Memory Management Requirements1. Relocation

� Programmer does not know where the program will be placed in memory when it is executed

� While the program is executing, it may be swapped to disk and returned to main memory at a different location (relocated)

� Memory references must be translated in to actual physical memory address

Page 9: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ��������������������������

Memory Management Requirements

2. Protection� Processes should not be able to reference

memory locations in another process without permission. (segmentation fault??)

� Protection is granted by the hardware (not by operating system). WHY?• absolute address is not known (b/c of relocation)• The memory manager uses the features in the

hardware to deny unpermitted access. • Adding permission bit.

Page 10: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� � ���

Memory Management Requirements3. Sharing

� Allow several processes to access the same portion of memory • make communication possible

� Better to allow each process access to the same copy of the program rather than have their own separate copy• Avoid multiple copies of the same module/data• Eg. printf, log.

Page 11: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ���

Memory Management Requirements

4. Logical Organization– Programs are written in modules– Modules can be written and compiled

independently– Different degrees of protection given to

modules (read-only, execute-only)– Sharable modules are addressed

independently.– Eg. DNS, sharing images in WWW

Page 12: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Memory Management Requirements5. Physical Organization• Managing the flow of data from/to

main memory to/from secondary memory (hard disk).

This task can be left to the programmer but:

• Memory available for the program plus its data may by unknown or insufficient.

� Use overlaying (allows many modules)…

too much work for the programmer.

Page 13: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Memory Management Techniques

1. Fixed Patitioning2. Dynamic Partitioning3. Paging 4. Segmentation5. Virtual Memory Paging6. Virtual Memory Segmentation

Page 14: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Fixed Partitioning

• Equal/Unequal-size partitions– any process whose size is less than or equal

to the partition size can be loaded into an available partition

– if all partitions are full, the operating system can swap a process out of a partition

– a program may not fit in a partition. The programmer must design the program with overlays.

– Fixed number of processes at a time.

Page 15: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Exa

mpl

e of

Fix

ed P

artit

ioni

ng o

f a 6

4 M

byte

Mem

ory

(Sta

lling

s fig

7.2

5)

Onl

y 7

proc

esse

s at

a ti

me

Page 16: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Fixed Partitioning

• Main memory use is inefficient. • Any program, no matter how small,

occupies an entire partition. � This is called internal fragmentation.

Page 17: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Placement Algorithm with Fixed Partitions

• Unequal-size partitions– can assign each process to the smallest

partition within which it will fit– queue for each partition– processes are assigned in such a way

as to minimize wasted memory within a partition

• Equal-size partitions– because all partitions are of equal size, it does not

matter which partition is used

Page 18: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ����

Memory Assignment for Fixed Partioning (Stallings Fig. 7.3)

* Once a process gets its queue can not move to another queue.

Page 19: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������� ���

Dynamic Partitioning• Partitions are of variable length

and number• Process is allocated exactly as

much memory as required.• Eventually get holes in the

memory. This is called external fragmentation.

• Must use compaction to shift processes so they are contiguous and all free memory is in one block.

Replacing an employee may disturb others (compaction)

Page 20: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ������������������������� ���

The Effect of Dynamic Partitioning(Stallings Fig. 7.4)

Page 21: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ������������������������ ���

The Effect of Dynamic Partitioning(Stallings Fig. 7.4)

Add a 10M process in “f”? – move P3

Page 22: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Dynamic Partitioning Placement AlgorithmOperating system must decide which free

block to allocate to a process

• Three different algorithms� Best-fit algorithm� First-fit algorithm� Next-fit algorithm

Page 23: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Worst Fit

Increasingaddressvalues

Example of placement algorithms(Stallings Fig. 7.5)

Allocate 16M process?

Page 24: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Dynamic Partitioning Placement Algorithm

• Best-fit algorithm� Chooses the block that is closest in size to

the request� Worst performer overall (expensive search,

and leaving small fragments is worst)� Since smallest block is found for process,

the smallest amount of fragmentation is left memory compaction must be done more often

Page 25: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Dynamic Partitioning Placement Algorithm

• First-fit algorithm� Fastest� May have many process loaded in the front

end of memory that must be searched over when trying to find a free block

Page 26: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Dynamic Partitioning Placement Algorithm• Next-fit

� More often allocate a block of memory at the end of memory where the largest block is found

� The largest block of memory is broken up into smaller blocks

� Compaction is required to obtain a large block at the end of memory

Page 27: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Buddy System

• Entire space available is treated as a single block of 2U

• If a request of size s such that 2U-1 < s <= 2U, entire block is allocated� Otherwise block is split into two equal

buddies� Process continues until smallest block

greater than or equal to s is generated

Page 28: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Example of Buddy System(Stallings Fig. 7.6)

Page 29: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Tree Representation of Buddy System(Stallings Fig. 7.7)

Page 30: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ������������������������� ���

Relocation

• When program loaded into memory the actual (absolute) memory locations are determined

• A process may occupy different partitions which means different absolute memory locations during execution (from swapping)

• Compaction will also cause a program to occupy a different partition which means different absolute memory locations

Page 31: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ������������������������ ���

Addresses

• Logical� reference to a memory location independent of the

current assignment of data to memory� translation must be made to the physical address

• Relative� address expressed as a location relative to some

known point

• Physical� the absolute address or actual location in main

memory

Page 32: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Hardware Support for Relocation(Stallings Fig. 7.8)

Page 33: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Registers Used during Execution• Base register

� starting address for the process

• Bounds register� ending location of the process

• These values are set when the process is loaded and when the process is swapped in

Page 34: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Registers Used during Execution• The value of the base register is added to

a relative address to produce an absolute address

• The resulting address is compared with the value in the bounds register

• If the address is not within bounds, an interrupt is generated to the operating system

Page 35: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Problems with Fixed and Dynamic Partitioning

• Fixed Partitioning� Internal fragmentation

• Dynamic Partitioning� External fragmentation

fixed dynamic

Page 36: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Paging• Partition memory into small equal-size chunks

(frames) and divide each process into the same size chunks (pages)

• Operating system maintains a page table for each process� contains the frame location for each page in the

process� memory address consist of a page number and

offset within the page.• Contigenuous frames not necessary• No external framentation. Little internal

framentation

Page 37: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Page 38: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Page 39: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ���������������������������

Page Tables

Page 40: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ������������������������� ���

Problems with Paging

• Paging is similar to fixed partitioning, except that processes are also partitioned.

• Partitioning a process into equal pages is not always possible.

• Another possiblity to have a paging similar to dynamic partitioning called segmentation

Page 41: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ������������������������ ���

Segmentation

• All segments of all programs do not have to be of the same length

• There is a maximum segment length• Addressing consist of two parts - a segment

number and an offset (similar to paging)• No internal fragmentation, little external

fragmentation

Page 42: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Page 43: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Page Translation

Page 44: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Segment Translation

Page 45: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Paging vs Segmentation

• Invisible to User

• Internal Fragmentation

• Pages all the same length

• Programmer/Compiler needs to be aware

• External Fragmentation

• Segments can be different lengths

Page 46: memory management - Institute for Computing and ...ths/a3/vm/memory_management.pdf · Paging • Partition memory into small equal-size chunks (frames) and divide each process into

��������������� ����������������������������

Next Week’s Lecture Virtual Memory• Introduction• Page tables• Translation Lookaside Buffer (TLB cache)• Fetch and Replacement Policy and

algorithm.• UNIX and Solaris Memory Management• Linux Memory Management• Windows 2000 Memory Management