allocating memory 1. readings r silberschatz: 8.3, 8.4 2

40
Allocating Memory 1

Upload: osborne-blair

Post on 17-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

1

Allocating Memory

2

Readings

Silberschatz: 8.3, 8.4

3

Outline

Contiguous Memory Allocation Paging Concepts

4

Introduction

In this section we will discuss approaches to allocating memory to processes Contiguous Paging

5

Contiguous Memory Allocation

6

Contiguous Memory Allocation

We will start out with the most basic method used that allows multiple processes to reside in memory

With contiguous memory allocation each process is contained in a single section of memory that is contiguous

7

Fixed Partitioning Any program, no matter how small, occupies

an entire partition. This represents internal fragmentation.

8 M

8 M

8 M

8 M

8 M

Operating System

8

Fixed Partitioning

Unequal-size partitions Lessens the problem with equal-size

partitionsOperating System

8 M

12 M

8 M

8 M

6 M

4 M

2 M

9

Placement Algorithm with Partitions

Equal-size partitions Since all partitions are of equal size, it does

not matter which partition is used Unequal-size partitions

Each process can be assigned to the smallest partition within which it will fit

There is a queue for each partition or a queue for all partitions

Processes are assigned in such a way as to minimize wasted memory within a partition

One Process Queue per Partition

NewProcesses

OperatingSystem

10

11

One Common Process Queue

When its time to load a process into main memory the smallest available partition that will hold the process is selected

OperatingSystem

NewProcesses

12

Simple MMU for Address Mapping

The base register holds the smallest physical memory address

The limit register specifies the range

These registers can be loaded only by the operating system

13

Simple MMU for Address Mapping

Simple mapping from logical addresses to physical

Relocation register: Value is added to every address generated by a user process

14

Questions Single Queue:

What is the problem with taking the first job that fits into the partition?

What is the problem with taking the largest job that fits into the partition?

Multiple Queues: What is the disadvantage?

15

Multiprogramming with Fixed Partitions

Was used by OS/360 on large IBM mainframes for many years

Incoming jobs were queued until a suitable partition was available

Today no modern OS uses fixed partitions

16

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 Compaction is required to obtain a large

block at the end of memory Shift processes so they are contiguous and

all free memory is in one block

17

Example Dynamic Partitioning

Operating System

Process 1 320 K

Process 2

Process 3

224 K

288 K

64 K

Operating System

Process 1 320 K

Process 3

224 K

288 K

64 K

Operating System

Process 1 320 K

Process 3 288 K

64 K

Process 4 128 K

96 K

18

Example Dynamic Partitioning

Operating System

320 K

Process 3 288 K

64 K

Process 4 128 K

96 K

Operating System

Process 3 288 K

64 K

Process 4 128 K

96 K

Process 2 224 k

96 K

19

Dynamic Partitioning Placement Algorithm

Operating system must decide which free block to allocate to a process

Best-fit algorithm Choose the block that is closest in size to

the request This has the worst overall performance The smallest block is found for a process

• The smallest amount of fragmentation is left • Memory compaction must be done more often

20

Dynamic Partitioning Placement Algorithm

First-fit algorithm Starts scanning memory from the beginning

and chooses the first available block that is large enough.

Next-fit Starts scanning memory from the location of

the last placement and chooses the next available block that is large enough

21

Paging

22

Introduction

Previous memory schemes were characterized by Need for compaction External fragmentation

Paging is a memory management technique that avoids compaction and external fragmentation

23

Paging Partition physical memory into small

equal-size chunks and divide a process’s logical memory into the same size chunks

The chunks of a process are called pages and chunks of memory are called frames

When a process is to be executed, its pages are loaded (from a file system) into any available memory frames

A page/frame is associated with a set of memory addresses

24

Paging An operating system maintains a page

table for each process Contains the frame location for each page in

a process

25

Page Example

0

1

2

3

Process A

0

1

2

3

0

1

2

Process B

7

8

9

01

2

3

4

5

6

7

8

9

10

11

12

13

14

FrameNumber

A0A1

A2A3

B0B1

B2Page Tables

• Each page table entry consists of a frame number

• The contents of the page fill that frame number

26

Address Translation Scheme

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

page table which contains start address of each page in physical memory

Page offset (d) – combined with start address of the page to define the physical memory address that is sent to the memory unit

27

Address Translation Scheme

Assume that the number of logical address bits is m

Organization of an address is the following page number page offset

p d

m - n n

28

Address Translation Scheme

If the number of logical address bits is m and the number of bits in the offset is n then Logical address space is 2m

Page size (number of entries in page) is 2n Example: m = 4, n = 2

Number of addresses is 16 Number of entries in a page is 4

29

Paging Hardware – Memory Management Unit (MMU)

30

Paging Hardware The CPU issues a logical address

(remember that all addresses are binary)

The hardware extracts the page number, p, and the page offset d

The page number, p, is used to index the page table The entry in the page table consists of the

frame number, f The actual address is the concatenation

of the bits that make up f and d.

31

Paging Example

m=4n=2Page size of 4 bytesPhysical memory of 32 bytes (8 pages)

Binary representations:0 001 012 103 11

32

Paging Example First a little reminder. All addresses are

in bits. The example on the previous page gives the base 10 equivalent of binary numbers

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

33

Example Logical address in binary form is 0000

From the bits we extract the page number as 00 and the offset as 00

Page number 00 corresponds to the first entry of the page table.

There we find that the corresponding frame is 5 (or 0101)

The physical address produced is the concatenation of the bits of the frame number and the offset• 010100

In base 10 we find that the physical address is 20

34

Example Logical address in binary form is 0101

From the bits we extract the page number as 01 and the offset as 01

Page number 01 corresponds to the second entry of the page table.

There we find that the corresponding frame is 6 (or 0110)

The physical address produced is the concatenation of the bits of the frame number and the offset• 011001

In base 10 we find that the physical address is 25(6*4+1)

35

Exercise

What do you think logical address 13 maps to?

36

Observations

Let’s look at logical addresses: 0000 (0), 0001 (1), 0010(2) and 0011(3) The first two bits (page number) are the

same This means that these logical addresses will

be in the same frame. The position in the frame is determined by

the offset For a process its pages can be in any

order For our example page 1 is in a frame that

appears “later” than the frame associated with page 2

37

Observations

The logical address space and the physical address space DO NOT have to be the same size

The logical address space can be larger then the physical address space (more on this later)

38

Paging No external fragmentation

Any free frame can be allocated to a process that needs it

There may be some internal fragmentation The memory requirements may not coincide

with page boundaries The last frame allocated may not be

completely full

39

Paging Internal fragmentation is on average

one half page per process Larger page sizes means more wasted

space Smaller page sizes means larger tables

40

Summary

This section introduced the concept of paging in memory