75
1 CHAPTER FOUR MEMORY MANAGEMENT Basics of memory Management • Swapping • Virtual memory • Page replacement • Segmentation

Chapter 4.pptx

Embed Size (px)

Citation preview

PowerPoint Presentation

1CHAPTER FOURMEMORY MANAGEMENTBasics of memory ManagementSwappingVirtual memoryPage replacementSegmentation 12Basics of memory managementProgram must be brought into memory and placed within a process for it to be runInput queue collection of processes on the disk that are waiting to be brought into memory to run the programSubdividing memory to accommodate multiple processesMemory needs to be allocated to ensure a reasonable supply of ready processes to consume available processor time23Basics of memory managementMemory management is the task carried out by the OS and hardware to accommodate multiple processes in main memoryIf only a few processes can be kept in main memory, then much of the time all processes will be waiting for I/O and the CPU will be idleHence, memory needs to be allocated efficiently in order to pack as many processes into memory as possible34Basics of memory managementMemory Management RequirementsRelocationProgrammer does not know where the program will be placed in memory when it is executedWhile the program is executing, it may be swapped to disk and returned to main memory at a different location (relocated)Memory references must be translated in the code to actual physical memory address45Basics of memory managementMemory Management RequirementsRelocationbinding of instructions and data to memory addresses can be done at any step along the way:Compile timeRequires recompiling for relocationLoad timeRequires reloading for relocationExecution timeSpecial hardware must be usedCommonly used in general purpose operating systems56Basics of memory managementMemory Management RequirementsProtectionProcesses should not be able to reference memory locations in another process without permissionImpossible to check absolute addresses at compile time Must be checked at run timeMemory protection requirement must be satisfied by the processor (hardware) rather than the operating system (software)Operating system cannot anticipate all of the memory references a program will make67Basics of memory managementMemory Management RequirementsProtectionWe first need to make sure that each process has a separate memory spaceneed the ability to determine the range of legal addresses that the process may accessMemory Address Spaceand ensure that the process can access only these legal addressesprovide this protection by using two registers, usually a base and a limitbase register holds the smallest legal physical memory addresslimit register specifies the size of the range78Basics of memory managementMemory Management Requirements SharingAllow several processes to access the same portion of memoryIf a number of processes are executing the same program, it is advantageous to allow each process access to the same copy of the program rather than have their own separate copyProcesses that are cooperating on some task may need to share access to the same data structure89Basics of memory managementMemory Management Requirements Logical OrganizationMain memory in a computer system is organized as a linear, or one-dimensional, address space, consisting of a sequence of bytes or wordsBut programs are written in modulesIf HW and OS can deal with user programs and data in the form of modules of some sort, then a number of advantages can be realizedModules can be written and compiled independentlyDifferent degrees of protection given to modules (read-only, execute-only)Share modules among processesAn address generated by the CPU is commonly referred to as a logical address910Basics of memory managementMemory Management Requirements Logical OrganizationThe actual address loaded into the memory-address register of the memory is known as a physical addressThe set of all logical addresses generated by a program is a logical address spacethe set of all physical addresses corresponding to these logical addresses is a physical address space.The run-time mapping from virtual to physical addresses is done by a hardware device called the memory-management unit (MMU).The base register is now called a relocation register.The value in the relocation register is added to every address generated by a user process at the time it is sent to memory1011Basics of memory managementMemory Management RequirementsPhysical OrganizationSecondary memory of large capacity can be provided for long-term storage of programs and data, while a smaller main memory holds programs and data currently in useMemory available for a program plus its data may be insufficientOverlaying allows various modules to be assigned the same region of memory with a main program responsible for switching the modules in and out as neededOverlaying wastes programmers timeProgrammer does not know how much space will be availablemoving information between these two levels of memory is a major concern of memory management (OS)1112Basics of memory managementMemory management systems can be divided into two classes: Those that move processes back and forth between main memory and disk during execution (swapping and paging)Those that do not sufficient main memory to hold all the programs at onceTwo simple memory management schemesMonoprogrammingMultiprogramming1213Basics of memory managementMultiprogrammingAlthough the following simple memory management techniques are not used in modern OS, they lay the ground for a proper discussion of virtual memoryfixed partitioningdynamic partitioningsimple pagingsimple segmentation1314Basics of memory managementEqual-sized fixed partitionsAny program, no matter how small, occupies an entire partition. If there is an available partition, a process can be loaded into that partitionbecause all partitions are of equal size, it does not matter which partition is usedIf all partitions are occupied by blocked processes, choose one process to swap out to make room for the new processinefficient use of Main memory. This is called internal fragmentation.1415Basics of memory managementUnequal-sized fixed partitions Any process whose size is less than or equal to a partition size can be loaded into the partitionIf all partitions are occupied, the operating system can swap a process out of a partitionTwo types:Using multiple queueUsing single queueMultiple queueWhen a job arrives, it can be put into the input queue for the smallest partition large enough to hold itProblemthe queue for a large partition is empty but the queue for a small partition is full1516Basics of memory managementUnequal-sized fixed partitions Single queuemaintain a single queue and whenever a partition becomes free, the job closest to the front of the queue that fits in it could be loaded into the empty partition and run Problemwaste a large partition on a small job Solutionsearch the whole input queue whenever a partition becomes free and pick the largest job that fitsProblemdiscriminates against small jobs as being unworthy of having a whole partition, whereas usually it is desirable to give the smallest jobs (often interactive jobs) the best service, not the worst. 1617Basics of memory managementUnequal-sized fixed partitions Single queueWays to allow small jobsTo have at least one small partition around. Such a partition will allow small jobs to run without having to allocate a large partition for them Another approach is to have a rule stating that a job that is eligible to run may not be skipped over more than k times. Each time it is skipped over, it gets one point.When it has acquired k points, it may not be skipped again. 1718Basics of memory managementUnequal-sized fixed partitions

1819Basics of memory managementDynamic PartitioningPartitions are of variable length and numberEach process is allocated exactly as much memory as it requiresEventually holes are formed in main memory. This is called external fragmentationMust use compaction to shift processes so they are contiguous and all free memory is in one blockUsed in IBMs OS/MVT (Multiprogramming with a Variable number of Tasks)191920Basics of memory management

Dynamic PartitioningA hole of 64K is left after loading 3 processes: not enough room for another processEventually each process is blocked. The OS swaps out process 2 to bring in process 42021Basics of memory management

Dynamic PartitioningAnother hole of 96K is createdEventually each process is blocked. The OS swaps out process 1 to bring in again process 2 and another hole of 96K is created...Compaction would produce a single hole of 256K2122Basics of memory managementUsed to decide which free block to allocate to a processGoal: to reduce usage of compactionPossible algorithms:Best-fit: choose smallest hole First-fit: choose first hole from beginning Next-fit: choose first hole from last placementWorst-fit: Choose the largest available holeDynamic Partitioning222223SwappingTwo general approaches to memory management can be usedSwapping: consists of bringing in each process in its entirety, running it for a while, then putting it back on the disk. Virtual memory: allows programs to run even when they are only partially in main memoryConcerns:how much memory should be allocated for a process when it is created or swapped in. If processes are created with a fixed size that never changes, then the allocation is simple: the operating system allocates exactly what is needed, no more and no less However, a problem occurs whenever a process tries to grow2324SwappingWhen a memory grows:If a hole is adjacent to the process, it can be allocated and the process allowed to grow into the holeIf the process is adjacent to another processmove the process to a hole in memory large enough for itone or more processes will have to be swapped out to create a large enough holeIf it is expected that most processes will grow as they run, it is probably a good idea to allocate a little extra memory whenever a process is swapped in or movedwhen swapping processes to disk, only the memory actually in use should be swapped2425Swapping

2526SwappingReasons for swapping in and outTime quantum expiredWhen a quantum expires, the memory manager will start to swap out the process that just finished and to swap another process into the memory space that has been freedHigher priority process arrivesIf a higher-priority process arrives and wants service, the memory manager can swap out the lower-priority process and then load and execute the higher-priority process2627SwappingRequirements for swappingAddress bindingIf binding is done at assembly or load time, then the process cannot be easily moved to a different locationa process that is swapped out will be swapped back into the same memory space it occupied previouslybacking storeSecondary storage must be large enough to accommodate copies of all memory images for all users, and it must provide direct access to these memory imagesSwapping timeThe memory block with smallest swapping time is swapped outIdleA process should be completely idle, to be swapped out2728CHAPTER FOURMEMORY MANAGEMENTBasics of memory ManagementSwappingVirtual memoryPage replacementSegmentation 2829Virtual MemoryPrograms are usually too big to fit in the available memory virtual memory is a mechanism in which the combined size of the program, data, and stack may exceed the amount of physical memory available for it. The operating system keeps those parts of the program currently in use in main memory, and the rest on the diskVirtual memory can also work in a multiprogramming system Most virtual memory systems use a technique called paging 2930Virtual MemoryA process executes only in main memory, that memory is referred to as real memory or physical memory A programmer or user perceives a potentially much larger memory that which is allocated on disk referred to as virtual memoryexample a user assumes 64K memory, but actually we have only 32K memory3031PagingBoth unequal fixed-size and variable-size partitions are inefficient in the use of memoryFixed-sized Internal fragmentationDynamic External fragmentationMain memory is partitioned into equal relatively small fixed-size chunks known as frames or page frames.Each process is also divided into small fixed-size chunks of the same size known as pages.Pages could be assigned to available chunks of memory3132Paging

3233Paging

3334PagingAt a given point in time, a list of free frames is maintained by the operating system Does the unavailability of sufficient contiguous memory space prevent the operating system from loading a process?No different pages can be stored on different framesMemory address used by programs are mapped into real memory address called physical addressThese program-generated addresses are called virtual addresses and form the virtual address space

3435PagingIf the processor encounters a virtual address that is not in main memory, it generates an interrupt indicating a memory access fault, known as page fault.The operating system puts the interrupted process in a blocking state and takes controlThe operating system maintains a page table which shows a frame location for each page of the process.Each process will have its own page tableConsider a computer with 16 bit (0-64K) addressing, 32 KB of physical memory and 4K frame size64 KB of virtual address space16 virtual pages8 page frames 3536

Paging3637PagingWhen the program tries to access address 0, for example, using the instruction MOV REG, 0virtual address 0 falls in page 0 (0 to 4095), which according to its mapping is page frame 2 (8192 to 12287) physical addressSimilarly, MOV REG,8192 (p2) MOV REG,24576 (p6)MOV REG,32780 page foult trap37Reading assignment The internal operation of the MMU 39Virtual Memory

3940CHAPTER FOURMEMORY MANAGEMENTBasics of memory ManagementSwappingVirtual memoryPage replacementSegmentation 4041Page Fetch PolicyThe Fetch policy determines when a page should be brought into main memoryDemand Paging: a page is brought into main memory only when a reference is made to a location on that pageWhen a process is first started, there will be a flurry of page faultsafter a time, matters should settle down and the number of page faults should drop to a very low levelPrepaging: pages other than the one demanded by a page fault are brought inineffective if most of the extra pages that are brought in are not referenced4142Page Replacement PolicyReplacement policy deals with the selection of a page in main memory to be replaced when a new page must be brought inIts sometimes difficult to explain because several interrelated concepts are involved:How many page frames are to be allocated to each active process Whether the set of pages to be considered for replacement should be limited to those of the process that caused the page fault or encompass all the page frames in main memoryAmong the set of pages considered, which particular page should be selected for replacement4243Page Replacement PolicyWhen a page-fault occurs, the OS has to:Find the location of the desired page on the disk. Find a free frame:If there is a free frame, use it.If there is no free frame, use a page-replacement algorithm to select a victim frame.Write the victim frame to the disk; change the page and frame tables accordingly.Read the desired page into the newly freed frame; change the page and frame tables.Restart the user process.4344Page Replacement AlgorithmPick a random page to evict at each page fault probability of removing heavily used pageit will probably have to be brought back in quicklyOther applications of page replacement algorithmsCache memoryWeb server4445Page Replacement AlgorithmOptimal Each page can be labeled with the number of instructions that will be executed before that page is first referenced When a page fault occurs, the page with the highest label should be removedProblemimpossible to implement 4546Page Replacement AlgorithmNot Recently Used (NRU)Most computers with virtual memory have two status bits associated with each page R is set whenever the page is referenced (read or written). M is set when the page is written to (i.e., modified) The bits are contained in each page table entry and these bits must be updated on every memory referenceWhen a process is started up, both page bits for all its pages are set to 0 by the operating system. Periodically (e.g., on each clock interrupt), the R bit is cleared, to distinguish pages that have not been referenced recently from those that have been. 4647Page Replacement AlgorithmNot Recently Used (NRU)The bits are contained in each page table entry and these bits must be updated on every memory referenceOperating system inspects all the pages and divides them into four categories based on the current values of their R and M bits:Class 0: not referenced, not modifiedClass 1: not referenced, modifiedClass 2: referenced, not modifiedClass 3: referenced, modifiedThe NRU algorithm removes a page at random from the lowest numbered nonempty class Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in at least one clock tick 4748Page Replacement AlgorithmNot Recently Used (NRU)Advantageseasy to understandmoderately efficient to implement gives a performance that, while certainly not optimal, may be adequate4849Page Replacement AlgorithmThe First-In, First-Out (FIFO) The operating system maintains a list of all pages currently in memory, with the page at the head of the list the oldest one and the page at the tail the most recent arrival. On a page fault, the page at the head is removed and the new page added to the tail of the listProblemNot efficient as it may remove frequently used pagesAdvantageLess overhead 4950Page Replacement AlgorithmThe Second Chance PageModification to FIFO that avoids the problem of throwing out a heavily used page by inspect the R bit of the oldest page looking for an old page that has not been referenced in the previous clock intervalIf all the pages have been referenced, second chance degenerates into pure FIFO After getting the oldest page, checks for the R bitIf it is 0, the page is both old and unused, so it is replaced immediatelyIf the R bit is 1, the bit is cleared, the page is put onto the end of the list of pages, and its load time is . . . . updated as though it had just arrived in memory 5051Page Replacement Algorithm

Arrival time5152Page Replacement AlgorithmSuppose that a page fault occurs at time 20. The oldest page is A, which arrived at time 0, when the process started. If A has the R bit cleared, it is evicted from memory, either by being written to the disk or just abandoned On the other hand, if the R bit is set, A is put onto the end of the list and its load time is reset to the current time (20). The R bit is also clearedProblemMoving pages around the list5253Page Replacement AlgorithmThe Clock Keep all the page frames on a circular list in the form of a clock, a hand points to the oldest pageWhen a page fault occurs, the page being pointed to by the hand is inspected. If its R bit is 0, the page is evicted, the new page is inserted into the clock in its place, and the hand is advanced one position. If R is 1, it is cleared and the hand is advanced to the next page. This process is repeated until a page is found with R = 0. Not surprisingly, this algorithm is called clock. It differs from second chance only in the implementation 5354Page Replacement Algorithm

Clock5455Page Replacement AlgorithmThe Least Recently Used (LRU)An algorithm based on the observation that pages that have been heavily used in the last few instructions will probably be heavily used again in the next fewwhen a page fault occurs, throw out the page that has been unused for the longest timeNecessary to maintain a linked list of all pages in memory, with the most recently used page at the front and the least recently used page at the rearImplementations:Store a counter, c, with each page and increment automatically after each instructionFor a machine with n page frames maintain a matrix of n n bits5556Page Replacement AlgorithmWhenever page frame k is referenced, the hardware first sets all the bits of row k to 1, then sets all the bits of column k to 0. At any instant, the row whose binary value is lowest is the least recently used, the row whose value is next lowest is next least recently used, and so forthThe workings of this algorithm are given in the following figure for four page frames and page references in the order0 1 2 3 2 1 0 3 2 35657Page Replacement Algorithm

0 1 2 3 2 1 0 3 2 35758Page Replacement AlgorithmThe Working Set Pages are loaded when requested by the process, not in advance.This strategy is called demand paging because pages are loaded only on demandAll pages of a program may not be usedThe set of pages that a process is currently using is called its working set A program causing page faults every few instructions is said to be thrashing 5859Page Replacement AlgorithmThe Working Set Many paging systems try to keep track of each process working set and make sure that it is in memory before letting the process runThis approach is called the working set model and reduces the page fault rate.Pages which are not in the working set are removed during page replacementTwo attributes are considered:Reference bit, RAge The working set of a process is the set of pages it has referenced during the past seconds of virtual timeVT - amount of CPU time a process has actually used since it started 5960Page Replacement AlgorithmThe Working Set Instead of defining the working set as those pages used during the previous 10 million memory references, we can define it as the set of pages used during the past 100 msec of execution time Each entry contains (at least) two items of information: the approximate time the page was last used and the R (Referenced) bitage = current virtual time time of last useR bit is examined, if it is 1, the current virtual time is written into the Time of last use field in the page table Its clearly in the working set and is not a candidate for removalIf R is 0, the page has not been referenced during the current clock tick and may be a candidate for removal compare age with T; and remove it if age greater than Tif R is 0 but the age is less than or equal to , the page is still in the working set remove the oldest page6061Page Replacement Algorithm

6162Page Replacement AlgorithmThe WSClock Page Replacement Algorithm An improved algorithm, that is based on the clock algorithm but also uses the working set informationThe data structure needed is a circular list of page frames, as in the clock algorithm Each entry contains the Time of last use field from the basic working set algorithm, as well as the R bit and the M bitAs with the clock algorithm, at each page fault the page pointed to by the hand is examined first. If the R bit is set to 1, the page has been used during the current tick so it is not an ideal candidate to remove. The R bit is then set to 0, the hand advanced to the next page, and the algorithm repeated for that page.6263Page Replacement Algorithm

A case when R=16364Page Replacement Algorithm

64Page Replacement Algorithm

6566CHAPTER FOURMEMORY MANAGEMENTBasics of memory ManagementSwappingVirtual memoryPage replacementSegmentation 6667Segmentation The virtual memory is one-dimensional because the virtual addresses go from 0 to some maximum address, one address after another.For many problems, having two or more separate virtual address spaces may be much better than having only oneThese completely independent address spaces are called segments A user program can be subdivided using segmentation, in which the program and its associated data are divided into a number of segmentsDifferent segments may have different lengthsA logical address using segmentation consists of two parts, in this case a segment number and an offset6768Segmentation

6869Segmentation Segmentation is similar to dynamic partitioning in the use of unequal-size segmentswith segmentation a program may occupy more than one partition, and these partitions need not be contiguousWith dynamic partitioning, a program occupies contiguous memory partitionsSegmentation eliminates internal fragmentationSegmentation and dynamic partitioning suffers from external fragmentation6970Segmentation Whereas paging is invisible to the programmer, segmentation is usually visible and is provided as a convenience for organizing programs and dataAnalogous to paging, a simple segmentation scheme would make use of a segment table for each process and a list of free blocks of main memoryEach segment table entry would have to give the starting address in main memory of the corresponding segmentlength of the segment, to assure that invalid addresses are not used7071Segmentation

7172Segmentation Consider an address of n X m bits, where the leftmost n bits are the segment number and the rightmost m bits are the offset, in the previous figure, n= 4 and m=12. Thus the maximum segment size is 212 = 4096.The following steps are needed for address translation:Extract the segment number as the leftmost n bits of the logical address.Use the segment number as an index into the process segment table to find the starting physical address of the segment.Compare the offset, expressed in the rightmost m bits, to the length of the segment. If the offset is greater than or equal to the length, the address is invalid.The desired physical address is the sum of the starting physical address of the segment plus the offset.7273Segmentation In our example, we have the logical address 0001001011110000, which is segment number 1, offset 752. Suppose that this segment is residing in main memory starting at physical address 0010000000100000.Then the physical address is 0010000000100000+001011110000=0010001100010000To summarize, with simple segmentation, a process is divided into a number of segments that need not be of equal size.When a process is brought in, all of its segments are loaded into available regions of memory, and a segment table is set up.7374Segmentation Advantages:simplifying the handling of data structures that are growing or shrinkinglinking up of procedures compiled separately on different segments is simplifiedchanging one procedures size doesnt affect the starting address of othersfacilitates sharing procedures or data between several processesE.g. shared librarydifferent segments can have different kinds of protection. 7475Segmentation

75