Requirements, Partitioning, paging, and segmentation
Main Memory: The 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) 2
Memory Management Main memory holds The kernel Processes that are running, ready, or blocked Memory management Efficient and effective allocation of main memory to processes Performed by the operating system with support from the hardware 3
Requirements Relocation Protection Sharing Logical organization Physical organization 4
Relocation Why/What: Programmer does not know where the program will be placed in memory when it is executed While the program is executing, it may be swapped to disk and returned to main memory at a different location Consequences/Constraints: Memory references must be translated in the code to actual physical memory address 5
Protection Why/What: Protect process from interference by other processes Processes require permission to access memory in another processes address space. Consequences/Constraints: Impossible to check addresses at compile/link time since the program could be relocated Must be checked at run time 6
Sharing Allow several processes to access the same data Allow multiple process to share the same program text rather than have their own separate copy 7
Logical Organization Programs organized into modules stack, text, uninitialized data, or logical modules such as libraries, objects, etc. Code modules may be compiled independently Different degrees of protection given to modules read-only, execute-only Share modules 8
Unix Process Memory Layout Low Address Text Initialized Data Uninitialized Data (BSS) Heap Read from program file by exec Zeroed by exec High Address Stack argv[ ], env[ ] 9
Physical Organization Memory organized into two levels: main and secondary memory. Memory available for a program plus its data may be insufficient Overlaying allows various modules to be assigned the same region of memory Main memory relatively fast, expensive and volatile Secondary memory relatively slow, cheaper, larger capacity, and non-volatile 10
Hardware Support How to partition memory How to keep track of which partition belongs to which process How to prevent a process from accessing a partition that has not been allocated to it 11
Section 7.1 Assumptions Processes are never partially in memory Processes are stored contiguously 12
Memory Partitioning Virtual Memory Segmentation and/or Paging Non-Virtual memory approaches Partitioning - Fixed and Dynamic simple Paging simple Segmentation 13
Fixed Partitioning Partition available memory into regions with fixed boundaries Equal-size partitions Process size <= partition size can be loaded into available partition If all partitions are full, the operating system can swap a process out of a partition If program size > partition size, then programmer must use overlays 14
Fixed Partitioning - Equal Size Main memory use is inefficient Internal Fragmentation - Part of partition unused Operating System 8 M 8 M process 1 Unused 8 M 8 M 3 M 8 M 15
Fixed partitioning - unequal sizes Lessens the problem of Internal Fragmentation Operating System 8 M 2 M process 1 6 M 8 M 3 M 8 M 12 M 16
Fixed partition: placement algorithm 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 17
One Process Queue per partition Operating System New Processes 18
One Process Queue for all partitions Operating System New Processes 19
Pros and cons of fixed partitioning Pros: Fixed partitioning is simple and very little OS support is required. Cons: Since number of partitions is fixed, it limits the degree of multi-programming It is inefficient with small processes. If the sizes of processes is known beforehand then a reasonable size can be decided on. 20
Dynamic Partitioning Partitions are created dynamically Partitions are of variable size and number Partition sizes are determined based on requests Disadvantages: External Fragmentation - small holes in memory between allocated partitions Placement is more complicated - Must use compaction to shift processes so they are contiguous and all free memory is in one block 21
Example Dynamic Partitioning 22
Dynamic partition: placement alg. Operating system must decide which free block to allocate to a process Best-fit algorithm Chooses block that is closest in size to the request Results in minimally sized fragments requiring compaction Worst performer overall 23
Dynamic partition: placement alg. First-fit algorithm Starts scanning from beginning and choose first available block that is large enough. May have many process loaded in the front end of memory Fastest Best 24
Dynamic partition: placement alg. Next-fit Scan memory from the location of the last allocation and chooses the next available block that is large enough More often allocate a block of memory at the end of memory where the largest block is found Compaction is required to obtain a large block at the end of memory Compaction is more frequent 25
8K alloc 16K block 8K 12K First Fit 12K Last allocated block (14K) 22K 18K Best Fit 6K 2K 8K 6K 8K 6K Allocated block 14K Free block Next Fit 14K 36K Before 20K After 26
Buddy System Fixed and dynamic partitioning have their drawbacks Fixed partitioning Limits number of active processes Uses space inefficiently if the sizes of the processes don t match with that of the partitions Dynamic partitioning More complex to maintain Includes overhead of compaction 27
Buddy System Entire space available is treated as a single block of size 2 U If a request for a block of size s such that 2 U-1 < s <= 2 U Allocated entire block Otherwise Split block into two equal buddies Continues until smallest block greater than or equal to s becomes available requests rounded to power of two 28
Buddy System Advantages: coalesces adjacent buffers Disadvantage: performance (recursive coalescing is expensive) poor api 29
Buddy System Allocation Begin with one large block Suppose we want a block of size 16 256 128 64 32 16 8 4 2 1 30
Buddy System Allocation Begin with one large block Recursively subdivide 256 128 64 32 16 8 4 2 1 31
Buddy System Allocation Begin with one large block Recursively subdivide 256 128 64 32 16 8 4 2 1 32
Buddy System Allocation Begin with one large block Recursively subdivide 256 128 64 32 16 8 4 2 1 33
Buddy System Allocation Begin with one large block Yield 2 blocks size 16 256 128 64 32 16 8 4 2 1 34
Buddy System Allocation Begin with one large block Yield 2 blocks size 16 One of those blocks can be given to the program 256 128 64 32 16 8 4 2 1 35
Deallocation and Coalescing 256 128 64 32 16 8 4 2 1 36
Deallocation and Coalescing When a block becomes free, it tries to rejoin its buddy A bit in its buddy tells whether the buddy is free If so, they can glue together and make a block twice as big 256 128 64 32 16 8 4 2 1 37
Deallocation and Coalescing 256 128 64 32 16 8 4 2 1 38
Deallocation and Coalescing 256 128 64 32 16 8 4 2 1 39
Deallocation and Coalescing 256 128 64 32 16 8 4 2 1 40
Deallocation and Coalescing Coalescing strategies: Prompt at the point of deallocation (right away) Delayed wait until it s necessary (at allocation) Thorough coalesce the entire heap Demand coalesce only to satisfy an allocation request 256 128 64 32 16 8 4 2 1 41
Relocation Absolute or physical memory addresses are Based on the partitions into which the process is loaded Determined when A process is first loaded A process is reloaded after being swapped out Compaction occurs Not knowable at compile or link time Executable files use logical or relocatable addresses Relative addresses are one example of logical addresses Logical addresses must be translated to physical addresses at run time 42
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 43
Registers Used during Execution Assigned when a process enters the running state Base register starting address for the process Bounds register ending location of the process (where do the values come from?) Translate from relative addresses to physical addresses Base register is added to each relative address to produce absolute address If absolute address exceeds bounds register, an interrupt is generated to the operating system 44
Assumptions Sections 7.3 and 7.4 Processes are (still) never partially in memory Processes are not necessarily stored contiguously 45
Simple Paging Partition memory into equally sized pieces Pieces of memory are called frames Partition each process into frame-sized pieces Pieces of a process are called pages Pages are loaded in frames when process is loaded Not necessarily contiguous! No external fragmentation Internal fragmentation limited to last page Operating system maintains a page table for each process Contains an entry for each process page specifying the frame into which the page is loaded (when and how is the page table created?) Physical address = frame number * frame size + offset 46
47 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 efficient, the page (frame) size is a power of 2 Given a (page #, offset), find (frame #, offset) 2 n > number of pages => n bits to hold the page number 2 k > number of frames => 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 concatenation nm Physical address is concatenation km where k is the frame corresponding to page n (obtained from the page table) 48
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 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1... Frame # (k bits) 0 1 0 0 1 1 0 1 1 1 0 1 1 1 1 0 Physical Address 49
Address translation e.g. Main memory Frame # Physical address Value 0 0 E 0 1 F Page # Logical Address Value 0 2 G 0 3 H 0 0 A 0 1 B 0 2 C 0 3 D 1 4 E 1 5 F 1 6 G Page # Frame # 0 2 1 0 2 5 3 4 Page table 1 4? 1 5? 1 6? 1 7? 2 8 A 2 9 B 2 10 C 2 11 D 3 12? 1 7 H 3 13? 2 8 I 2 9 J 3 14? 3 15? 4 16 M 2 10 K 4 17 N 2 11 L 4 18 O 3 12 M 3 13 N 4 19 P 5 20 I 5 21 J 3 14 O 3 15 P Process 5 22 K 5 23 L 50
Segmentation Partition each process logical parts Pieces of a process are called segments For example, text segment, data segment, stack segment Segments are loaded in memory when process is loaded Not necessarily contiguous No internal fragmentation External fragmentation is a concern OS maintains a segment table for each process Contains an entry for each segment specifying the starting and ending addresses (when and how is the segment table created?) Physical address = starting address + offset 51
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 001011101110 0000010000000000 1 011110011110 0010000000100000 + Length Base Address 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 Physical Address 52