Paging, and segmentation
Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as many processes into memory as possible 2
Big Picture kernel memory proc struct kernel stack/u area Stack kernel stack/u area Stack kernel stack/u area Stack Data Text (shared) Data Text (shared) Data Text (shared) 3
Memory mgnt requirements Relocation Protection Sharing Logical organization Physical organization 4
Relocation When program loaded in memory, absolute memory locations assigned A process may occupy different partitions thus different absolute memory locations during execution (from swapping) Compaction will also cause a program to occupy a different partition which means different absolute memory locations 5
Addresses Logical Reference to a storage location independent of the current assignment of data or program to memory Translation must be made to the physical address Relative Address expressed as a location relative to some known point A relative address is an example of a logical address Physical The absolute address or actual location in memory 6
Hardware Support for Relocation Relative address Base Register Process Control Block Adder Program Bounds Register Comparator Absolute address Interrupt to operating system Data Stack Process image in main memory 7
Registers Used during Execution Base register Starting address for the process Bounds register Ending location of the process These values are set when the process is loaded and when the process is swapped in 8
Registers Used during Execution The value of the base register is added to a relative address to produce an absolute address The resulting address is compared with the value in the bounds register If the address is not within bounds, an interrupt is generated to the operating system 9
Simple paging Partition memory into small equal size pieces Pieces of memory are called frames Divide each process into the same size pieces Pieces of a process are called pages All process pages are loaded in memory frames when process is loaded Operating system maintains a page table for each process Contains the frame number for each process page Absolute address = frame number + offset 10
Simple paging Like fixed partitioning, but: Partitions are small A process can occupy more than 1 partitions Partitions do not need to be contiguous Some internal fragmentation (last page), but no external fragmentation 11
12 0 1 2 3 4 5 6 7 A.0 A.1 B.0 B.1 A.0 A.1 C.0 B.0 B.1 A.0 A.1 C.0 A.0 A.1 C.0 A.0 A.1 D.0 D.1 D.2 D.3 0 1 2 3 2 3 5 6 Process D Page Table 7 Free Frame List 0 1 2 3 4 5 6 7 Memory frames
Address translation in paging To make it convenient, the size of a page is a power of 2 Given a (page #, offset), find (frame #, offset) 2 n > number of pages in process => n bits to hold the page number 2 k > number of frames in memory => k bits to hold the frame number 2 m = number of bytes in a page => m bits to determine the offset within a page Logical address is nm Physical address is simply km where k is the frame that stores page n (obtained from the page table) 13
Address translation in paging Page # (n bits) Offset (m bit) 0 0 0 0 0 1 0 1 1 1 0 1 1 1 1 0 Process Page Table: 0 1 2 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 1... Frame # (k bits) 0 0 0 0 1 1 0 1 1 1 0 1 1 1 1 0 Physical Address 14
Address translation e.g. Main memory Frame # Physical address Value 0 0 E Page # Logical Address Value 0 1 F 0 2 G 0 0 A 0 1 B 0 2 C 0 3 D 1 4 E 1 5 F 1 6 G 1 7 H 2 8 I 2 9 J 2 10 K 2 11 L Page # Frame # 0 2 1 0 2 5 3 4 Page table 0 3 H 1 4 1 7 2 8 A 2 9 B 2 10 C 2 11 D 3 12 3 15 4 16 M 4 17 N 4 18 O 4 19 P 3 12 M 5 20 I 3 13 N 5 21 J 3 14 O 3 15 P Process 5 22 K 5 23 L 15
Segmentation Divide a process into unequal size blocks called segments There is a maximum segment length A logical address consists of two parts A segment number and an offset Since segments are not equal Segmentation is similar to dynamic partitioning 16
Address translation in segmentation Segment # Offset 0 0 0 1 0 0 1 0 1 1 1 1 0 0 0 0 Process Segment Table: 0 1 001011101110 011110011110 0000010000000000 0010000000100000 + Length Base Address 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 Physical Address 17