Download presentation
Presentation is loading. Please wait.
Published byFrank Thornton Modified over 9 years ago
1
1 Memory Management Chapter 7
2
2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable supply of ready processes to consume available processor time
3
3 Memory Management Requirements Relocation Protection Sharing –Each process access same copy of a program Logical organization Physical organization
4
4 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 main memory at a different location (relocated) –Memory references must be translated in the code to actual physical memory address Processor hardware and OS software must be able to translate the memory references found in the code into actual physical memory addresses –Next diagram is a process image
5
5 Memory Management Requirements OS needs to know location of process control info, execution stack and entry point of program to begin execution OS must deal with memory references within program –Branch instructions – data reference instructions Memory addresses found in code must be translated into physical addresses
6
6
7
7 Memory Management Requirements Protection –Processes should not be able to reference memory locations in another process without permission –Impossible to check absolute addresses at compile time Location of program in main memory unpredictable Must be checked at run time –Memory 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 make
8
8 Memory Management Requirements Protection –Normally users cannot access any portion of OS –Normally one program cannot branch to instructions in another process –Normally one program cannot access data in another process without special arrangements –Processor must abort instructions that do the above OS cannot anticipate such conditions
9
9 Memory Management Requirements Sharing –Allow several processes to access the same portion of memory –Better to allow each process access to the same copy of the program that they all need to execute rather than each process having their own separate copy Re-entrant code –Memory management system must allow controlled access to shared areas of memory without compromising security
10
10 Memory Management Requirements Logical Organization –Most 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 among processes
11
11 Memory Management Requirements Logical Organization –If OS and hardware can effectively deal with user programs and data in form of modules then there are advantages Modules can be written and compiled independently with all references among modules resolved by system at run-time Different degrees of protection can be granted –(read-only, execute-only)
12
12 Memory Management Requirements Logical Organization –Mechanism by which modules can be shared among processes is possible –Memory management tool that most readily satisfies these above requirements is segmentation.
13
13 Memory Management Requirements Physical Organization –Computer memory organized into primary and secondary memory –The flow of information between these two-levels is of concern –Responsibility cannot be assigned to programmer –Impractical for two reasons Memory available for a program plus its data may be insufficient Overlaying allows various modules to be assigned the same region of memory Overlaying wastes programmers time –Programmer does not know how much space will be available at time of coding
14
14 Memory Management Requirements Physical Organization –The task of moving information between two levels of memory should be system responsibility Two levels are main memory and disk
15
15 Memory Partitioning Principle operation of memory management –Bring processes into main memory for execution –Scheme in multi-programming called virtual memory –Two basic virtual memory techniques Segmentation Paging –Break program into equal-sized partitions
16
16 Memory Partitioning Simpler techniques that do not involve virtual memory In most schemes OS occupies some fixed portion of main memory and the rest of main memory is available for multiple processes Fixed Partitioning –Partition main memory into regions with fixed boundaries
17
17 Fixed Partitioning Equal-size partitions –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 –Difficulties A program may not fit in a partition. Then the programmer must design the program with overlays
18
18 Fixed Partitioning Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. Wasted space internal to the partition. This is called internal fragmentation. –8 Mgbyte partition, 2Mgbyte program. 6 Mgbyte wasted
19
19
20
20 Fixed Partitioning Un-equal sized partitions –Allow lessening of two problems above, that is Program may be too big to fit Internal fragmentation
21
21 Placement Algorithms with Partitions Equal-size partitions –Placement algorithm trivial –Because all partitions are of equal size, it does not matter which partition is used –If all partitions used, it is a scheduling decision as to which process is swapped out Unequal-size partitions –Can assign each process to the smallest partition within which it will fit –Can have a Job Queue for each partition –Processes are assigned in such a way as to minimize wasted memory within a partition Minimizes internal fragmentation See figure 7.3(a) below
22
22 Placement Algorithm with Partitions Unequal-size partitions –However one queue for each partition not optimal No processes of certain sizes exist to fit into certain partition sizes –Best to employ single process queue. When time to load a process, placement algorithm selects smallest available partition that will hold the process If all partitions are occupied, swapping decision may be to swap out process in smallest partition that will hold the incoming process See figure 7.3(b) below
23
23 Placement Algorithm with Partitions Unequal-size partitions –Unequal partition scheme provides degree of flexibility that a fixed partition scheme does not –Fixed scheme is relatively simple and requires minimal OS software and overhead –Disadvantages of unequal sized partitioning. Number of partitions specified at system generation time –Limits number of processes in system Preset partition sizes make it such that small jobs do not use partition space efficiently
24
24 Fixed Partitions Fixed partitions not used in today’s OS IBM mainframe OS OS/MFT (Multi- programming with a fixed number of tasks) used fixed partitioning
25
25
26
26 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
27
27 Dynamic Partitioning Example Scenario –64 Mbytes of memory –OS takes first 8Mbytes –Rest of memory empty
28
28 (b),© and (d) P1, P2, P3 loaded with just enough space ( d), (e),(f) P4 can not fit, So OS swaps out P2 and places P4 in hole leftover Small hole leftover ( g),(h) P2, in ready- suspend state is available to run. Insufficient room in memory to place P2. OS swaps out P1 and swaps in P2 in its place
29
29 Dynamic Partitioning Placement Algorithm Operating system must decide which free block to allocate to a process Best-fit algorithm –Chooses the block that is closest in size to the request –Worst performer overall –Since smallest block is found for process, the smallest amount of fragmentation is left –Many small holes –Memory compaction must be done more often
30
30 Dynamic Partitioning Placement Algorithm First-fit algorithm –Scans memory from the beginning and chooses the first available block that is large enough –Fastest, simplest, and usually the best –May have many process loaded in the front end of memory that must be searched over when trying to find a free block
31
31 Dynamic Partitioning Placement Algorithm Next-fit –Scans memory from the location of the last placement –More often allocate a block of memory at the end of memory is where the largest block is found –The largest block of memory is broken up into smaller blocks –Compaction is required to obtain a large block at the end of memory
32
32 Performance of Dynamic Partitioning First-fit fastest, simple and best –Litters front-end of memory with small free partitions that must be searched Next-fit gives slightly worst results than First-fit – More frequent allocation of free block from end of memory –Rear end of memory more quickly broken up into small blocks –More compaction need than First-Fit Best-Fit has worst performance –Memory littered with blocks too small to satisfy memory requests –Compaction is required to obtain a large block at the end of memory
33
33 Performance of Dynamic Partitioning Example – Fig 7.5(a) shows makeup of memory at some point –The last block allocated below was a 22M block allocated to a process of size 14M. – Fig 7.5(b) shows the state of memory after a 16M request for each of the allocation algorithms
34
34
35
35 Buddy System(Here) Both fixed and dynamic have drawbacks Fixed limits number of processes and may use space inefficiently Dynamic more complex to maintain and needs compaction Buddy system is a compromise
36
36 Buddy System 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
37
37
38
38
39
39 Relocation Placement of processes in memory For fixed partition, a process will always be assigned to the same partition – Use a simple relocating loader –Replace all memory locations in code by absolute main memory addresses determined by the base address of the loaded processes In case of equal-sized partitions and un- equal sized partitions a process may occupy different partitions during its lifetime partition which means different absolute memory locations
40
40 Relocation When program loaded into memory the actual (absolute) memory locations are determined A process may occupy different partitions which means different absolute memory locations during execution (from swapping) Compaction will also cause a program to occupy a different partition which means different absolute memory locations
41
41 Addresses Logical –Reference to a memory location independent of the current assignment of data to memory –Translation must be made to the physical address Relative –Address expressed as a location relative to some known point Physical –The absolute address or actual location in main memory Hardware support for relocation
42
42
43
43 Registers Used during Execution Base register –Starting address for the process Bounds register –Ending location of the process These values are set when the process is loaded or when the process is swapped in
44
44 Registers Used during Execution The value of the base register is added to a relative address to produce an absolute address The resulting address is compared with the value in the bounds register If the address is not within bounds, an interrupt is generated to the operating system If the address is within bounds, it is legal
45
45 Paging Partition memory into small equal fixed- 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 –Memory address consist of a page number and offset within the page
46
46 Assignment of Process Pages to Free Frames
47
47 Assignment of Process Pages to Free Frames
48
48 Page Tables for Example
49
49 Paging Operating system maintains a page table for each process –Contains the frame location for each page in the process –Memory address consist of a page number and offset within the page For convenience page size must be a power of 2 With size power of 2, easy to see that relative address with respect to the start of the program and the logical address expressed as page#, off-set pair are the same
50
50 Paging Example // assume 16 bit logical addresses // page size is 1024 bytes or 10 bits relative address 1502 decimal is 0000 0101 1101 1110 With page size of 10 bits offset is low-order 10 bits 0000 01 011101 1110 page # is 6 high-order bits 0000 01 011101 1110 So relative address 1502 is equivalent to logical address page no =1 and offset = 478 Program can consist of at most 2 6 = 64 pages
51
51 Paging Dynamic Address Translation Logical addresses and relative addresses same Both are transparent to the programmer Can easily implement function in hardware to do address translation at run time
52
52 Address Translation Example // Consider address of m + n bits where leftmost n bits are page number and the rightmost m bits are the offset // In the example 0000 0101 1101 1110 n=6 and m=10 000001 011101 1110 Perform the following steps: 1.Extract the page number as the left-most n bits of the logical address 2.Use the page number as an index into the process page table to find the corresponding frame k 3.The starting physical address is k*2 m. 4.The physical address is k*2 m + offset If the frame number in the page table for page#=1 is 6, the physical address associated with logical address 0000 000001 011101 1110 is 000110 011101 1110.
53
53 Segmentation All segments of all programs do not have to be of the same length There is a maximum segment length Addressing consist of two parts - a segment number and an offset Since segments sizes are not equal, segmentation is similar to dynamic partitioning
54
54 Segmentation There is a segment table for each process Each entry in the segment table gives the starting address in physical memory of the corresponding segment along with the length of the segment to insure that invalid addresses are not used When process enters running state the address of it segment table is loaded into a special hardware register used by the memory management system
55
55 Segment Translation Example // Consider address of m + n bits where leftmost n bits is segment number and the rightmost m bits are the offset // Consider address 0001 0010 1111 0000 n=4 and m=12 Perform the following steps: 1.Extract the segment number as the left-most 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 3.Compare the offset, the right most m bits to to the length of the segment. If offset is greater, the address is invalid. 4.The physical address is is the sum of the starting physical address of the segment plus the off set Logical address 0001 0010 1111 0000 is segment # 1 and offset 752 If the starting physical address of segment #1 is 0010 0000 0010 0000, the physical address is address 0010 0000 0010 0000 + 0000 0010 1111 0000 = 0010 0011 0001 0000
56
56
57
57
58
58
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.