Lecture 19 Reminder: Homework 4 due Friday Questions? Wednesday, February 23 CS 470 Operating Systems - Lecture 19 1 Outline Discuss Watson video Memory management Storage organization issues Storage management issues Memory addressing Static, complete, contiguous storage organization Fragmentation Wednesday, February 23 CS 470 Operating Systems - Lecture 19 2 Watson Video Given the hardware architecture shown, what kind of OS do you think is running on Watson? What do you think was the easiest part to accomplish in building Watston? What do you think was the hardest part to accomplish in building Watson? Wednesday, February 23 CS 470 Operating Systems - Lecture 19 3 Memory Management Chapter 7 ended the section on processes and CPU management. Chapter 8 starts a new section on memory management. Memory management deals with two separate, but related, topics: Storage organization - how memory is used Storage management - how/when programs get put into or taken out of memory Wednesday, February 23 CS 470 Operating Systems - Lecture 19 4 Storage Organization Issues The issues in storage organization include providing support for: single vs. multiple processes complete vs. partial allocation fixed-size vs. variable-size allocation contiguous vs. fragmented allocation static vs. dynamic allocation of partitions Different choices for each of these issues results in a different storage organization. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 5 Storage Management Issues The issues in storage management include: How does memory get associated with a process? Where do new programs go in memory? For partial, dynamic allocation schemes, when is memory reallocated to another process? These issues deal with the allocation policies of memory as a resource. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 6 Memory Addressing (Cache) RAM Disk Storage in a computer system is a hierarchy. Usually, a program reside on disk as a binary image file. A program needs to be in main memory (RAM) in order to execute. The OS loads the binary image into memory and attaches it to a process. In most systems, processes can be located almost anywhere in physical memory. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 7 Memory Addressing In the program source, memory locations are addressed symbolically. The actual physical address can be bound to a particular symbolic location at different times: Compile-time: absolute address must be known. Recompile if it changes Load-time: addresses are relative to some base address. Bind the base address before run-time. Run-time: for most modern OS's, memory is virtual and the logical to physical mapping happens as the program runs. Use special hardware to make this work. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 8 Early Storage Organizations Simplest organization is a single process in memory at a time with absolute addressing. What is the main limitation of this scheme with respect to running a single program? Wednesday, February 23 CS 470 Operating Systems - Lecture 19 9 Early Storage Organizations Techniques for allowing larger programs Dynamic loading: load only those parts being used, starting with main( ). Keep track of starting address of each module. Overlay: sometimes different parts of a program are used at different times. Set up system to load all the common parts, and share the rest of memory between overlays. Dynamic linking: usually everything a program needs is statically linked into the binary image. This scheme shares images of libraries (mostly for multiprogramming systems). Wednesday, February 23 CS 470 Operating Systems - Lecture 19 10 Early Storage Organizations The problem with these techniques is that they require the programmer to set up the memory. This can be complex and requires detailed knowledge of the program and the hardware. Want the OS to take care of managing the memory so that programs of all sizes and types can run efficiently. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 11 Address Translation Most modern OS's use virtual memory. The CPU/user process generates logical addresses based in a virtual address space. The actual location mapped to a logical address is its physical address and is obtained using the memory management unit (MMU) of the hardware. CPU (user pgm) Wednesday, February 23 ------------------> logical addr MMU --------------------> physical addr CS 470 Operating Systems - Lecture 19 Main Memory 12 Static, Complete, Contiguous Organization Single process systems are not interesting, so we will assume a multi-programmed system. Start with static, complete, contiguous storage organization. That is, when a program is to be run, it is allocated all its memory space in a contiguous area of memory at load-time. The base address of this area is loaded into a relocation register of the MMU, and the MMU simply adds the base address to every logical address and does a limit check. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 13 Static, Complete, Contiguous Organization CPU (user pgm) 346 ---------------> logical addr MMU: main mem reloc. reg. 14346 |_________| 14000 ------------------> 14346 |_________| + physical |_________| limit chk addr | Note that the CPU/user process never sees the physical address, so the program can be loaded anywhere in memory. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 14 Fixed-Size Partitions If all programs are allocated the same size piece of memory (i.e., fixed-size partitions), the multi-programming is easy to provide. On admittance, the OS loads program into one of the partitions and stores the base address in the PCB. On a context switch, the base address from the PCB is loaded into the MMU relocation register. The limit check is whatever the size of a partition is. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 15 Fixed-Size Partitions P7 P2 P3 P6 P5 Wednesday, February 23 OS determines when a process can be loaded. If there is an available partition, admit. If no available partion, wait. Release partition on termination. Still limits size of program. CS 470 Operating Systems - Lecture 19 16 Variable-Size Partitions Could also have variable-size partitions. Add a limit register to the MMU and a limit field in the PCB to be used in the limit check. Also need more management. A (very) small example: Physical memory addresses 0-2560K (2MB), OS takes 400K at the low end, leaving 2160K for users. As with process management, give a scenario (on the next slide) Wednesday, February 23 CS 470 Operating Systems - Lecture 19 17 Variable-Size Partitions Memory request CPU burst time (ms) P1 600K 10 P2 1000K 5 P3 300K 20 P4 700K 8 P5 500K 15 The processes arrive at time 0 in the order specified. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 18 Variable-Size Partitions Only processes in memory can compete for the CPU. Assuming an RR scheduler, OS can only load P1, P2, and P3 into the first 1900K (600K+1000K+300K) of memory, leaving a 260K (2160K-1900K) hole of unallocated memory. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 OS - 400K P1 - 600K P2 - 1000K P3 - 300K Hole - 260K 19 Variable-Size Partitions If q = 1, then P2 terminates at t = 14, leaving a 1000K hole. This is enough to load P4, leaving a 300K hole. P1 terminates at t = 28, and OS allocates P5, leaving a 100K hole. OS - 400K P5 - 500K Hole - 100K P4 - 700K Hole - 300K P3 - 300K Hole - 260K Wednesday, February 23 CS 470 Operating Systems - Lecture 19 20 Variable-Size Partitions In this scheme, releasing is easy, just add hole to the head of a linked list. Allocation is much harder - if more than one hole is big enough, which hole? First fit Best fit Worst fit ?? Wednesday, February 23 CS 470 Operating Systems - Lecture 19 21 Fragmentation Both schemes suffer from fragmentation, that is, memory space that is unused due to the memory organization. Variable-size partition organization causes external fragmentation. That is, the unused memory is outside of any allocation. In particular, eventually all the holes will be small, so while there may be enough total free space to run another program, there is not enough contiguous to do so. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 22 Fragmentation Fixed-size partition organization causes internal fragmentation. That is, a process has been allocated more memory than it needs to run. On average, a partition will be only half filled. Thus even when all of the memory is allocated, the system could have run more programs had the partitions been smaller. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 23 Fragmentation Since fixed-size partitions are determined by the memory architecture, there is not much that can be done about internal fragmentation. But we can and should do something about external fragmentation when using variable-size partitions. E.g., in the earlier scenario, when P 1 terminates, it releases 1000K of which P 4 took 700K, leaving a 300K hole. If this free space were added to the other 260K hole, 560K would be large enough to hold P5 as well. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 24 Fragmentation A simple technique to handle fragmentation is to coalesce holes that are next to each other forming one larger hold of contiguous memory. This can be done just after a process terminates, but would not handle our scenario. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 25 Fragmentation A better technique is to do compaction. That is, put all the free space together by moving processes. E.g., Slide all process towards one end Move processes from one end to the other Move processes in the middle to the ends Deallocation becomes more difficult. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 26 Static, Complete, Contiguous Organization Generally, fixed-size partitions are favored over variable-size partitions. They are faster to allocate and deallocate, and are simpler to manage. Just need to make sure the partition size is big enough, but not too big. Next class start looking at non-contiguous storage techniques. Wednesday, February 23 CS 470 Operating Systems - Lecture 19 27