Requirements, Partitioning, 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 Q1 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 Q2 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 Protection and Relocation are interrelated 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 in programs since the program could be relocated Must be checked at run time 6
Sharing Sharing and Relocation are interrelated 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 Stack High Address argv[ ], env[ ] Q3 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 Q410
Hardware support for memory mgnt 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
Memory Partitioning Virtual Memory Segmentation and/or Paging Non Virtual memory approaches Partitioning Fixed and Dynamic Simple Paging Simple Segmentation 12
Fixed Partitioning Partition available memory into regions with fixed boundaries Equal size partitions Process whose 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 13
Fixed Partitioning Equal Size Main memory use is inefficient Internal Fragmentation Part of partition unused Operating System 8 M process 1 Unused 8 M 8 M 3 M 8 M 14
Fixed partitioning unequal sizes Lessons the problem of Internal Fragmentation Operating System 2 M process 1 6 M 8 M 3 M 8 M 12 M 15
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 16
One Process Queue per partition Operating System New Processes 17
One Process Queue for all partitions Operating System New Processes 18
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. 19
Dynamic Partitioning Partitions are created dynamically Partitions are of variable length and number Process is allocated exactly as much memory as required 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 Q5, 6 20
Example Dynamic Partitioning 21
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 22
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 23
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 24
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 Q7 25
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 26
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 Q827
Buddy System Advantages: coalesces adjacent blocks Disadvantage: performance (recursive coalescing is expensive) poor api 28
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 29
Buddy System Allocation Begin with one large block Recursively subdivide 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 Yield 2 blocks size 16 256 128 64 32 16 8 4 2 1 33
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 34
Deallocation and Coalescing 256 128 64 32 16 8 4 2 1 35
Deallocation and Coalescing When a block becomes free, it tries to rejoin its buddy 256 128 64 32 A bit in its buddy tells whether the buddy is free 16 8 4 2 If so, they can glue together and make a block twice as big 1 Q9 36
Deallocation and Coalescing 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 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 40