Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP5102/5122 Lecture 4 Operating Systems (OS) Memory Management phones off (please)

Similar presentations


Presentation on theme: "COMP5102/5122 Lecture 4 Operating Systems (OS) Memory Management phones off (please)"— Presentation transcript:

1 COMP5102/5122 Lecture 4 Operating Systems (OS) Memory Management phones off (please)

2 © De Montfort University, 2005Comp5102/5122 - L42 Lecture Outline Memory Management –Requirements Partitioning –Fixed, dynamic Placement Algorithms –First-fit,Best-fit,Worst-fit Swapping Virtual Memory –Paging, Segmentation

3 © De Montfort University, 2005Comp5102/5122 - L43 Memory Management Process scheduling defines when a process should be run. Memory Management defines where a process should be stored The problem ! No matter how much memory we install, some programmer will always want to use more!! How do we get a quart into a pint pot ??

4 © De Montfort University, 2005Comp5102/5122 - L44 Memory Management … Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as many processes into memory as possible should be able to run a program whose size is larger than the available real memory

5 © De Montfort University, 2005Comp5102/5122 - L45 Requirements 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 ( relocated ) Memory references in the code must be translated to actual physical memory addresses

6 © De Montfort University, 2005Comp5102/5122 - L46 Fixed Partitioning Main memory is divided into equal- (or unequal-) sized partitions and these partitions are assigned to processes. Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This is called internal fragmentation.

7 © De Montfort University, 2005Comp5102/5122 - L47 Dynamic Partitioning Partitions are of variable length and number Process is allocated exactly as much memory as required Eventually get holes in the memory. This is called external fragmentation Must use compaction to shift processes so they are contiguous and all free memory is in one block

8 © De Montfort University, 2005Comp5102/5122 - L48 Placement Algorithms Operating system must decide which free block to allocate to a process First-fit: scan the list segment and choose the block that is closest to the request. Best-fit: search the entire list and allocate the smallest hole that is big enough. Worst-fit: allocate the largest hole; must also search entire list. Produces the largest leftover hole.

9 © De Montfort University, 2005Comp5102/5122 - L49 Example: A variable partition memory has the following hole sizes in memory order: 200K, 600K, 400K, 800K, 350K, 70K A new process of size 300K enters the system. Determine where it will go according to the best-fit, first-fit and worst-fit algorithms and update the hole size status of the memory after the process has been added.

10 © De Montfort University, 2005Comp5102/5122 - L410 A Simple Model In the simplest memory management model –one process is in memory at a time that process is allowed to use as much memory as available user program 1 user program 2 operating system 0x0000 0x1000 0xFFFF

11 © De Montfort University, 2005Comp5102/5122 - L411 Overlays Split the program into small blocks Keep in main only those blocks that are needed at any given time. Needed when process is larger than amount of memory allocated to it.

12 © De Montfort University, 2005Comp5102/5122 - L412 Overlays … it is possible to increase the amount of memory available through the use of overlays main:initialisation input processing output initialisation main:initialisation input processing output main:initialisation input processing output input main:initialisation input processing output processing main:initialisation input processing output operating system 0x0000 0x1000 0xFFFF

13 © De Montfort University, 2005Comp5102/5122 - L413 Swapping Moving processes to and fro between main memory and hard disk is called swapping Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher- priority process can be loaded and executed.

14 © De Montfort University, 2005Comp5102/5122 - L414 Virtual Memory uses locality of reference in which most commonly used instructions and data are referenced: –instruction execution is localised either within loops or heavily used subroutines, –data manipulation is on local variables or upon tables or arrays of information. Virtual memory can also be defined as: A mapping from a virtual address space to a physical address space.

15 © De Montfort University, 2005Comp5102/5122 - L415 0: 1: N-1: A System with Virtual Memory (paging) Address Translation: MMU converts virtual addresses to physical addresses via OS-managed lookup table (page table) CPU Memory 0: 1: P-1: Page Table Disk Virtual Addresses Physical Addresses

16 © De Montfort University, 2005Comp5102/5122 - L416 Paging Partition memory into small equal-size chunks and divide each process into the same size chunks The chunks of a process are called pages and chunks of memory are called frames Operating system maintains a page table for each process –contains the frame location for each page in the process –each memory reference consist of a page number and offset within the page

17 © De Montfort University, 2005Comp5102/5122 - L417 virtual page numberpage offset virtual address physical page numberpage offset physical address 0p–1 address translation pm–1 n–10p–1p Page offset bits don’t change as a result of translation VM Address Translation Parameters –P = 2 p = page size (bytes). –N = 2 n = Virtual address limit –M = 2 m = Physical address limit

18 © De Montfort University, 2005Comp5102/5122 - L418 Paging … Each process has its own page table Each page table entry contains the frame number of the corresponding page in main memory A bit is needed to indicate whether the page is in main memory or not

19 © De Montfort University, 2005Comp5102/5122 - L419 Page Tables Memory resident page table (physical page address) Physical Memory Disk Storage (swap blocks) Valid 1 1 1 1 1 1 1 0 0 0 Virtual Page Number

20 © De Montfort University, 2005Comp5102/5122 - L420 Execution of a Program Operating system brings into main memory a few blocks of the program An interrupt is generated when a block is needed that is not in main memory - page fault Operating system places the process in a blocking state and a DMA is started to get the page from disk An interrupt is issued when disk I/O is complete which causes the OS to place the affected process in the Ready Queue

21 © De Montfort University, 2005Comp5102/5122 - L421 Thrashing Swapping out a piece of a process just before that piece is needed The processor spends most of its time swapping pieces rather than executing user instructions

22 © De Montfort University, 2005Comp5102/5122 - L422 Replacement Policy Which page should be replaced? –Page that is least likely to be referenced in the near future Each page in memory usually has two bits associated with it: R referenced bit: set when page is accessed (read/written) M m odified bit: set when page is written to These bits would be set by MMU when a page is read/written and cleared by the OS - in particular the OS clears the R bits on every clock interrupt.

23 © De Montfort University, 2005Comp5102/5122 - L423 When a page fault occurs there are four possible scenarios: R = 0, M = 0: not referenced, not modified R = 0, M = 1: not referenced, but modified R = 1, M = 0: referenced, but not modified R = 1, M = 1: referenced and modified

24 © De Montfort University, 2005Comp5102/5122 - L424 Replacement algorithm NRU, Simple to operated and adequate performance. If R = 0 and M = 0 (not referenced, not modified) –swap one of these pages else if R = 0, M = 1 (not referenced, but modified) – swap one of these pages else if R = 1, M = 0 (referenced, but not modified) – swap one of these pages else swap one of the R = 1, M = 1 pages Note that any page with M = 0 can be overwritten since that have not been written to since being read from disk.

25 © De Montfort University, 2005Comp5102/5122 - L425 Replacement algorithm … FIFO: Not very good - pages are kept in a list and the one at the end of the list is swapped out even if still in use. LRU: swap out or discard the page which has been least used (used by Atlas). Each page has a counter which is incremented by the MMU when the page is accessed. On a page fault swap page with lowest count.

26 © De Montfort University, 2005Comp5102/5122 - L426 Replacement algorithms … NFU: Similar to LRU but implemented in software; the OS adds the R bit to the page counter on each clock interrupt (then clears the R bit). On a page fault swap page with lowest count. Find out about the problems with LRU and NFU.

27 © De Montfort University, 2005Comp5102/5122 - L427 Segmentation OS creates multiple virtual address spaces, each starting at an arbitrary location and with arbitrary length. The start of a segment is virtual address 0. Each process can be assigned a different segment, independent of others. Relocation done at run time by the virtual memory mapping mechanism. Segmentation is implemented in a manner much like paging, through a lookup table. The difference is that each segment descriptor in the table contains the base address of the segment and a length.

28 © De Montfort University, 2005Comp5102/5122 - L428 Segmentation with Paging Some operating systems allow for the combination of segmentation with paging. If the size of a segment exceeds the size of main memory, the segment may be divided into equal size pages.

29 © De Montfort University, 2005Comp5102/5122 - L429 Segmentation with paging … The address consists of: segment number, page within the segment and offset within the page. The segment number is used to find the segment descriptor and the address within the segment is used to find the page frame and the offset within that page.

30 © De Montfort University, 2005Comp5102/5122 - L430 Summary A number of processes can coexist in memory using a variety of allocation techniques and placement policies Main problem is the creation of unusable holes as processes terminate making it sometimes difficult to accommodate new processes Compaction can be used to shuffle processes and create larger more usable space but the large overheads involved make it not always feasible to carry out in real time. When swapping is used, the system can handle more processes than it has room for in memory … virtual memory concept.. and hence paging and segmentation When memory is full, a decision must be made as to which page or pages are to be replaces (replacement policy) http://www.cs.umass.edu/~weems/CmpSci535/535lecture9.html

31 © De Montfort University, 2005Comp5102/5122 - L431 Example Page Sizes

32 © De Montfort University, 2005Comp5102/5122 - L432 Swap file A swap file (or swap space or, in Windows NT, a page) is a space on a hard disk used as the virtual memory extension of a computer's real memory (RAM). Having a swap file allows your computer's operating system to pretend that you have more RAM than you actually do. The least recently used files in RAM can be "swapped out" to your hard disk until they are needed later so that new files can be "swapped in" to RAM. In larger operating systems (such as IBM's OS/390), the units that are moved are called pages and the swapping is called paging. One advantage of a swap file is that it can be organized as a single contiguous space so that fewer I/O operations are required to read or write a complete file. In general, Windows and Unix-based operating systems provide a default swap file of a certain size that the user or a system administrator can usually change.

33 © De Montfort University, 2005Comp5102/5122 - L433 thrashing Thrashing is computer activity that makes little or no progress, usually because memory or other resources have become exhausted or too limited to perform needed operations. When this happens, a pattern typically develops in which a request is made of the operating system by a process or program, the operating system tries to find resources by taking them from some other process, which in turn makes new requests that can't be satisfied. In a virtual storage system (an operating system that manages its logical storage or memory in units called pages), thrashing is a condition in which excessive paging operations are taking place. A system that is thrashing can be perceived as either a very slow system or one that has come to a halt.


Download ppt "COMP5102/5122 Lecture 4 Operating Systems (OS) Memory Management phones off (please)"

Similar presentations


Ads by Google