Notes for lecture on 2/25/09 ------ Sharing Main Memory: Segmentation & Paging ------ Question: How do we allocate memory to processes? > I. Simple uniprogramming with a single segment per process: >> One program in memory at a time. >>> Highest memory holds OS. >>> Process is allocated memory starting at 0 (or J), up to (or from) the OS area. >>> Process always loaded at 0. >>> Advantages * Low overhead * Simple * No need to do relocation. >>> Disadvantages * No protection - process can overwrite OS >>> Process limited to size of memory * No good way share - only one process at a time > II. Relocation - load program anywhere in memory. >> Idea is to use loader or linker to load program at an ar- bitrary memory address. >> Note that program can't be moved (relocated) once loaded. >> This scheme (#2) is essentially the same as #1, but the ability to load at any address will be used in #3 below. > III. Simple multiprogramming with static software relocation, no protection, one segment per process: > Highest or lowest memory holds OS. > Processes allocated memory starting at 0 (or N), up to the OS area. > When a process is initially loaded, link it so that it can run in its allocated memory area > Can have several programs in memory at once, each loaded at a different (non overlapping) address. > Advantages: >> Allows multiprogramming without swapping processes in and out. >> Makes better use of memory >> Higher CPU use due to more efficient multiprogram- ming. > Disadvantages: >> No protection - jobs can read or write others. >> External fragmentation >> Overhead for variable size memory allocation. >> Still limited to size of physical memory. >> Hard to increase amount of memory allocation. > IV. Dynamic memory relocation: instead of changing the ad- dresses of a program before it's loaded, change the address dynamically during every reference. > Figure of a processor and a memory box, with a memory re- location box in between. > There are many types of relocation - to be discussed. > Dynamic relocation leads to two views of memory, called address spaces. We have the virtual address space and the real address space. Each process has its own virtual address space. With static relocation we force the views to coincide. In some systems, there are several levels of mapping. ** Several types of dynamic relocation. ** > Base & bounds relocation: >> Two hardware registers: base register for process, bounds register that indicates the last valid address the process may generate. >> Real address = base + virtual address >>> IF virtual_address < bounds, and VA>= 0 >> In parallel, the real address is generated by adding it to the base register. >>> This is a form of translation. >>> Discuss why comparison is done in parallel. >> On each memory reference, the virtual address is compared >> Advantages: >>> Each process appears to have a completely private memory of size equal to the bounds register plus 1. >>> Processes are protected from each other. >>> No address relocation is necessary when a process is loaded. >>> Compaction is possible. >> Disadvantages: >>> Still limited to size of main memory. >>> External fragmentation (between processes) >>> Overhead for allocating variable size spaces in memory. >> OS must be able to change value of relocation registers. >>> OS loads new process and sets base and bounds regis- ters. >>> OS schedules process, and sets base and bounds regis- ter. >>> When tasks are switched, must be able to swap base, bounds and PC registers simultaneously. >>> These imply that OS must run with base and bounds re- location turned off - otherwise, would affect itself when running. (Or would need its own set of base and bounds registers.) >>> Use of base and bounds controlled by status bit, usu- ally in PSW or SSW, or similar control register. >> Users must not be able to change values of base and bounds registers >>> Otherwise, no protection between users. Can trash others or OS. >> Problem: how does OS regain control once it has given it up? >>> OS is entered on trap (including SVC) or interrupt. >>> When OS is entered, use of base and bounds must be disabled. (I.e. bit in PSW is reset.) >>> Typically, trap handler loads new control register values. >> Base & bounds is cheap -- only 2 registers -- and fast -- the add and compare can be done in parallel. *** Can consider three types of systems using base and bounds re- gisters: > Uniprogramming - single user region. Bring a user in, and run him. > Multiprogramming with Fixed Partitions - (OS/MFT) - par- tition memory into fixed regions (may be different sizes). User goes into region of given size. >> Not very flexible. > Multiprogramming with Variable Partitions (OS/MVT) - par- titions are dynamically variable. *** Note that we can do any of the three above schemes without base and bounds registers - just load programs into region at appropriate base address. > Task Switching >> We can now switch between processes very cheaply - don't have to reload memory, just change contents of process control block. >> We can also run processes which are not in memory - how? >>> Find empty area of memory in which to place process - how?. >>>> Remove one or more processes from memory, if necessary, in order to find space. >>> Copy new process (from disk) into memory. >> If only one process fits in memory, have to wait for swap to take place. >>> If several processes fit in memory, can swap one while executing the other. > V. Multiple segments - Segmentation. >> Divide virtual address space into several "segments". >>> This is not the same as the "segments" of linkers and loaders. >> Use a separate base and bound for each segment, and also add protection bits (read, write, execute), and valid bit. (Also will want dirty bit.) > Each address is now >> Each memory reference indicates a segment and offset in one or more of three ways: >>> Top bits of address select segment, low bits the offset. This is the most common, and the best. >>> Or, segment is selected implicitly by the instruction (e.g. code vs. data, stack vs. data, which base re- gister is used, or 8086 prefixes). >>> Instruction specifies directly or indirectly a base register for the segment. >>> Subprograms (procedures, functions, etc.) can be separate segments. > Segments typically are associated with logical partitions of your process address space - e.g. code, data stack. Or, each module or procedure can be a separate segment. >> Need either segment table or segment registers to hold the base and bounds for each segment. >>> Draw picture of segment table, with segment table en- tries. >> Memory mapping procedure consists of table lookup + add + compare. > Address translation for segmentation >> Have segment table - maps segment number to [Segment base address, segment length (limit), protection bits, valid bit, reference bit, dirty bit] >>> This info is in Segment Table Entry (STE) >>> Diagram of segment table. >>> Segment descriptor >> Need some hardware to automatically map virtual (segment number, word number) to real address. > Real address = segment_table(segment #) + word number. >> Invalid if word_number > limit. (Note that we do test without adding bound to both) >>> Also valid bit must be on, and permission bits must permit access. >> Need more hardware to make it go fast >> Have Segment Table Base Register (STBR) point to base of segment table (for hardware to use) > Alternate approach - if there are a small number of segments, can have segment registers - one register per segment. > Can also multiplex a small number of segment registers among a large number of segments --------- END --------