Operating systems Module 11 Main memory introduction Part 1 Tami Sorgente 1
MODULE 11 MAIN MEMORY INTRODUCTION Background Swapping Contiguous Memory Allocation Noncontiguous Memory Allocation o Segmentation o Paging Tami Sorgente 2
BACKGROUND 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, causing a stall Cache sits between main memory and CPU registers Protection of memory required to ensure correct operation Tami Sorgente 3
MEMORY-MANAGEMENT UNIT (MMU) Hardware device that at run time maps virtual/logical to physical address The user program deals with logical addresses; it never sees the real physical addresses o Execution-time binding occurs when reference is made to location in memory o Logical address bound to physical addresses Tami Sorgente 4
SCHEMATIC VIEW OF SWAPPING Tami Sorgente 5
CONTIGUOUS VS NONCONTIGUOUS OS OS base limit Free Free P1 P2 Free P1 P2 P1 P2 contiguous noncontiguous Tami Sorgente 6
CONTIGUOUS ALLOCATION Main memory must support both OS and user processes Contiguous allocation is one early method Main memory usually into two partitions: o o o Resident operating system, usually held in low memory with interrupt vector User processes then held in high memory Each process contained in single contiguous section of memory Tami Sorgente 7
DYNAMIC STORAGE-ALLOCATION PROBLEM First-fit: Allocate the first hole that is big enough Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size o Produces the smallest leftover hole Worst-fit: Allocate the largest hole; must also search entire list o Produces the largest leftover hole Tami Sorgente 8
CONTIGUOUS VS NONCONTIGUOUS OS OS base limit Free Free P1 P2 Free P1 P2 P1 P2 contiguous noncontiguous Tami Sorgente 9
FRAGMENTATION External Fragmentation total memory space exists to satisfy a request, but it is not contiguous Reduce external fragmentation by storage compaction o Shuffle memory contents to place all free memory together in one large block Internal Fragmentation allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Tami Sorgente 10
SEGMENTATION A program is a collection of segments o A segment is a logical unit such as: main program procedure function method object local variables, global variables common block stack symbol table arrays segment 0 segment 1 segment 2 segment 3 segment 4 Tami Sorgente 11
SEGMENTATION variable size o units are smaller and have meaning noncontiguous map each segment separately Advantages: segments correspond to semantic (meaningful) units better protection (security) sharing and reliability Disadvantages: variable size unit of memory allocation. o First fit, best fit, worst fit External fragmentation Tami Sorgente 12
PAGING 0 1 2 3 4 5 6 7 Logical/virtual memory pages 0 1 1 4 2 3 3 7 4 5 6 7 Frame # Page table 0 1 Page 0 2 3 Page 2 4 Page 1 5 6 7 Page 3 8 9 10 11 12 13 14 Physical memory frames 13
PAGING noncontiguous Avoids external fragmentation Avoids problem of varying sized memory chunks Divide physical memory into fixed-sized blocks called frames Divide logical memory into blocks of same size called pages Keep track of all free frames Set up a page table to translate logical to physical addresses Backing store likewise split into pages Internal fragmentation Tami Sorgente 14
Operating systems Module 11 Main memory introduction PART 11 memory placement exercise Tami Sorgente 15
MEMORY PLACEMENT EXERCISE Job Size(KB) Time Units J1 500 3 J2 250 4 J3 200 5 J4 350 3 J5 60 5 J6 300 3 J7 400 2 Definitions Coalescing holes- merging holes to form one large hole Storage compaction ( garbage collection ) move all occupied areas of memory to one end Analyze execution of 7 jobs specified: Suppose main memory is 1 MB OS designed: First Fit strategy CPU switches job every time unit OS performs coalescing holes (cost 1 time unit) OS applies storage compaction every 20 time units When a new job is placed in memory it executes first How many time units are needed for completion of 7 jobs Tami Sorgente 16
MEMORY PLACEMENT EXERCISE Job Size(KB) Time Units J1 500 3 J2 250 4 J3 200 5 J4 350 3 J5 60 5 J6 300 3 J7 400 2 Suppose main memory is 1 MB First Fit strategy CPU switches job every time unit (adjacent holes) OS performs coalescing holes (cost 1 time unit) OS applies storage compaction every 20 time units Tami Sorgente 17
Job Size(KB) Time Units J1 500 3 J2 250 4 J3 200 5 J4 350 3 J5 60 5 J6 300 3 J7 400 2 Hole = 500 Hole = 90 Hole = 340 Hole = 250 Hole = 40 Hole = 50 Hole = 50 Hole = 50 Hole = 50 0 10 CH 15 17 18
Time 18: External fragmentation Time 20 to 24: Storage compaction Job Size(KB) Time Units J1 500 3 J2 250 4 J3 200 5 J4 350 3 J5 60 5 J6 300 3 J7 400 2 Hole = 350 Hole = 40 Hole = 40 Hole = 440 Hole = 50 Hole = 50 Hole = 40 SC SC SC SC CH CH 17 20 24 32 19