1 / 107

Outline

This article provides an overview of memory management techniques in operating systems, including memory allocation strategies, segmentation, and paging. It also discusses the functions of a memory manager and the requirements of memory designs.

ajudith
Télécharger la présentation

Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Outline • Memory management • Introduction • Memory allocation strategies • Segmentation • Paging

  2. Memory Manager in OS COP4610

  3. Background • Program must be brought into memory and placed within a process for it to be executed • A program is a file on disk • CPU reads instructions from main memory and reads/writes data to main memory • Determined by the computer architecture • Address binding of instructions and data to memory addresses COP4610

  4. Review: Instruction Execution • Instruction fetch (IF) MAR  PC; IR  M[MAR] • Instruction Decode (ID) A  Rs1; B  Rs2; PC  PC + 4 • Execution (EXE) • Depends on the instruction • Memory Access (MEM) • Depends on the instruction • Write-back (WB) COP4610

  5. S1 bus Dest bus S2 bus Control unit ALU A R0, r1,... (registers) C B ia(PC) psw... MAR MDR IR MAR memory address register MDR memory data register IR instruction register Memory

  6. Creating a load module COP4610

  7. Linker function • Combine the object modules into a load module • Relocate the object modules as they are being loaded • Link the object modules together as they are being loaded • Search libraries for external references not defined in the object modules COP4610

  8. Relocation COP4610

  9. Linking COP4610

  10. A Sample Code Segment ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... } COP4610

  11. The Relocatable Object module Code Segment RelativeAddress Generated Code 0000 ... ... 0008 entry proc_a ... 0220 load =7, R1 0224 store R1, 0036 0228 push 0036 0232 call ‘put_record’ ... 0400 External reference table ... 0404 ‘put_record’ 0232 ... 0500 External definition table ... 0540 ‘proc_a’ 0008 ... 0600 (symbol table) ... 0799 (last location in the code segment) Data Segment RelativeAddress Generated variable space ... 0036 [Space for gVar variable] ... 0049 (last location in the data segment) COP4610

  12. The Absolute Program Code Segment RelativeAddress Generated Code 0000 (Other modules) ... 1008 entry proc_a ... 1220 load =7, R1 1224 store R1, 0136 1228 push 1036 1232 call 2334 ... 1399 (End of proc_a) ... (Other modules) 2334 entry put_record ... 2670 (optional symbol table) ... 2999 (last location in the code segment) Data Segment RelativeAddress Generated variable space ... 0136 [Space for gVar variable] ... 1000 (last location in the data segment) COP4610

  13. The Program Loaded at Location 4000 Relative Address Generated Code 0000 (Other process’s programs) 4000 (Other modules) ... 5008 entry proc_a ... 5036 [Space for gVar variable] ... 5220 load =7, R1 5224 store R1, 7136 5228 push 5036 5232 call 6334 ... 5399 (End of proc_a) ... (Other modules) 6334 entry put_record ... 6670 (optional symbol table) ... 6999 (last location in the code segment) 7000 (first location in the data segment) ... 7136 [Space for gVar variable] ... 8000 (Other process’s programs) COP4610

  14. Variations in program linking/loading COP4610

  15. Normal linking and loading COP4610

  16. Load-time dynamic linking COP4610

  17. Run-time dynamic linking COP4610

  18. UNIX Style Memory Layout for a Process COP4610

  19. Memory Management COP4610

  20. Requirements on Memory Designs • The primary memory access time must be as small as possible • The perceived primary memory must be as large as possible • The memory system must be cost effective COP4610

  21. Functions of Memory Manager • Allocate primary memory space to processes • Map the process address space into the allocated portion of the primary memory • Minimize access times using a cost-effective amount of primary memory COP4610

  22. Application Program VirtualAlloc() exec() VMQuery() shmalloc() VirtualFree() sbrk() VirtualLock() getrlimit() ZeroMemory() File Mgr Device Mgr Memory Mgr File Mgr Device Mgr Memory Mgr Process Mgr Process Mgr UNIX Windows Hardware The External View of the Memory Manager COP4610

  23. Less Frequently Used Information More Frequently Used Information Storage Hierarchies COP4610

  24. CPU Registers Less Frequently Used Information Primary Memory (Executable Memory) e.g. RAM More Frequently Used Information Secondary Memory e.g. Disk or Tape The Basic Memory Hierarchy COP4610

  25. CPU Registers Primary (Executable) L1 Cache Memory L2 Cache Memory “Main” Memory Larger storage Rotating Magnetic Memory Faster access Secondary Optical Memory Sequentially Accessed Memory Contemporary Memory Hierarchy COP4610

  26. Exploiting the Hierarchy • Upward moves are (usually) copy operations • Require allocation in upper memory • Image exists in both higher & lower memories • Updates are first applied to upper memory • Downward move is (usually) destructive • Destroy image in upper memory • Update image in lower memory • Place frequently-used info high, infrequently-used info low in the hierarchy • Reconfigure as process changes phases COP4610

  27. Overview of Memory Management Techniques • Memory allocation strategies • View the process address space and the primary memory as contiguous address space • Paging and segmentation based techniques • View the process address space and the primary memory as a set of pages / segments • Map an address in process space to a memory address • Virtual memory • Extension of paging/segmentation based techniques • To run a program, only the current pages/segments need to in primary memory COP4610

  28. Memory Allocation Strategies - There are two different levels in memory allocation COP4610

  29. Two levels of memory management COP4610

  30. Memory Management System Calls • In Unix, the system call is brk • Increase the amount of memory allocated to a process COP4610

  31. Malloc and New functions • They are user-level memory allocation functions, not system calls COP4610

  32. Memory Management in a Process COP4610

  33. Issues in a memory allocation algorithm • Memory layout / organization • how to divide the memory into blocks for allocation? • Fixed partition method: divide the memory once before any bytes are allocated. • Variable partition method: divide it up as you are allocating the memory. • Memory allocation • select which piece of memory to allocate to a request • Memory organization and memory allocation are close related • It is a very general problem • Variations of this problem occurs in many places. • For examples: disk space management COP4610

  34. Fixed-Partition Memory allocation • Statically divide the primary memory into fixed size regions • Regions can have different sizes or same sizes • A process / request can be allocated to any region that is large enough COP4610

  35. Fixed-Partition Memory allocation– cont. • Advantages • easy to implement. • Good when the sizes for memory requests are known. • Disadvantage: • cannot handle variable-size requests effectively. • Might need to use a large block to satisfy a request for small size. • Internal fragmentation – The difference between the request and the allocated region size; Space allocated to a process but is not used • It can be significant if the requests vary in size considerably COP4610

  36. Queue for each block size COP4610

  37. Allocate a large block to a small request? COP4610

  38. Variably-sized memory requests COP4610

  39. Variable partition memory allocation • Grant only the size requested • Example: • total 512 bytes: • allocate(r1, 100), allocate(r2, 200), allocate(r3, 200), • free(r2), • allocate(r4, 10), • free(r1), • allocate(r5, 200) COP4610

  40. Issues in Variable partition memory allocation • Where are the free memory blocks? • Keeping track of the memory blocks • List method and bitmap method • Which memory blocks to allocate? • There may exist multiple free memory blocks that can satisfy a request. Which block to use? • Fragmentation must be minimized • How to keep track of free and allocated memory blocks? COP4610

  41. The block list method COP4610

  42. Information of each memory block • Information of each memory block • Address: Block start address • size: size of the block. • Allocated: whether the block is free or allocated. Struct list_node { int address; int size; int allocated; struct list_node *next; }; COP4610

  43. After allocating P5 COP4610

  44. After freeing P3 COP4610

  45. Design issues in the list method • Linked list or doubly linked list ? • Keep track of allocated blocks in the list? • Two separate lists • Can be used for error checking • Where to keep the block list • Static reserved space for linked list • Use space with each block for memory management COP4610

  46. Reserving space for the block list COP4610

  47. Block list with list headers COP4610

  48. Bitmap method COP4610

  49. Which free block to allocate • How to satisfy a request of size n from a list of free blocks • First-fit: Allocate the first free block that is big enough • Next-fit: Choose the next free block that is large enough • Best-fit: Allocate the smallest free block that is big enough • Must search entire list, unless ordered by size. • Produces the smallest leftover hole. • Worst-fit: Allocate the largest free block • Must also search entire list. • Produces the largest leftover hole. COP4610

  50. Fragmentation • Without mapping, programs require physically continuous memory • External fragmentation • total memory space exists to satisfy a request, but it is not contiguous. • Internal fragmentation • allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. COP4610

More Related