Top Banner
CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı
48
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 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

CHAPTER 3MEMORY MANAGEMENTPART I

by Ugur Halıcı

Page 2: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

memory management

In a multiprogramming system, in order to share the processor, a number of processes must be kept in memory.

Memory management is achieved through memory management algorithms.

Each memory management algorithm requires its own hardware support.

In this chapter, we shall see the partitioning, paging and segmentation methods.

2

Page 3: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Memory Management

In order to be able to load programs at anywhere in memory, the compiler must generate relocatable object code.

Also we must make it sure that a program in memory, addresses only its own area, and no other program’s area. Therefore, some protection mechanism is also needed.

3

Page 4: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

3.1 Fixed Partitioning

In this method, memory is divided into partitions whose sizes are fixed.

OS is placed into the lowest bytes of memory.

Relocation of processes is not needed

memory

OS

n KB small

3n KB

Medium

6n KB

Large

4

Page 5: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

3.1 Fixed Partitioning

Processes are classified on entry to the system according to their memory they requirements.

We need one Process Queue (PQ) for each class of process.

memory

OS

n KB small

3n KB

Medium

6n KB

Largelarge area Qlarge area Q

5

Page 6: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

3.1 Fixed Partitioning

If a process is selected to allocate memory, then it goes into memory and competes for the processor.

The number of fixed partition gives the degree of multiprogramming.

Since each queue has its own memory region, there is no competition between queues for the memory.

memory

OS

n KB small

3n KB

Medium

6n KB

Largelarge area Qlarge area Q

6

Page 7: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

3.1 Fixed Partitioning

The main problem with the fixed partitioning method is how to determine the number of partitions, and how to determine their sizes.

memory

OS

n KB small

3n KB Medium

6n KB

Largelarge area Qlarge area Q

7

Page 8: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

This is a version of fixed partitioning that uses RRS with some time quantum.

When time quantum for a process expires, it is swapped out of memory to disk and the next process in the corresponding process queue is swapped into the memory.

memory

OS

2K P1

6K P2

12K empty

P3P3

P4P4

emptyempty

P5P5

8

Page 9: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K P1

6K P2

12K empty

P3P3

P4P4

emptyempty

P5P5Secondary

storage

9

Page 10: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K

6K P2

12K empty

P3P3

P4P4

emptyempty

P5P5

Swap out P1

Secondarystorage

P1P1

10

Page 11: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K P3

6K P2

12K empty

P1P1

P4P4

emptyempty

P5P5

Swap in P3

Secondarystorage

11

Page 12: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K P3

6K P2

12K empty

P1P1

P4P4

emptyempty

P5P5Secondary

storage

12

Page 13: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K

6K P2

12K empty

P1P1

P4P4

emptyempty

P5P5

Swap out P3

Secondarystorage

P3P3

13

Page 14: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K P1

6K P2

12K empty

P3P3

P4P4

emptyempty

P5P5

Swap in P1

Secondarystorage

14

Page 15: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Fixed Partitioning with Swapping

memory

OS

2K P1

6K P2

12K empty

P3P3

P4P4

emptyempty

P5P5Secondary

storage

15

Page 16: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

fragmentation

memory

OS

2K

6K Empty (6K)

12K empty

Empty (3K)

P2 (9K)P2 (9K)

P1 (2K)P1 (2K)

If a whole partition is currently not being

used, then it is called an external

fragmentation.

If a whole partition is currently not being

used, then it is called an external

fragmentation.

If a partition is being used by a

process requiring some memory

smaller than the partition size, then

it is called an internal

fragmentation.

If a partition is being used by a

process requiring some memory

smaller than the partition size, then

it is called an internal

fragmentation.

16

Page 17: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

 

3.2 Variable Partitioning

With fixed partitions we have to deal with the problem of determining the number and sizes of partitions to minimize internal and external fragmentation.

If we use variable partitioning instead, then partition sizes may vary dynamically.

In the variable partitioning method, we keep a table (linked list) indicating used/free areas in memory.

17

Page 18: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

 

3.2 Variable Partitioning

Initially, the whole memory is free and it is considered as one large block.

When a new process arrives, the OS searches for a block of free memory large enough for that process.

We keep the rest available (free) for the future processes.

If a block becomes free, then the OS tries to merge it with its neighbors if they are also free.

18

Page 19: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

3.2 Variable Partitioning

There are three algorithms for searching the list of free blocks for a specific amount of memory. First Fit Best Fit Worst Fit

19

Page 20: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

first fit

First Fit : Allocate the first free block that is large enough for the new process.

This is a fast algorithm.

20

Page 21: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

first fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

Initial memory mapping

Initial memory mapping

21

Page 22: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

first fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

P4 of 3KB arrives

P4 of 3KB arrives

22

Page 23: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

first fit

OS

P1 12 KB

P4 3 KB

<FREE> 7 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

P4 of 3KB loaded here

by FIRST FIT

P4 of 3KB loaded here

by FIRST FIT

23

Page 24: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

first fit

OS

P1 12 KB

P4 3 KB

<FREE> 7 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

P5 of 15KB arrives

P5 of 15KB arrives

24

Page 25: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

first fit

OS

P1 12 KB

P4 3 KB

<FREE> 7 KB

P2 20 KB

P5 15 KB

<FREE> 1 KB

P3 6 KB

<FREE> 4 KB

P5 of 15 KB loaded here

by FIRST FIT

P5 of 15 KB loaded here

by FIRST FIT

25

Page 26: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

Best fit

Best Fit : Allocate the smallest block among those that are large enough for the new process.

In this method, the OS has to search the entire list, or it can keep it sorted and stop when it hits an entry which has a size larger than the size of new process.

This algorithm produces the smallest left over block. However, it requires more time for searching all the

list or sorting it If sorting is used, merging the area released when a

process terminates to neighboring free blocks, becomes complicated.

26

Page 27: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

best fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

Initial memory mapping

Initial memory mapping

27

Page 28: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

best fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

P4 of 3KB arrives

P4 of 3KB arrives

28

Page 29: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

best fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

P4 3 KB

<FREE> 1 KB

P4 of 3KB loaded here

by BEST FIT

P4 of 3KB loaded here

by BEST FIT

29

Page 30: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

best fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

P4 3 KB

<FREE> 1 KB

P5 of 15KB arrives

P5 of 15KB arrives

30

Page 31: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

best fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P5 15 KB

<FREE> 1 KB

P3 6 KB

P4 3 KB

<FREE> 1 KB

P5 of 15 KB loaded here

by BEST FIT

P5 of 15 KB loaded here

by BEST FIT

31

Page 32: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

worst fit

Worst Fit : Allocate the largest block among those that are large enough for the new process.

Again a search of the entire list or sorting it is needed.

This algorithm produces the largest over block.

32

Page 33: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

worst fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

Initial memory mapping

Initial memory mapping

33

Page 34: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

worst fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

<FREE> 16 KB

P3 6 KB

<FREE> 4 KB

P4 of 3KB arrives

P4 of 3KB arrives

34

Page 35: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

worst fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

P4 of 3KB Loaded here

by WORST FIT

P4 of 3KB Loaded here

by WORST FIT

35

Page 36: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

worst fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

No place to load P5 of 15K

No place to load P5 of 15K

36

Page 37: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

worst fit

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

No place to load P5 of 15K

No place to load P5 of 15K

Compaction is needed !!Compaction is needed !!

37

Page 38: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

Compaction is a method to overcome the external fragmentation problem.

All free blocks are brought together as one large block of free space.

Compaction requires dynamic relocation. Certainly, compaction has a cost and selection of

an optimal compaction strategy is difficult. One method for compaction is swapping out

those processes that are to be moved within the memory, and swapping them into different memory locations

38

Page 39: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

<FREE> 10 KB

P2 20 KB

P4 3 KB

<FREE> 13 KB

P3 6 KB

<FREE> 4 KB

39

Memory mapping before

compaction

Memory mapping before

compaction

Page 40: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

40

Swap out P2

Page 41: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

Swap in P2

Secondarystorage

41

Page 42: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

Swap out P4

Secondarystorage

42

Page 43: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

Swap in P4 with a different starting address

Secondarystorage

43

Page 44: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

Swap out P3

Secondarystorage

44

Page 45: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

Swap in P3

Secondarystorage

45

Page 46: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

<FREE> 27 KB

Memory mapping after compaction

Memory mapping after compaction

Now P5 of 15KB can be loaded here

Now P5 of 15KB can be loaded here

46

Page 47: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

compaction

OS

P1 12 KB

P2 20 KB

P4 3 KB

P3 6 KB

P5 12 KB

<FREE> 12 KB P5 of 15KB is loaded

P5 of 15KB is loaded

47

Page 48: CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı.

relocation

Static relocation: A process may be loaded into memory, each time possibly having a different starting address

Necessary for variable partitioning Dynamic relocation: In addition to static

relocation, the starting address of the process may change while it is already loaded in memory

Necessary for compaction

48