Download presentation
Presentation is loading. Please wait.
Published byClemence Williams Modified over 8 years ago
1
CSNB224 – AAG 2007 Memory Management Chapter 7
2
CSNB224 – AAG 2007 Memory Management In uniprogramming system, memory is divided into two parts: for the OS for the program that currently being executed – user part In multiprogramming system, the user part of the memory must be further subdivided to accommodate multiple processes. The task of subdivision is carried out dynamically by the OS memory management.
3
CSNB224 – AAG 2007 Memory Management Memory needs to be allocated efficiently to pack as many processes into memory as possible. This is because effective memory management is important in multiprogramming system.
4
CSNB224 – AAG 2007 Memory Management Requirements Five requirements of memory management: 1. Relocation 2. Protection 3. Sharing 4. Logical Organization 5. Physical Organization
5
CSNB224 – AAG 2007 Memory Management Requirements Relocation 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 memory at a different location (relocate process at different area) The location for the process is unknown and the process must be allowed to moved in and out due to swapping.
7
CSNB224 – AAG 2007 Memory Management Requirements These facts raise some technical concerns related to addressing, as illustrated in Figure 7.1. The OS will need to know the location of: process control information the execution stack the entry point to begin the execution of the program for the process. Because OS is managing memory and is responsible for bringing this process into memory, these addresses are easy to recognize.
8
CSNB224 – AAG 2007 Memory Management Requirements However the processor need to deal with memory references within the program. Branch inst. contain an address to reference the instruction to be executed next. Data reference inst. contain the address of the byte or word of data referenced. So the processor and OS must be able to translate the memory references found in the code of the program into actual physical memory address reflecting the current location of the program in memory.
9
CSNB224 – AAG 2007 Memory Management Requirements Protection Processes should not be able to reference memory locations in another process without permission Impossible to check absolute addresses in programs since the program could be relocated. All memory references must be checked at run time to ensure that it refer to the only memory space allocated to the process
10
CSNB224 – AAG 2007 Memory Management Requirements Memory protection requirement must be satisfied by the processor rather than OS. OS cannot anticipate all the memory references that a program will make Time consuming to screen each program in advance for possible memory referenced violation. It is only possible to assess the permissibility of a memory reference at the time of execution of the instruction making reference.
11
CSNB224 – AAG 2007 Memory Management Requirements Sharing Allow several processes to access the same portion of memory. So, any protection mechanism must have the flexibility, example: If 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 copy e.g. ECHO procedure
12
CSNB224 – AAG 2007 Memory Management Requirements Memory management system must therefore allow controlled access to shared areas of memory without compromising essential protection.
13
CSNB224 – AAG 2007 Memory Management Requirements Logical Organization Programs are written in modules Modules can be written and compiled independently Different degrees of protection given to modules (read-only, execute-only) Share modules ##Read on your own
14
CSNB224 – AAG 2007 Memory Management Requirements Physical Organization Memory available for a program plus its data may be insufficient Overlaying allows various modules to be assigned the same region of memory Programmer does not know how much space will be available ##Read on your own
15
CSNB224 – AAG 2007 Memory Management Principal operation of memory management is to bring programs into memory for execution by the processor. In most modern multiprogramming system it invokes virtual memory (VM) that use both segmentation and paging techniques. First look at simpler technique that do not use Virtual Memory Partitioning Simple Paging Simple Segmentation
16
CSNB224 – AAG 2007 Memory Partitioning Divide the memory to small size partition. Two types of partitioning: 1.Fixed Partitioning 2.Dynamic Partitioning
17
CSNB224 – AAG 2007 Fixed Partitioning Most schemes of memory management, assume that the OS occupies some fixed partition of memory the rest of memory is available for use by multiple processes. USER AREA The simplest scheme for managing this available memory is to partition it into regions with fixed boundaries. Figure 7.2 shows example of two alternatives for fixed partitioning: 1. Equal-size partition 2. Unequal-size partition
19
CSNB224 – AAG 2007 Fixed Partitioning Equal-size partition (Figure 7.2a) Divide the memory into same size partition any process whose size is less than or equal to the partition size can be loaded into an available partition if all partitions are full, the operating system can swap a process out of a partition
20
CSNB224 – AAG 2007 Fixed Partitioning Two difficulties: A program may not fit in a partition. –The programmer must design the program with “overlays” »Program and data are organized in such away that various modules can be assigned the same region of memory with a main program responsible for switching in and out as needed Memory use is inefficient. –Any program, no matter how small, occupies an entire partition. This is called internal fragmentation. »E.g. Program length 2MB, it occupies an 8MB partition. So 6MB is internal fragmentation.
21
CSNB224 – AAG 2007 Fixed Partitioning Unequal-size partition (Figure 7.2b) Divide the memory into different partition size. A program as large as 16MB can be accommodate without need to overlays it. Partition smaller than 8MB allow smaller program to be accommodate with less internal fragmentation.
22
CSNB224 – AAG 2007 Fixed Partitioning - Placement Algorithm Where to put / load process in memory. Equal-size partitions If there is an available partition, a process can be loaded into that partition because all partitions are of equal size, it does not matter which partition is used If all partitions are occupied by blocked processes, choose one process to swap out to make room for the new process
23
CSNB224 – AAG 2007 Fixed Partitioning - Placement Algorithm Unequal-size partitions: Two ways: 1.Use of multiple queues Assign each process to the smallest partition within which it will fit A queue for each partition size Tries to minimize internal fragmentation Problem: some queues will be empty if no processes within a size range is present
24
CSNB224 – AAG 2007 Fixed Partitioning - Placement Algorithm
25
CSNB224 – AAG 2007 Fixed Partitioning - Placement Algorithm 2.Use of a single queue When its time to load a process into memory the smallest available partition that will hold the process is selected If all partitions is occupied: Preference to swapping out the smallest partition that will hold the incoming process. Also have to consider other factors such as priority and blocked vs. ready process. Increases the level of multiprogramming at the expense of internal fragmentation
26
CSNB224 – AAG 2007 Fixed Partitioning - Placement Algorithm
27
CSNB224 – AAG 2007 Fixed Partitioning The used of unequal-size partition provides a degree of flexibility to fixed partitioning. Fixed partitioning schemes are relatively simple and require minimal OS software and processing overhead. But it still have disadvantages: 1.The number of partitions specified at system generation time limits the number of active (not suspended) processes in the system. 2.Because partition sizes are preset at system generation time, small jobs will not utilize partition space efficiently.
28
CSNB224 – AAG 2007 Fixed Partitioning The used of fixed partitioning is almost unknown today. Example of OS that use this technique was an early IBM mainframe OS, OS/MFT (Multiprogramming with a Fixed Number of Tasks.
29
CSNB224 – AAG 2007 Dynamic Partitioning Partitions are of variable length and number Each process is allocated exactly as much memory as it requires. Used in IBM’s OS/MVT (Multiprogramming with a Variable number of Tasks) An example using 1 MB (1024KB) of memory is shown in Figure 7.4.
30
CSNB224 – AAG 2007 Dynamic Partitioning
31
CSNB224 – AAG 2007 Dynamic Partitioning Initially memory is empty, except for the OS(refer figure a) The first three process are loaded in, starting where the OS ends and occupying just enough space for each process (refer figure b,c,d). This leaves a “hole” at the end of memory that is too small for a fourth process.
32
CSNB224 – AAG 2007 Dynamic Partitioning
33
CSNB224 – AAG 2007 Dynamic Partitioning At some point none of the processes in memory is ready. OS swaps process 2 (figure e), which leaves sufficient room to load a new process, process 4 (figure f). Because process 4 is smaller than process 2, another small hole is created. Later, a point is reached at which none of the processes in memory is ready, but process 2, in the Ready-Suspend state is available. So OS swaps process 1 out (figure g) and swaps process 2 back in (figure h).
34
CSNB224 – AAG 2007 Dynamic Partitioning Eventually holes are formed in the memory. Memory will become more and more fragmented, and memory utilization declines. This hole is called external fragmentation. Referring to fact that the memory is external to all partitions becomes increasingly fragmented. Technique to overcome external fragmentation is called compaction. Time to time OS shifts the processes so that they are contiguous and so that all of free memory is together in one block.
35
CSNB224 – AAG 2007 Dynamic Partitioning Example in figure 7.4h, compaction will result in a block of free memory of length 256K, and this maybe sufficient to load an additional process. Difficulties of compaction is time consuming procedure and wasteful of processor time.
36
CSNB224 – AAG 2007 Dynamic Partitioning - Placement Algorithm Where to put / load process in memory. OS must decide which free block to allocate to a process. 3 placement algorithms that might be considered: 1.Best Fit 2.First Fit 3.Next Fit
37
CSNB224 – AAG 2007 Dynamic Partitioning - Placement Algorithm 1.Best Fit Chooses the block that is closest in size to the request. Chooses smallest hole 2.First-fit: Begins to scan memory from the beginning and chooses the first available block that is large enough. Chooses first hole from beginning
38
CSNB224 – AAG 2007 Dynamic Partitioning - Placement Algorithm 3.Next-fit: Begins to scan memory from the last placement and chooses the next available block that is large enough. Chooses first hole from last placement
40
CSNB224 – AAG 2007 Dynamic Partitioning - Placement Algorithm Which of these algorithms is best will depend on the exact sequence of process swapping that occurs and the size of those processes. Remarks for each of the algorithms: 1.First Fit The simplest and usually the best and fastest. 2.Next-Fit Tends to produce slightly worse result than first fit. More frequently lead to an allocation from a free block at the end of memory Compaction maybe required more frequently with next fit
41
CSNB224 – AAG 2007 Dynamic Partitioning - Placement Algorithm 3.Best Fit Worse performer, because this algorithm will looks for the smallest block that will satisfy the requirement, it guarantees that the fragment left behind is small as possible. The memory is quickly littered by blocks too small to satisfy memory allocation requests. Compaction must be done more frequently than the other two algorithms.
42
CSNB224 – AAG 2007 Replacement Algorithm When all processes in memory are blocked, the OS must choose which process to replace A process must be swapped out (to a Blocked- Suspend state) and be replaced by a new process or a process from the Ready-Suspend queue We will discuss later such algorithms for memory management schemes using virtual memory
43
CSNB224 – AAG 2007 Buddy System Both fixed and dynamic partitioning schemes have drawbacks. A fixed partitioning scheme limits the number of active processes and may used space inefficiently if there is a poor match between available partition sizes and process sizes. A dynamic partitioning scheme is more complex to maintain and includes the overhead of compaction. An interesting compromise is the buddy system.
44
CSNB224 – AAG 2007 Buddy System Memory blocks are available of size 2 K, L<=K<=U, where: 2 L = smallest size block that is allocated 2 U = largest size block that is allocated, generally 2 U is the size of the entire memory available for allocation. Begin with entire space available is treated as a single block of 2 U If a request of size s such that 2 U-1 < s <= 2 U, entire block is allocated Otherwise block is split into two equal buddies Process continues until smallest block greater than or equal to s is generated
45
CSNB224 – AAG 2007 Buddy System At any time, the buddy system maintains a list of holes (unallocated blocks) of each size 2 i. A hole may be removed from the (i+1) list by splitting it in half to create two buddies of size 2i in the i list. Whenever a pair of buddies on the i list both become unallocated, they are removed from that list and combine back into a single block on the (i+1) list.
46
CSNB224 – AAG 2007 Buddy System Example shows in Figure 7.6 using 1MB initial block. The first request, A, is for 100kb is needed. The initial block is divided into two 512k buddies. The first of these is divided into two 256K buddies, and the first these is divided into two 128K buddies, one of which is allocated to A. The next request,B, requires 256K block. Such block is already available and is allocated. The process continues with splitting and combine back into 256K, which immediately combine back with its buddy.
48
CSNB224 – AAG 2007 Buddy System Figure 7.7 shows a binary tree representation of the buddy allocation immediately after the release B request. The leaf nodes represent the current partitioning of the memory. If two buddies are leaf nodes, then at least one must be allocated, otherwise they would be combine into a larger block.
50
CSNB224 – AAG 2007 Buddy System The buddy system is a reasonable compromise to overcome the disadvantages of both partitioning schemes. In contemporary OS, VM based on paging and segmentation is superior. This buddy system has found application in parallel systems as an efficient means of allocation and release for parallel programs. A modified form of buddy system is used for UNIX kernel memory allocation.
51
CSNB224 – AAG 2007 Relocation When program loaded into memory the actual (physical / absolute ) memory locations are determined A process may occupy different partitions which means different physical memory locations during execution (from swapping) Compaction will also cause a program to occupy a different partition which means different physical memory locations Thus the locations (instruction and data) referenced by a process are not fixed. Absolute address = physical address
52
CSNB224 – AAG 2007 Relocation To solve this problem, a distinction is made among several types of addresses: 1.Logical Address reference to a memory location independent of the current assignment of data to memory translation must be made to the physical address 2.Relative Address address expressed as a location relative to some known point 3.Physical Address / Absolute Address the absolute address or actual location in memory
53
CSNB224 – AAG 2007 Relocation Programs that employ relative addresses in memory used dynamic run-time loading. It means all of the memory references in the loaded process are relative to the origin of the program. Hardware mechanism is needed for translating relative addressed to physical memory addresses at the time of execution of the instruction that contain the reference. Figure 7.8 shows the way in which this address translation is dynamically accomplished.
55
CSNB224 – AAG 2007 Relocation When a process is assigned to the Running state: Base register - loaded with the starting address in memory of the process. “bounds” register loaded with the address of the ending location of the process These values must be set when the program is loaded into memory or when the process image is swapped in. During execution of the process, relative address are encountered.
56
CSNB224 – AAG 2007 Relocation These include the contents of the instruction register, instruction addresses that occur in branch and call instruction, and addresses that occur in load and store instruction. Each relative address goes through two steps of manipulation by the processor: 1.The value in the base register is added to the relative address to produce an absolute address. Address translation
57
CSNB224 – AAG 2007 Relocation 2.The resulting address is compared to the value in the bound register – If the address within bounds, then the instruction execution may proceed, otherwise an interrupt is generated to the OS which must respond to the error. Check either address valid or not The scheme in Figure 7.8 allows programs to be swapped in and out of memory during execution.
58
CSNB224 – AAG 2007 Relocation It also provide a measure of protection: Each process image is isolated by the contents of the base and bounds registers and safe from unwanted accesses by other processes.
59
CSNB224 – AAG 2007 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 or page frames Figure 7.9 illustrates the use of pages and frames. Some of the frames in memory are in use and some are free and list of free frames is maintained by the OS. Paging
62
CSNB224 – AAG 2007 PA stored on disk, consists of four pages. When it comes to load PA, the OS finds four free frames and loads the four pages of PA into the four frames (Fig 7.9b). PB consisting of three pages and PC consisting of four pages are subsequently loaded. (Fig 7.9c,d). The PB is suspended and is swapped out of memory. Paging
63
CSNB224 – AAG 2007 Later all of the processes in memory are blocked, and the OS needs to bring in anew process, PD that contains five pages. From the figure (Fig7. 9e) we can see that there are no sufficient unused frames to hold the PD. Now the concept of logical address is used but with the absent of base register OS maintains a page table for each process. The page table shows the frames location of each page of the process. Paging
64
CSNB224 – AAG 2007 Within the program, each logical address consists of a page number and an offset within the page. Logical to physical address translation is done by processor and the processor must know how to access the page table of current process. Presented with a logical address (page #, offset), the processor uses the page table to produce a physical address (frame #, offset). From figure 7.9d, five pages of PD are loaded in frames 4, 5,6,11 and 12. Paging
65
CSNB224 – AAG 2007 Figure 7.10 shows the various page tables. Page table entry contains the number of the frame in memory, if any, that holds the corresponding page. OS also maintains a single free frame list of all frames in memory that are currently occupied and available pages. Simple paging similar to fixed partitioning but the different are: In paging the partitions are rather small Program may occupy more than one partitions and no need to be contiguous. Paging
66
Page Tables for Example
67
CSNB224 – AAG 2007 An example is shown in Figure 7.11 In this example consider 16bit addresses are used, and the page size is 1K = 1024 bytes. The relative address 1502 in binary form is 0000010111011110. Offset need 10 bits (m, rightmost) and page number 6 bits (n, leftmost). So the program can consist a maximum 2 6 = 64 pages of 1Kb each. As figure 7.11b shows address 1502 (0000010111011110) correspond to an offset of 478(0111011110) and page number 1 (000001). Paging
68
CSNB224 – AAG 2007 Consider an address of n+m bits, where the leftmost n bits are the page number and the rightmost m bits are the offset. From the example n=6 and m=10. The following steps are needed for address translation: 1.Extract the page number as the leftmost n bits of the logical address. 2.Use the page number as an index into the process page table to find the frame number k. Paging
69
CSNB224 – AAG 2007 3.The starting physical address of the frame is k X 2m, and the physical address of the referenced byte is that number plus the offset. This physical address need not be calculated; it is easily constructed by appending the frame number to the offset. From our example the logical address is 0000010111011110, which page number 1 and offset 478. Paging
70
CSNB224 – AAG 2007 Suppose that this page is residing in memory frame 6 = binary 000110. Then the physical address is frame number 6, offset 478 = 0001100111011110 (Fig 7.12a). Paging
71
CSNB224 – AAG 2007 Summarize for simple paging: Memory is divided into small equal-size frames. Each process is divided into frame-size pages Smaller processes require fewer pages, larger processes require more. When a process is brought in, all of its pages are loaded into available frames, and a page table is set up. This approach solves many problems inherent in partitioning. Paging
72
CSNB224 – AAG 2007 Segmentation Alternative way to subdivided user program into segments. All segments of all programs do not have to be of the same length, but there is a maximum length. Addressing consist of two parts - a segment number and an offset Since segments are not equal, segmentation is similar to dynamic partitioning the different is that with segmentation a program may occupy more than one partition and these partitions need not be contiguous.
73
CSNB224 – AAG 2007 Segmentation Segmentation eliminates internal fragmentation but suffer from external fragmentation. However because a process is broken up into a number of smaller pieces, the external fragmentation should be less. Paging is invisible for the programmer but segmentation is visible and is provided as a convenience for organizing programs and data.
74
CSNB224 – AAG 2007 Segmentation Programmer or compiler will assign programs and data to different segments For modular programming, the program or data may be further broken down into multiple segments
75
CSNB224 – AAG 2007 Segmentation The principal inconvenience is that the programmer must be aware of the maximum segment size limitation. For unequal size segments there is no simple relationship between logical addresses and physical addresses. Analogous form paging, a simple segmentation scheme would make use of segment table for each process and a list of free blocks of memory.
76
CSNB224 – AAG 2007 Segmentation Each segment table entry would have to give the starting address in memory of the corresponding segment. The entry should also provide the length of the segment, to assure that invalid addresses are not used. When process enters the Running state, the address of its segment table is loaded into a special register used by the memory management hardware.
77
CSNB224 – AAG 2007 Segmentation Consider and address of n + m bits, where the leftmost n bits are segment number and the rightmost m bits are the offset. In our example (Fig. 7.11c), n=4 and m=12. Thus the maximum segment size is 2 12 = 4096. The following steps are needed for address translation: 1.Extract the segment number as the leftmost n bits of the logical address. 2.Use the segment number as an index into the process segment table to find the starting physical address of the segment.
78
CSNB224 – AAG 2007 Segmentation 3.Compare the offset, expressed in the rightmost m bits, to the length of the segment. If the offset greater than the length, the address is invalid. 4.The desired physical address is the sum of the starting physical address of the segment plus the offset. Example the logical address 0001001011110000, which is segment number 1 (0001) and offset 752 (001011110000).
79
CSNB224 – AAG 2007 Segmentation Suppose that this segment is residing in memory starting at physical address 0010000000100000. Then the physical address is (Fig 7.12b) 010000000100000 + 001011110000 = 0010001100010000.
80
CSNB224 – AAG 2007 Segmentation Summarization for simple segmentation: A process is divided into a number of segments which need not be equal size. When a process brought in, all of its segments are loaded into available regions of memory, and a segment table is setup.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.