Chapter 2 Memory Management: Early Systems Understanding Operating Systems, Fourth Edition Objectives You will be able to describe: « The basic functionality of the three memory allocation schemes presented in this chapter: fixed partitions, dynamic partitions, relocatable dynamic partitions « Best-fit memory allocation as well as first-fit memory allocation schemes « How a memory list keeps track of available memory « The importance of deallocation of memory in a dynamic partition system Understanding Operating Systems, Fourth Edition 2 Objectives (continued) Students should be able to describe: « The importance of the bounds register in memory allocation schemes « The role of compaction and how it improves memory allocation efficiency Understanding Operating Systems, Fourth Edition Memory Management: Early Systems MEMORY MANAGER Single-User Configurations ¥ Fixed Partitions \/ Dynamic Partitions Y Relocatable Dynamic Partitions “Memory is the primary and fundamental power, without which there could be no other intellectual operation.” —Samuel Johnson (1709-1784) Understanding Operating Systems, Fourth Edition 4 Memory Management: Early Systems « Types of memory allocation schemes: — Single-user systems — Fixed partitions — Dynamic partitions — Relocatable dynamic partitions Understanding Operating Systems, Fourth Edition 5 Single-User Contiguous Scheme « Single-User Contiguous Scheme: Program is loaded in its entirety into memory and allocated as much contiguous space in memory as it needs — Jobs processed sequentially in single-user systems — Requires minimal work by the Memory Manager * Register to store the base address » Accumulator to keep track of the program size Understanding Operating Systems, Fourth Edition 6 Single-User Contiguous Scheme (continued) « Disadvantages Scheme: of Single-User Contiguous — Doesn’t support multiprogramming — Not cost effective Understanding Operating Systems, Fourth Edition Fixed Partitions * Fixed Partitions: Main memory is partitioned; one partition/job — Allows multiprogramming — Partition sizes remain static unless and until computer system id shut down, reconfigured, and restarted — Requires protection of the job’s memory space — Requires matching job size with partition size Understanding Operating Systems, Fourth Edition 8 Fixed Partitions (continued) To allocate memory spaces to jobs, the operating system’s Memory Manager must keep a table as shown below: Partition Size Memory Address Access Partition Status 100K 200K Job 1 Busy 25K 300K Job 4 Busy 25K 325K 50K 350K Free Job 2 Busy Table 2.1: A simplified fixed partition memory table with the free partition shaded Understanding Operating Systems, Fourth Edition Fixed Partitions (continued) Job List: J1 30K J2 50K J3 30K J4 25K Original State e After Job Entry Job 1 30K) - — NOTE: Job 3 must wait even though 70K of free space Parttion2| Partition 3 25K 25K Job 4 (25K) | f Partition2 Partition 3 Partition 4 Partition 4 e | Job2 GOKYI] Main Memory Main Memory Figure 2.1: Main memory of Table 2.1 Understanding Operating Systems, Fourth Edition Is availablein Partition 1 where Job 1 occupies Only 30K Of the 100K available use during fixed partition allocation 10 Fixed Partitions (continued) * Disadvantages: — Requires entire program to be stored contiguously — Jobs are allocated space on the basis of first available partition of required size — Works well only if all of the jobs are of the same size or if the sizes are known ahead of time — Arbitrary partition sizes lead to undesired results * Too small a partition size results in large jobs having longer turnaround time * Too large a partition size results in memory waste or internal fragmentation Understanding Operating Systems, Fourth Edition 11 Dynamic Partitions « Dynamic Partitions: Jobs are given only as much memory as they request when they are loaded — Available memory is kept in contiguous blocks — Memory waste is comparatively small + Disadvantages: — Fully utilizes memory only when the first jobs are loaded — Subsequent allocation leads to memory waste or external fragmentation Understanding Operating Systems, Fourth Edition 12 Dynamic Partitions (continued) Job J1 J2 J3 J4 List: 10K 15K 20K 50K Operating System | Job1 Job2 (10K ) Uperting ystom|| Job 5 (5K} 20K 20K (15K) Job2 35K Job3 (20K) (15K) Job 1 ends Job 4 ends 5K Job3 (20K) 55K Job4 Operating System Job2 (15K) Job 3 (20K) Job& (30K) Job 5 (5K) arrives Job 6 (30K) arrives 55K (50K) 105K 105K Initial job entry After Job 1 and Job 4 After Job 5 and Job 6 memory allocation have finished have entergd (a) (b) (c) Figure 2.2: Main memory use during dynamic partition allocation Understanding Operating Systems, Fourth Edition 13 Dynamic Partitions (continued) Operating System - Operating System 10K Job5 15K Job5 15K (5K) (5K) 20K Job2 20K (15K) Job 3 ends Job2 35K (15K) Job 7 (10K) arrives Job 8 (30K) arrives 35K N Job7 (10K) 55K Job6 (30K) 45K 55K Job6 (30K) 85K 85K 105K 105K After Job 3 has finished Job 8 has to wait After Job 7 has entered Figure 2.2 (continued): Main memory use during dynamic partition allocation Understanding Operating Systems, Fourth Edition 14 Best-Fit Versus First-Fit Allocation « Free partitions are allocated on the following basis: — First-fit memory allocation: First partition fitting the requirements » Leads to fast allocation of memory space — Best-fit memory allocation: Smallest partition fitting the requirements * Results in least wasted space * Internal fragmentation Understanding Operating Systems, Fourth Edition reduced but not eliminated 15 Best-Fit Versus First-Fit Allocation (continued) First-fit memory allocation: — Advantage: Faster in making allocation — Disadvantage: Leads to memory waste « Best-fit memory allocation — Advantage: Makes the best use of memory space — Disadvantage: Slower in making allocation Understanding Operating Systems, Fourth Edition Best-Fit Versus First-Fit Allocation (continued) Job List: Job Memory number requested n 10K J2 20K 13 30K* 14 10K Memory List: Memory Memory Job Job location block size number size 10240 30K J1 10K | Busy 40960 15K J4 10K Busy 5K 56320 50K J2 20K Busy 30K 107520 20K Total Available: = 115K Internal Status fragmentation 20K Free Total Used: 40K Figure 2.3: An example of a first-fit free scheme Understanding Operating Systems, Fourth Edition 17 Best-Fit Versus First-Fit Allocation (continued) Job List: Job number Memory requested J1 10K J2 J3 J4 20K 30K 10K Memory List: Memory Memory location block size Job number 40960 107520 15K J1i 20K 10240 Job size Internal Status fragmentation 5K J2 10K 20K | Busy | None 30K J3 30K Busy None 56320 50K J4 10K Busy Total Available: 115K Total Used: Busy 40K 70K Figure 2.4: An example of a best-fit free scheme Understanding Operating Systems, Fourth Edition Best-Fit Versus First-Fit Allocation (continued) « Algorithm for First-Fit: — Assumes Memory Manager keeps two lists, one for free memory and one for busy memory blocks — Loop compares the size of each job to the size of each memory block until a block is found that’s large enough to fit the job — Job is stored into that block of memory — Memory Manager moves out of the loop to fetch the next job from the entry queue Understanding Operating Systems, Fourth Edition 19 Best-Fit Versus First-Fit Allocation (continued) « Algorithm for First-Fit (continued): — If the entire list is searched in vain, then the job placed into a waiting queue is — The Memory Manager then fetches the next job and repeats the process Understanding Operating Systems, Fourth Edition 20 Best-Fit Versus First-Fit Allocation (continued) Before Request Beginning Address Memory Block Size After Request Beginning Address Memory Block Size 4075 105 4075 105 5225 5 5225 5 6785 600 *6985 400 7560 20 7560 20 7600 205 7600 205 10250 4050 10250 4050 15125 230 15125 230 24500 1000 24500 1000 Table 2.2: Status of each memory block before and after a request is made for a block of 200 spaces using the first-fit algorithm Understanding Operating Systems, Fourth Edition Best-Fit Versus First-Fit Allocation (continued) « Algorithm for Best-Fit: — Goal: find the smallest memory block into which the job will fit — Entire table must be searched Understanding Operating Systems, Fourth Edition before allocation 22 Best-Fit Versus First-Fit Allocation (continued) Before Request Beginning Address Memory Block Size After Request Beginning Address Memory Block Size 4075 105 4075 105 5225 5 5225 5 6785 600 6785 600 7560 20 7560 20 7600 205 *7800 5 10250 4050 10250 4050 15125 230 15125 230 24500 1000 24500 1000 Table 2.3: Status of each memory block before and after a request is made for a memory block of 200 spaces using the best-fit algorithm Understanding Operating Systems, Fourth Edition 23 Best-Fit Versus First-Fit Allocation (continued) « Hypothetical allocation schemes: — Next-fit: Starts searching from last allocated block, for the next available block when a new job arrives — Worst-fit: Allocates the largest free available block to the new job * Opposite of best-fit « Good way to explore the theory of memory allocation; might not be the best choice for an actual system Understanding Operating Systems, Fourth Edition 24 Deallocation * Deallocation: Freeing an allocated memory space — For fixed-partition system: « Straightforward process * When job completes, Memory Manager resets the status of the job’s memory block to “free” * Any code—for example, binary values with 0 indicating free and 1 indicating busy—may be used Understanding Operating Systems, Fourth Edition 25 Deallocation (continued) * For dynamic-partition system: — Algorithm tries to combine free areas of memory whenever possible — Three cases: » Case 1: When t he b oC k to be dea to another free block ocated is adjacent » Case 2: When't he b ocC K to be dea two free blocks ocated is between « Case 3: When't he b oC k to be dea from other free blocks ocated is isolated Understanding Operating Systems, Fourth Edition 26 Deallocation: Dynamic Partition System « Case 1: Joining Two Free Blocks — Change list must reflect starting address of the new free block * In the example, 7600—which was the address of the first instruction of the job that just released this block — Memory block size for the new free space must be changed to show its new size—that is, the combined total of the two free partitions * In the example, (200 + 5) Understanding Operating Systems, Fourth Edition 27 Case 1: Joining Two Free Blocks Beginning Address Memory Block Size Status 4075 105 Free 5225 5 Free 6785 600 Free 7560 20 Free (7600) (200) (Busy *7800 5 Free 10250 4050 Free 15125 230 Free 24500 1000 Free *Although the numbers in parentheses don’t appear in the free list, they've been inserted here for clarity. The job size is 200 and its beginning location is 7600. Table 2.4: Original free list before deallocation for Case 1 Understanding Operating Systems, Fourth Edition 28 Case 1: Joining Two Free Blocks (continued) Beginning Address Memory Block Size Status 4075 105 Free 5225 5 Free 6785 600 Free 7560 20 Free *7600 205 Free 10250 4050 Free 15125 230 Free 24500 1000 Free Table 2.5: Free list after deallocation Understanding Operating Systems, Fourth Edition for Case 1 Deallocation: Dynamic Partition System (continued) « Case 2: Joining Three Free Blocks. Deallocated memory space is between two free memory blocks — Change list to reflect the starting address of the new free block * In the example, 7560— which was the smallest beginning address — Sizes of the three free partitions must be combined * In the example, — Combined (20 + 20 + 205) entry is given the status of null entry * In the example, 7600 Understanding Operating Systems, Fourth Edition 30 Case 2: Joining Three Free Blocks Beginning Address Memory Block Size Status 4075 105 Free 5225 5 Free 6785 600 Free *7560 20 Free (7580) (20) (Busy)! *7600 205 Free 10250 4050 Free 15125 230 Free 24500 1000 Free ! Although the numbers in parentheses don't appear in the free list, they have been inserted here for clarity. Table 2.6: Original free list before deallocation for Case 2 Understanding Operating Systems, Fourth Edition Case 2: Joining Three Free Blocks (continued) Beginning Address Memory Block Size Status 4075 105 Free 5225 5 Free 6785 600 Free 7560 245 Free & (null entry) 10250 4050 Free 15125 230 Free 24500 1000 Free Table 2.7: Free list after job has released memory Understanding Operating Systems, Fourth Edition 32 Deallocation: Dynamic Partition System (continued) « Case 3: Deallocating an Isolated Block. Space to be deallocated is isolated from other free areas — System learns that the memory block to be released is not adjacent to any free blocks of memory, it is between two other busy areas — Must search the table for a null entry — Null entry in the busy list occurs when a memory block between two other busy memory blocks is returned to the free list Understanding Operating Systems, Fourth Edition 33 Case 3: Deallocating an Isolated Block Beginning Address Memory Block Size Status 4075 105 Free 5225 5 Free 6785 600 Free 7560 245 Free 10250 4050 Free 15125 230 Free 24500 1000 Free Table 2.8: Original free list before deallocation for Case 3 Understanding Operating Systems, Fourth Edition 34 Case 3: Deallocating an Isolated Block (continued) The job to be deallocated is of size 445 and begins at location 8805. The asterisk indicates the soon-to-be-free memory block. Beginning Address Memory Block Size Status 7805 1000 Busy *8805 445 Busy 9250 1000 Busy Table 2.9: Memory list before deallocation Understanding Operating Systems, Fourth Edition 35 Case 3: Deallocating an Isolated Block (continued) Beginning Address Memory Block Size Status 7805 1000 Busy & 9250 (null entry) 1000 Busy Table 2.10: Busy list after the job has released its memory. The asterisk indicates the new null entry in the busy list. Understanding Operating Systems, Fourth Edition 36 Case 3: Deallocating an Isolated Block (continued) Beginning Address Memory Block Size Status 4075 105 Free 5225 5 Free 6785 600 Free 7560 245 Free *88os 445 Free 10250 4050 Free 15125 230 Free 24500 1000 Free Table 2.11: Free list after the job has released its memory. The asterisk indicates the new free block entry replacing the null entry Understanding Operating Systems, Fourth Edition 37 Relocatable Dynamic Partitions * Relocatable Dynamic Partitions: — Memory Manager relocates programs to gather together all of the empty blocks — Compact the empty blocks to make one block of memory large enough to accommodate some or all of the jobs waiting to get in Understanding Operating Systems, Fourth Edition 38 Relocatable Dynamic Partitions (continued) « Compaction: Reclaiming fragmented sections of the memory space — Every program in memory must be relocated so they are contiguous — Operating system must distinguish between addresses and data values « Every address must be adjusted to account for the program’s new location in memory » Data values must be left alone Understanding Operating Systems, Fourth Edition 39 Relocatable Dynamic Partitions (continued) A EXP BEGIN: LOOP: 132, 144, 125, 110 ;the data values MOVEI 1,0 ;initialize register 1 MOVEI 2,0 ;initialize register 2 ADD 2,A(1) ;add (A ADDI 1.1 ;add 1 CAIG 1,4,-1 ;is reg JUMPA LOOP ;if not, go MOVE 3,2 ;if so, move IDIVI 3.4 :divide + reg 1) to reg 1 reg 2 1.4-17 reg ;remainder EXIT to to Loop reg 3 to by 2 to reg 3 4, register 4 ;end END Figure 2.5: An assembly language program that performs a simple incremental operation Understanding Operating Systems, Fourth Edition 40 Relocatable Dynamic Partitions (continued) 000EE0' 00E001' 000002"' 0OEEO3' 00E0RO 0DOOOO 0OOOOO 000000 000132 000144 000125 000110 peeee4' 00EEO5' 00OEO6' 0EEEO7' POEE68' 0EEEEY"' EEEE10"' 00E011' 0oEe12"' 201 201 270 271 307 324 200 231 047 O 0 @ 0 0 @ 0 0 0 01 02 02 01 01 00 03 03 00 OO 00 @1 00 00 GO 00 00 00 0OOOEO 0OOOEO GOOOEO’ 000001 000003 0OOOO6’ 000002 000004 000012 000000 A: EXP132,144,125,110 BEGIN: MOVEI MOVEI ADD ADDI CAIG JUMPA MOVE IDIVI EXIT LOOP: 1.0 2,0 2,A(1) 1,1 1,4-1 LoopP 3,2 3.4 END Figure 2.6: The original assembly language program after it has been processed by the assembler Understanding Operating Systems, Fourth Edition Relocatable Dynamic Partitions (continued) « Compaction issues: — What goes on behind the scenes when and compaction take place? relocation — What keeps track of how far each job has moved from its original storage area? — What lists have to be updated? Understanding Operating Systems, Fourth Edition 42 Relocatable Dynamic Partitions (continued) What lists have to be updated? — Free list must show the partition for the new block of free memory — Busy list must show the new locations for all of the jobs already in process that were relocated — Each job will have a new address except for those that were already at the lowest memory locations Understanding Operating Systems, Fourth Edition 43 Relocatable Dynamic Partitions (continued) « Special-purpose registers are used for relocation: — Bounds register « Stores highest location accessible by each program — Relocation register » Contains the value that must be added to each address referenced in the program so it will be able to access the correct memory addresses after relocation « If the program isn't relocated, the value stored in the program’s relocation register is zero Understanding Operating Systems, Fourth Edition 44 Relocatable Dynamic Partitions (continued) Operating System | 4o Operating System | ;0. Operating System | 40, Job 1 (8K) Job 1 (BK) Job 1 (8K) 18K 30K _._._._._._._.___________._-—r 18K Job 4 (32K) Jobd (32K) 50K S0K Job4 (32K) 62K Job2 (16K) 66K 18K Now Job 6 bty Job 2 (16K) 66K accommodated 92K Job5 (48K) Job5 (48K) Job 2 (16K) 114K 114K Job S (48K) m‘/ 156K Job 6 (84K) 198K ———LL L Initial memory layout — 20K Memaory layout after compaction (a} (b} 210K Memaory layout after Job 6 has been Ipaded (€) Figure 2.7: Three snapshots of memory before and after compaction Understanding Operating Systems, Fourth Edition Relocatable Dynamic Partitions Operating System 10K Operating System Job 1 18K Job 1 (8K) —= Load 4, 53248 (8K) 30K 31744— /, Load 4, 53248 19456 40960 — Job 4 37 (32K) Job 4 53248— 37 62K 10K 18K 50K (32K) Job2 (16K) 66K Relocation register 00000 92K Job2 (16K) e i Relocation register Job5 12288 (48K) 1 i (a) | (b) Figure 2.8: Contents of relocation register and close-up of Job 4 memory area (a) before relocation and (b) after relocation and compaction Understanding Operating Systems, Fourth Edition 46 Relocatable Dynamic Partitions (continued) « Compacting and relocating optimizes the use of memory and thus improves throughput « Options for when and how often it should be done: — When a certain percentage of memory is busy — When there are jobs waiting to get in — After a prescribed amount of time has elapsed Goal: Optimize processing time and memory use while keeping overhead as low as possible Understanding Operating Systems, Fourth Edition 47 Summary * Four memory management techniques were used in early systems: single-user systems, fixed partitions, dynamic partitions, and relocatable dynamic partitions * Memory waste in dynamic partitions is comparatively small as compared to fixed partitions First-fit is faster in making allocation but leads to memory waste « Best-fit makes the best use of memory space but slower in making allocation Understanding Operating Systems, Fourth Edition 48 Summary (continued) « Compacting and relocating optimizes the use of memory and thus improves throughput « All techniques require that the entire program must: — Be loaded into memory — Be stored contiguously — Remain in memory until the job is completed « Each technique puts severe restrictions on the size of the jobs: can only be as large as the largest partitions in memory Understanding Operating Systems, Fourth Edition 49