Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage.

Similar presentations


Presentation on theme: "Memory Management Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage."— Presentation transcript:

1

2 Memory Management

3 Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage CPU can access directly Register access in one CPU clock (or less) Main memory can take many cycles Cache sits between main memory and CPU registers Protection of memory required to ensure correct operation Memory Management

4 Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as many processes into memory as possible

5 5 General Memory Problem We have a limited (expensive) physical resource: main memory We want to use it as efficiently as possible We have an abundant, but slower, resource: disk

6 6 2/24/2016 Memory Management  Why?  What?  How? Swapping

7 7 2/24/2016 Memory Management Storage Hierarchy Local Monitor Fixed Partitions for Spooling and Multiprogramming Variable Sized Partitions Storage Placement Strategies Compaction

8 82/24/2016 Storage Hierarchy CPU Reg Cache Memory Secondary Storage 32-64 bits 4-128 words 512-16k words

9 9 2/24/2016 Lots of Variants Many processes, total size less than memory  Technically possible to pack them together  Will programs know about each other’s existence? One process, size exceeds memory  Can you only keep part of the program in memory? Lots of processes, total size exceeds memory  What programs are in memory, and how to decide?

10 Base and Limit Registers A pair of base and limit registers define the logical address space

11 Memory Management Five Requirements for Memory Management to satisfy: –Relocation Users generally don’t know where they will be placed in main memory May want to swap in at a different place –Must deal with user pointers Generally handled by hardware –Protection Prevent processes from interfering with the O.S. or other processes Often integrated with relocation –Sharing Allow processes to share data/programs –Logical Organization Support modules, shared subroutines –Physical Organization Manage mem « disk transfers

12 Binding of Instructions and Data to Memory Address binding of instructions and data to memory addresses can happen at three different stages –Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes –Load time: Must generate relocatable code if memory location is not known at compile time –Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers)

13 Multistep Processing of a User Program

14 14 2/24/2016 Memory Manager It’s task: Manage memory hierarchy  Track used and free memory  Allocate memory to processes  Reclaim (De-allocate) memory  Swapping between main memory and disk

15 15 2/24/2016 Mono-programming without Swapping Run one process at a time  simplest possible memory management scheme Memory is shared only between OS and the process Three different ways to organize memory:  OS at the bottom of memory in RAM (Random Access Memory); was used in mainframes and minicomputers; not used anymore  OS is in ROM (Read-Only Memory) at the top of memory; is used in some palm-tops and embedded systems  Device drivers are in ROM at the top of memory and the rest is in RAM; was used by early PCs (e.g., running MS-DOS), where the portion in the ROM is called BIOS (Basic Input Output System).

16 16 2/24/2016 Overlaying Overlay Manager Overlay Area Main Program Overlay 1 Overlay 2 Overlay 3 Secondary Storage Overlay 1Overlay 2Overlay 3Overlay 1 0K 5k 7k 12k Used when process memory requirement exceeds the physical memory space Split process space into multiple, sequentially runnable parts Load one overlay at a time

17 Logical vs. Physical Address Space The concept of a logical address space that is bound to a separate physical address space is central to proper memory management –Logical address – generated by the CPU; also referred to as virtual address –Physical address – address seen by the memory unit Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme

18 Memory-Management Unit ( MMU ) Hardware device that maps virtual to physical address In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory The user program deals with logical addresses; it never sees the real physical addresses

19 19 2/24/2016 Fragmentation 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. Reduce external fragmentation by compaction  Shuffle memory contents to place all free memory together in one large block.  Compaction is possible only if relocation is dynamic, and is done at execution time.  I/O problem Latch job in memory while it is involved in I/O. Do I/O only into OS buffers.

20 Dynamic relocation using a relocation register

21 Techniques Fixed Partitioning –Divide memory into partitions at boot time, partition sizes don’t change –Simple but has internal fragmentation Dynamic Partitioning –Create partitions as programs loaded –Avoids internal fragmentation, but must deal with external fragmentation Simple Paging –Divide memory into equal-size pages, load program into available pages Pages do not need to be consecutive –No external fragmentation, small amount of internal fragmentation

22 Techniques Simple Segmentation –Divide program into segments Each segment is contiguous, but different segments need not be –No internal fragmentation, some external fragmentation Virtual-Memory Paging –Like simple paging, but not all pages need to be in memory at one time –Allows large virtual memory space –More multiprogramming, overhead Virtual Memory Segmentation –Like simple segmentation, but not all segments need to be in memory at one time –Easy to share modules –More multiprogramming, overhead

23 23 2/24/2016 Multiprogramming with Fixed Partitions Divide memory into n (possibly unequal) partitions. Problem:  Fragmentation Total space may be enough for a new process, but it’s “fragmented” into parts Free Space Pid: 21 Pid: 405 0k 4k 16k 64k 128k

24 24 2/24/2016 Swapping Move a part of or the whole process to disk Allows several processes to share a fixed partition Processes that grow can be swapped out and swapped back in a bigger partition

25 25 2/24/2016 Swapping Monitor Disk User 1 User Partition

26 26 2/24/2016 Swapping Monitor User 1 Disk User 1 User Partition

27 27 2/24/2016 Swapping Monitor User 2 User 1 Disk User 1 User Partition

28 28 2/24/2016 Swapping Monitor Disk User 2 User Partition User 1

29 29 2/24/2016 Swapping Monitor Disk User 2 User Partition User 1

30 30 2/24/2016 Swapping Monitor Disk User 1 User 2 User Partition User 1

31 Fixed Partitioning Memory is divided into a set of partitions, each holds one program May have same or different sizes Equal-sized partitions –Program may be too large for one partition Programmer must handle overlays –Program may not be big enough to need a full partition Results in internal fragmentation

32 Fixed Partitioning Variable-sized partitions –Can fit partition size to program size –Placement Which partition do we use? –Want to use smallest possible partition –What if there are no large jobs waiting? Can have a queue for each partition size, or one queue for all partitions Easy to implement Tends to use memory poorly, especially for small programs (internal fragmentation) –Will work if the job sizes can be predicted in advance Used by IBM OS/MF, obsolete

33 Fixed Partitioning Equal-size partitions –any process whose size is less than or equal to the partition size can be loaded into an available partition –if all partitions are full, the operating system can swap a process out of a partition –a program may not fit in a partition. The programmer must design the program with overlays

34 Fixed Partitioning Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This is called internal fragmentation.

35

36 Placement Algorithm with Partitions Equal-size partitions –because all partitions are of equal size, it does not matter which partition is used Unequal-size partitions –can assign each process to the smallest partition within which it will fit –queue for each partition –processes are assigned in such a way as to minimize wasted memory within a partition

37

38 Dynamic Partitioning Create a new partition for each program Only allocate space required Tends to create “holes” as processes are swapped in and out (external fragmentation)

39 Dynamic Partitioning Tends to generate small holes in memory (external fragmentation) Compaction –Shifting processes so all of the available memory is in one block –Requires processor time to move items around in memory –Requires relocation hardware that can handle the change in addresses Cannot easily find all possible addresses in the program Need to move program without changing user addresses

40 40 2/24/2016 Fragmentation 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. Reduce external fragmentation by compaction  Shuffle memory contents to place all free memory together in one large block.  Compaction is possible only if relocation is dynamic, and is done at execution time.

41 41 2/24/2016 Compaction (Similar to Garbage Collection) Assumes programs are all relocatable (how supported?) Processes must be suspended during compaction Needed only when fragmentation gets very bad MonitorJob 3 Free Job 5Job 6Job 7Job 8 5 MonitorJob 3 Free Job 5Job 6Job 7Job 8 6 MonitorJob 3 Free Job 5Job 6Job 7Job 8 7 MonitorJob 3 Free Job 5Job 6Job 7Job 8 8 MonitorJob 3 Free Job 5Job 6Job 7Job 8 9

42 Dynamic Partitioning Partitions are of variable length and number Process is allocated exactly as much memory as required Eventually get holes in the memory. This is called external fragmentation Must use compaction to shift processes so they are contiguous and all free memory is in one block

43

44


Download ppt "Memory Management Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage."

Similar presentations


Ads by Google