CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur

CHAPTER 3 MEMORY MANAGEMENT PART I by Ugur Halıcı

memory management 1 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.

Memory Management 2 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. 1 Fixed Partitioning 3 memory OS n KB small 3 n KB Medium 6 n KB Large 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

3. 1 Fixed Partitioning 4 memory OS small area Q 3 n KB Medium medium area Q 6 n KB Large n KB large area Q 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.

3. 1 Fixed Partitioning 5 memory OS small area Q 3 n KB Medium medium area Q 6 n KB Large large area Q n KB 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.

3. 1 Fixed Partitioning 6 memory OS n KB 3 n KB small area Q Medium medium area Q Large large area Q 6 n KB The main problem with the fixed partitioning method is how to determine the number of partitions, and how to determine their sizes.

7 Fixed Partitioning with Swapping memory OS 2 K P 1 6 K P 2 12 K empty P 3 P 4 P 5 empty 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.

8 Fixed Partitioning with Swapping memory OS Secondary storage 2 K P 1 P 3 6 K P 2 P 4 12 K empty P 5 empty

9 Fixed Partitioning with Swapping memory Swap out P 1 Secondary storage OS 2 K 6 K P 2 12 K empty P 3 P 1 P 4 P 5 empty

10 Fixed Partitioning with Swapping memory Swap in P 3 Secondary storage OS 2 K P 3 P 1 6 K P 2 P 4 12 K empty P 5 empty

11 Fixed Partitioning with Swapping memory OS Secondary storage 2 K P 3 P 1 6 K P 2 P 4 12 K empty P 5 empty

12 Fixed Partitioning with Swapping memory Swap out P 3 Secondary storage OS 2 K 6 K P 2 12 K empty P 1 P 3 P 4 P 5 empty

13 Fixed Partitioning with Swapping memory Swap in P 1 Secondary storage OS 2 K P 1 P 3 6 K P 2 P 4 12 K empty P 5 empty

14 Fixed Partitioning with Swapping memory OS Secondary storage 2 K P 1 P 3 6 K P 2 P 4 12 K empty P 5 empty

fragmentation 15 memory OS 2 K P 1 (2 K) 6 K Empty (6 K) 12 K P 2 (9 K) empty Empty (3 K) 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.

3. 2 Variable Partitioning 16 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.

3. 2 Variable Partitioning 17 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.

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

first fit 19 First Fit : Allocate the first free block that is large enough for the new process. This is a fast algorithm.

first fit 20 OS P 1 12 KB Initial memory mapping <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

first fit 21 OS P 1 12 KB P 4 of 3 KB arrives <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

first fit 22 OS P 1 12 KB P 4 of 3 KB loaded here by FIRST FIT P 4 3 KB <FREE> 7 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

first fit 23 OS P 1 12 KB P 5 of 15 KB arrives P 4 3 KB <FREE> 7 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

first fit 24 OS P 1 12 KB P 5 of 15 KB loaded here by FIRST FIT P 4 3 KB <FREE> 7 KB P 2 20 KB P 5 15 KB <FREE> 1 KB P 3 6 KB <FREE> 4 KB

Best fit 25 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.

best fit 26 OS P 1 12 KB Initial memory mapping <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

best fit 27 OS P 1 12 KB P 4 of 3 KB arrives <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

best fit 28 OS P 1 12 KB P 4 of 3 KB loaded here by BEST FIT <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB P 4 3 KB <FREE> 1 KB

best fit 29 OS P 1 12 KB P 5 of 15 KB arrives <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB P 4 3 KB <FREE> 1 KB

best fit 30 OS P 1 12 KB P 5 of 15 KB loaded here by BEST FIT <FREE> 10 KB P 2 20 KB P 5 15 KB <FREE> 1 KB P 3 6 KB P 4 3 KB <FREE> 1 KB

worst fit 31 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.

worst fit 32 OS P 1 12 KB Initial memory mapping <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

worst fit 33 OS P 1 12 KB P 4 of 3 KB arrives <FREE> 10 KB P 2 20 KB <FREE> 16 KB P 3 6 KB <FREE> 4 KB

worst fit 34 OS P 1 12 KB P 4 of 3 KB Loaded here by WORST FIT <FREE> 10 KB P 2 20 KB P 4 3 KB <FREE> 13 KB P 3 6 KB <FREE> 4 KB

worst fit 35 OS P 1 12 KB No place to load P 5 of 15 K <FREE> 10 KB P 2 20 KB P 4 3 KB <FREE> 13 KB P 3 6 KB <FREE> 4 KB

worst fit 36 OS P 1 12 KB No place to load P 5 of 15 K <FREE> 10 KB P 2 20 KB P 4 3 KB <FREE> 13 KB Compaction is needed !! P 3 6 KB <FREE> 4 KB

compaction 37 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

compaction 38 OS P 1 12 KB Memory mapping before compaction <FREE> 10 KB P 2 20 KB P 4 3 KB <FREE> 13 KB P 3 6 KB <FREE> 4 KB

compaction 39 OS P 1 12 KB P 2 20 KB P 4 3 KB P 3 6 KB Swap out P 2

compaction 40 OS P 1 12 KB P 2 20 KB Swap in P 2 Secondary storage P 4 3 KB P 3 6 KB

compaction 41 OS P 1 12 KB P 2 20 KB Secondary storage P 4 3 KB P 3 6 KB Swap out P 4

compaction 42 OS P 1 12 KB P 2 20 KB P 4 3 KB Swap in P 4 with a different starting address P 3 6 KB Secondary storage

compaction 43 OS P 1 12 KB P 2 20 KB P 4 3 KB Secondary storage Swap out P 3 6 KB

compaction 44 OS P 1 12 KB P 2 20 KB P 4 3 KB P 3 6 KB Swap in P 3 Secondary storage

compaction 45 OS P 1 12 KB Memory mapping after compaction P 2 20 KB P 4 3 KB P 3 6 KB <FREE> 27 KB Now P 5 of 15 KB can be loaded here

compaction 46 OS P 1 12 KB P 2 20 KB P 4 3 KB P 3 6 KB P 5 12 KB <FREE> 12 KB P 5 of 15 KB is loaded

relocation 47 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
- Slides: 48