Memory management

485 downloads 11301 Views 793KB Size Report
Physical address is the address actually seen ... Best fit. - Allocate the smallest hole that is big enough. - Need to search the entire list, unless the list is ordered ...
Updated Memory management Assist. Prof. Dr. Qasim Mohammed Hussein

Memory management

Dr. Qasim Mohammed

Memory management • To improve both the utilization of the CPU and the speed of its response to users, • The computer must keep several processes in main memory • Many memory-management schemes exist, reflecting various approaches, and the effectiveness of each algorithm depends on the situation Memory management

Dr. Qasim Mohammed

Logical Versus Physical Address Space • Logical address is an address generated by the CPU. • Physical address is the address actually seen by the memory hardware.

Memory management

Dr. Qasim Mohammed

Storage management strategies 1) Fetch strategies 2) Placement strategies 3) Replacement strategies

Memory management

Dr. Qasim Mohammed

1. Fetch strategies • Fetch strategies are concerned with when to obtain the next piece of program or data for transfer to main storage from secondary storage. There are two approaches. • Demand fetch strategies : The next piece of program or data is brought into the main memory when it is referenced by a running program. • Anticipatory fetch strategies: They make predict and guesses and anticipating the future where program control will go next. Memory management

Dr. Qasim Mohammed

2. Placement strategies They are concerned with determining where in main storage to place a new program . Strategies are most commonly used to select a free hole from the set of available holes. A) First fit. - The incoming job places in the first hole that is big enough. - It does not need search all the hole set. - It is faster strategies to placement. Memory management

Dr. Qasim Mohammed

2. Placement strategies • Best fit. - Allocate the smallest hole that is big enough. - Need to search the entire list, unless the list is ordered by size. - This strategy produces the smallest leftover hole. • Worst fit. - Allocate the largest hole. - Need to search the entire list, unless it is sorted by size. - This strategy produces the largest leftover hole, Memory management

Dr. Qasim Mohammed

2. Placement strategies

Example

Memory management

Dr. Qasim Mohammed

3. Replacement strategies • They are concerned with determining which piece of program of data to displace to make room for a new program or data.

Memory management

Dr. Qasim Mohammed

Contiguous and non – contiguous storage allocation • The contiguous allocation means ach program occupy a single contiguous block of storage locations. • In non – contiguous , a program is divided into several blocks or segments that may be placed I n memory in pieces not necessary adjacent to one another.

Memory management

Dr. Qasim Mohammed

Storage management 1. Bare machine : The user has control over the entire memory space. It is simple and not required cost , and not need special HW or SW. 2. Overlays allocation storage. It allows allocating run a program that its size larger than the amount of memory by keeps in memory only those instructions and data that are need at given time Memory management

Dr. Qasim Mohammed

Storage management 3. Swapping allocation method. A process can be swapped temporarily out of memory to a backing store and then brought back into memory for continued execution 4. Multiple partition allocation. The memory is divided into a number of regions or partitions. Each partition have one process. Memory management

Dr. Qasim Mohammed

Storage management • So, several user processes reside in memory at the same time. • There are two management schemes: a) multiple contiguous fixed partition (MFT) b) multiple contiguous variable partition (MVT).

Memory management

Dr. Qasim Mohammed

A. Multiple contiguous fixed partition. • The memory is divided into a number of fixed – size partitions. Each partition contains one process only. Need protection. By using two registers. (1) Bound registers: they contain the value of smallest and largest physical address. (2) Base and limit registers. Base register contains the smallest physical address. Limit register contains the size of process. Memory management

Dr. Qasim Mohammed

• The problem of MFT is determining the best partitions sizes to minimize internal and external fragmentation Memory management

Dr. Qasim Mohammed

Multiple contiguous variable partition (MVT). To solve the fragmentation problem in MFT , allow the partition sizes to vary dynamically. In MVT, the OS keeps a table indicating which parts of memory are available and which are used.

Memory management

Dr. Qasim Mohammed

Fragmentation • As processes are loaded and removed from memory, the free memory space is broken into little pieces. • After sometimes that processes can not be allocated to memory blocks considering their small size and memory blocks remains unused. • This problem is known as Fragmentation Memory management

Dr. Qasim Mohammed

Fragmentation S.N.

1

2

Fragmentation

Description

External fragmentation

Total memory space is enough to satisfy a request or to reside a process in it, but it is not contiguous so it cannot be used.

Internal fragmentation

Memory block assigned to process is bigger. Some portion of memory is left unused as it cannot be used by another process.

Memory management

Dr. Qasim Mohammed

Compaction • Solution of fragmentation is used compaction. • Compaction: move all processes to one end of memory and holes to the other end. • Expensive and can only be done when relocation is done at execution time, not at load time • All the methods suffer from external fragmentation. Memory management

Dr. Qasim Mohammed

Paging • A Pging technique is a one solution of external fragmentation. • Paging is a memory-management scheme that permits the physical address space of a process to be noncontiguous. • The physical memory is breaking into fixedsized blocks called frames and logical memory is breaking into blocks of the same size called pages Memory management

Dr. Qasim Mohammed

Paging

• Address generated by CPU is divided into Page number (p) and Page offset (d). • Page number (p) -- page number is used as an index into a page table which contains base address of each page in physical memory. • Page offset (d) -- page offset is combined with base address to define the physical memory address. • When a process is to be executed, its pages are loaded into any available memory frames from the backing store. The backing store is divided into fixed-sized blocks that are of the same size as the memory frames. Memory management

Dr. Qasim Mohammed

• A Page Table is used where the page number is the index and the table contains the base address of each page in physical memory. • This base address (p) is combined with the page offset (d) to define the physical memory address that is sent to the memory unit.

Memory management

Dr. Qasim Mohammed

Paging

Memory management

Dr. Qasim Mohammed

Example : Consider a user program of logical address of size 4 pages and page size is 4 bytes, use the physical address of 260 frames. If the user program consists of instruction a, b, c, …, p . each instruction takes 1 byte. Assume at that time the free frames list are 5, 6, 1, 2, 14. Show how the program can be allocated in the physical memory and draw the page table and the logical and physical map? The physical address = (frame number * page size)+ offset Memory management

Dr. Qasim Mohammed

Memory management

Dr. Qasim Mohammed

Paging Example 2: Consider a user program of logical address of size 6 pages and page size is 4 bytes. The physical address contains 300 frames. The user program consists of 16 instructions a, b, c, . . . u, v . Each instruction takes 1 byte. Assume at that time the free frames are 7, 26, 52, 20, 55, 6, 18, 21, 70, and 90. Find the following?

A) Draw the logical and physical maps and page tables? B) Allocate each page in the corresponding frame? C) Find the physical addresses for the instructions m, d, v, r? D) Calculate the fragmentation if exist?

The physical address = page size * frame number + offset • The physical address of m = 4*20 +0 = 80 • The physical address of d = 4*7+3 = 31 • The physical address of v = 4* 6 +1 = 25 • The physical address of r = 4*55+ 1= 221 • The external fragmentation = 0 • The internal fragmentation = 2

Shared Pages Shared code: One copy of read-only code shared among processes (i.e., text editors, compilers, window systems). Shared code must appear in same location in the logical address space of all processes.

Memory management

Dr. Qasim Mohammed

6. Segmentation • Segmentation is a memory-management scheme that supports this user view of memory, which uses a memory as a collection of variable size segment.

Memory management

Dr. Qasim Mohammed

6. Segmentation • A logical address space is a collection of segments. Each segment has a name and a variable length. The addresses specify both the segment name and the offset within the segment. • To implement segmentation, we use a segment table. Each entry in the segment table has a segment base and a segment limit. Memory management

Dr. Qasim Mohammed

6. Segmentation • The segment base contains the starting physical address where the segment resides in memory, whereas the segment limit specifies the length of the segment.

Memory management

Dr. Qasim Mohammed

6. Segmentation • A logical address consists of two parts: a segment number, s, and an offset into that segment, d. • The segment number is used as an index to the segment table. • The offset d of the logical address must be between 0 and the segment limit.

Memory management

Dr. Qasim Mohammed

Segment hardware

Physical address = Base of segment + offset Memory management

Dr. Qasim Mohammed

Example: Consider we have five segments numbered from 0 through 4. The segments are stored in physical memory as shown in figure. The segment table has a separate entry for each segment, giving the beginning address of the segment in physical memory (or base) and the length of that segment (or limit). For example, segment 2 is 400 bytes long and begins at location 4300 Memory management

Dr. Qasim Mohammed

6. Segmentation • Fragmentation in segmentation • Segmentation may cause external fragmentation when all blocks of free memory are too small to accommodate a segment. There fore , segmentation has two types of fragment: external and internal.

Memory management

Dr. Qasim Mohammed

Example Consider a program consists of five segments: S0 = 600, S1 = 14 KB , S2= 100 KB, S3 =580 KB, and S4 = 96 KB . Assume at that time, the available free space partition of memory are 1200 – 1805, 500 – 600 , 220-250, 25003200. Find the following : • Draw logical to physical maps and segment table? • Allocate space for each segment in memory? • Calculate the external fragmentation and the internal fragmentation? • What are the addresses in physical memory for the following logical addresses: • 0.580, (b) 1.17 (c) 2.96 (d) 4.112 ( e) 3.420

Memory management

Dr. Qasim Mohammed

Memory management

Find the fragmentation Because all segments are allocated, therefore no external fragmentation. So external Frag. =0 Internal fragmentation = (250-234)+(1805-1800)+ 3200-3176) = 16 + 5 + 24 = 45

4. The physical addresses are a) S= 0, d= 580 , the base for S0 = 1200, limit = 600 The physical address = 1200+580 = 1780. b) S = 1 , d = 17 the base for S1 = 220, limit = 14 . Because d > limit , the address is wrong. C) S= 2, d= 96 , the base for S2 = 500, limit = 100 The physical address = 500+ 96 = 596 d) S= 4, d= 112 , the base for S4 = 3080, limit = 96. Because d > limit , the address is wrong. e) S= 3 , d= 420, the base for S3 = 2500, limit = 580. The physical address = 2500 + 430 = 2920.

Memory management

Dr. Qasim Mohammed

Exampl2:Let program has five segments with following size: S0= 60 KB, S1= 140 KB, S2 = 100KB , S3= 340 KB, S4 = 218KB. And suppose the free space list at that time is: (1700 – 1760, 1800 – 2020, 2500 – 2640, 2100 – 2200, and 2800 - 3144). Answer the following? • Allocate a memory space for each segment and draw the logical map, physical map and segment and segment table? • Calculate the external and internal fragmentation? • Find the physical addresses for the logical addresses? • 0.50 (2) 1.91 (3) 2.102 (4) 3. 17 (5) 4`.60

3. External fragmentation =0. The internal fragmentation =(160-150)+(250-234) = 24 The physical address of 0.580 = 1200+580 = 1780. Because d > limit of S1 , the address is wrong. The physical address of 2.96= 50 + 96 = 146 The physical address is of 4.82 is = 3080 + 82 = 3162 The physical address 3.24 = 2500 + 430 = 29206

Thanks for your attention Memory management

Dr. Qasim Mohammed