1.28k likes | 1.6k Views
Operating Systems. CST 352 Memory Management. Topics. Introduction Definitions Fixed Partitions Multiprogramming Modeling Process Relocation Swapping Virtual Memory Segmentation. Introduction. Memory management deals with handling all memory resident in a computer system.
E N D
Operating Systems CST 352 Memory Management CST 352 - Operating Systems
Topics • Introduction • Definitions • Fixed Partitions • Multiprogramming Modeling • Process Relocation • Swapping • Virtual Memory • Segmentation CST 352 - Operating Systems
Introduction Memory management deals with handling all memory resident in a computer system. Current systems have memory ranging from: • High speed/low volume (expensive) • Low speed/high volume (cheap) CST 352 - Operating Systems
Introduction Arrow direction depicts increase CST 352 - Operating Systems
Block Block Word Transfer Transfer Transfer Main CPU Cache Disk Memory Introduction Memory Management: • What is the best policy to deal with the speed/volume/cost issues associated with memory management? CST 352 - Operating Systems
Introduction • Memory Management deals in tradeoffs: • How does a typical process in your system behave? • Do processes need to run “simultaneously”? • What is the typical size of a process? • What type of constraints will be imposed on processes running in your system? • What constraints have been “architected” into the system? • Etc. CST 352 - Operating Systems
Definitions • Monoprogramming – one process is run at a time till completion. • Multiprogramming– more than one process is resident in the system and must share memory as a resource. • Relocation – moving a process from one physical memory address space to another. • Swapping – moving the entire state of a process (PCB, run-time stack, data segment, code segment) from physical memory to an area on disk. CST 352 - Operating Systems
Definitions • Virtual Memory – providing an address space larger than the physical address space in a system. • Page – a contiguous address space that makes up a division of a virtual address space. CST 352 - Operating Systems
Definitions Notes: • Don’t get memory management confused to programmatic use of new/malloc and delete/free. • Heap management (new/malloc, free/delete) is a subset of the overall memory management problem. • Heap management is relative to processes at run time. • Memory management deals with process creation, relocation, and run-time behavior. CST 352 - Operating Systems
Fixed Partitions • Divide memory up into “n” partitions. • Each partition has a fixed size • Not necessarily the same size. • As “processes create” requests are made, they are put in a queue. • The system checks for free memory. • When a memory block becomes available that will fit the first process in the queue, create the new process. CST 352 - Operating Systems
Fixed Partitions A potential fixed partition layout for 64K of physical RAM. CST 352 - Operating Systems
Fixed Partitions • When each process is created • Load the process into memory • Create a PCB • Create a stack • Create a data and code segment • Place the new process in the suspend state. CST 352 - Operating Systems
Fixed Partitions Two allocation schemes: • Provide multiple process create queues. • Processes are added to the queue that is larger than the process required physical address. • Provide a single process create queue. • Processes are taken from the queue when a memory partition becomes available that will fit the process. CST 352 - Operating Systems
Fixed Partitions Fixed Partitions – Multiple Queues CST 352 - Operating Systems
Fixed Partitions Fixed Partitions – Single Queues CST 352 - Operating Systems
Fixed Partitions Q: • What is the difference between switching and swapping (…from an Operating Systems perspective)? CST 352 - Operating Systems
Multiprogramming Modeling • CPU Utilization in a multiprogramming environment can be modeled as using a probability model. • Assume a process spends a fraction of it’s time waiting for I/O (p). CST 352 - Operating Systems
Multiprogramming Modeling • The probability that all processes are waiting for I/O is: pn ,n = number of processes in the system. n is known as the “degree of multiprogramming. e.g.: pn = probability CPU is idle. CST 352 - Operating Systems
Multiprogramming Modeling • Therefore, the probability the CPU is utilized is: CPU Utilization = 1 – pn CST 352 - Operating Systems
Multiprogramming Modeling CPU Utilization = 1 – pn If processes spend 80% (.80) of their time waiting for I/O (I/O bound) and there are 5 process in the system: CPU Utilization = 1 – (.8)5 = 1 – 0.32768 = 0.67232 = 67% CST 352 - Operating Systems
Multiprogramming Modeling CST 352 - Operating Systems
Multiprogramming Modeling This simple model can be used to study system tuning. Example: • I have 16 Mbytes of RAM • On Average, my system runs 8 processes. • My OS takes up 4 Mbytes of memory. CST 352 - Operating Systems
Multiprogramming Modeling Example: (cont’d) • My processes are as follows • 2 processes that are 80% I/O bound and take up 2 Mbytes each. • 2 processes that are 50% I/O bound and take up 4 Mbytes each. • 4 processes that are 20% I/O bound and take up 2 Mbytes of memory. CST 352 - Operating Systems
Multiprogramming Modeling Example: (cont’d) • Should I purchase more memory for this system? If so, how much RAM should I purchase? CST 352 - Operating Systems
Multiprogramming Modeling Example: Solution • First consider the process parameters: • 16 Mbytes of memory – 4 Mbytes for OS = 12 Mbytes available for processes. • Worse Case: • Both 50% 4 Mbyte processes are in memory. • Both 80% 2 Mbyte processes are in memory. CPU Utilization = 1 – (.8)2 * (.5)2 = 1 – 0.16 = 0.84 = 84% CST 352 - Operating Systems
Multiprogramming Modeling Example: Solution • First consider the process parameters (cont’d): • Best Case: • One 50% 4 Mbyte process is in memory. • Four 20% 2 Mbyte processes are in memory. CPU Utilization = 1 – (.5)* (.2)4 = 1 – 0.00064 = 0.99936 = 99.936% CST 352 - Operating Systems
Multiprogramming Modeling Example: Solution • First consider the process parameters (cont’d): • Average Case: (99.936% + 84%) / 2 = 91.96% CST 352 - Operating Systems
Multiprogramming Modeling Example: Solution • Calculate Utilization for buying 8 more Mbytes of RAM to accommodate all processes. CPU Utilization = 1 – (.5)2 * (.2)4 * (.8)2 = 1 – 0.0064 = 0.9936 = 99.36% CST 352 - Operating Systems
Multiprogramming Modeling Example: Solution • Cont’d CPU Utilization Difference = 99.36% – 91.96% = 7.4% 8 Mbytes of RAM = = 7.4% CPU Utilization increase. Depending on the price of RAM, this would probably be a good investment. CST 352 - Operating Systems
Multiprogramming Modeling • Given this scenario, is it beneficial to buy for the future in anticipation of the system being loaded with more processes? CST 352 - Operating Systems
Process Relocation In a multiprocessing system, it is impossible to know a-priori where a process will be run. Linking of an executable requires the linker to generate addresses that can be translated to different physical addresses. CST 352 - Operating Systems
Process Relocation • The linker will generate relative addresses. • The loader will resolve the relative address to some absolute physical address. Example: Linker creates addresses in the range: 0x0000 – 0x0F80 Loader relocates to the 1K partition starting at 0x2000. All addresses will then fall in the range: 0x2000 – 0x2F80 CST 352 - Operating Systems
Pic32 Memory Map CST 352 - Operating Systems
Process Relocation • Relocation Strategies • Find all addresses in the linker generated file and replace it with a relocated absolute address. • Use a base and limit register (hardware support) to produce absolute addresses from the linker generated file (this greatly reduces the job of the loader). (a memory map) CST 352 - Operating Systems
Process Relocation • Relocation Strategies • I need to devise a process relocation strategy for a system design where the hardware architecture is not known. What will I do? • I know the hardware architecture. What will I do? (a memory map) CST 352 - Operating Systems
Process Relocation • Relocation Strategies Base – Limit Register Implementation CST 352 - Operating Systems
Process Relocation A word on the linker: • Programmers must resolve all symbols through programmatic directives (e.g. extern, #include, etc.) • Assembler must be able to resolve out all symbols (see above). • Linker creates “.exe” using assembled symbols to generate relative addresses. • At run time, relative addresses are resolved, dependent on what memory area the “process” is being loaded. CST 352 - Operating Systems
Process Relocation A word on the loader: • Loader translates the relative addresses generated by the linker into addresses that can be used at run-time. • Full translation may not be done until the process (e.g. .exe) is being executed in the CPU. • See “base-limit” strategy for process relocation. CST 352 - Operating Systems
Swapping • Moving a process, in it’s entirety, from physical memory to a file on the disk (swap file). • With process swapping, the system can handle more processes than can actually fit into memory. • The OS must decide when to swap processes in and out and how to allocate those processes memory blocks. CST 352 - Operating Systems
Swapping Bitmaps based Memory Manager • Keep a bitmap where each bit corresponds to a block of memory. • A bit set to 0 represents a free memory block. • A bit set to 1 represents a used memory block. CST 352 - Operating Systems
Swapping Bitmaps based Memory Manager • Small memory blocks will result in: • Large bitmaps • Longer to manipulation time • Less wasted memory • Large memory blocks will result in: • Small bitmaps • Quick manipulation time • More unused memory due to fragmentation CST 352 - Operating Systems
Swapping Bitmap based Memory Manager Block Size – 16 Bytes Gray – Free Space White – Used Memory CST 352 - Operating Systems
Swapping Bitmaps based Memory Manager • When the OS needs to allocate memory: • Walk each bitmap searching for contiguous free blocks that can accommodate the process. • Must be able to account for groups of free blocks that bridge bitmaps. CST 352 - Operating Systems
Swapping Linked List based Memory Manager – Variation 1 • Keep a linked list that maps memory to processes. • The memory will initially all be on a “free list” or all descriptors will be marked as “free”. • As memory is allocated, it’s descriptor will be moved from the “free list” to an “allocated list” or the state of the block will change from “free” to “allocated”. CST 352 - Operating Systems
Swapping Linked List based Memory Manager (Variation 2) • Keep a linked list that maps memory to processes. • The memory list will initially have a single descriptor block of all free memory. • As memory is allocated, a descriptor will be created and put on the allocated list. • When memory is freed, the allocated descriptor will be put on a free list. • Contiguous free blocks will be combined into a single free list entry. CST 352 - Operating Systems
Swapping Linked List based Memory Manager • When the OS needs to allocate memory, it must find a contiguous set of free blocks. • To support this, the linked lists must be sorted based on address. CST 352 - Operating Systems
Swapping Linked List based Memory Manager • Allocation Policies • First Fit – Give back the first block that will fit the process. • Next Fit – Keep track of the last block that was allocated and always start searching for the first fit from there. • Best Fit – Find the contiguous free block that best fits the process and use it. • This actually turns out to fragment memory the quickest. CST 352 - Operating Systems
Swapping Linked List based Memory Manager • Allocation Policies • Worst Fit – Find the largest available hole and use it. • The hope here is that the remaining unused memory will be large enough to be useful to other processes. • Quick Fit – Keep a pre-allocated list of the most common requests sizes. CST 352 - Operating Systems
Virtual Memory • A key characteristic to notice about the previous memory management schemes is: • All memory references within a process are logical memory references. • Processes can be broken up into segments. For a process to execute, only the current segment needs to be in physical RAM. CST 352 - Operating Systems
Virtual Memory • Processes too large for physical RAM… • Deny the process run-time in the system. • Or…? CST 352 - Operating Systems