chapter 5 : memory management 1by : jigar m. pandya

40
Chapter 5 : Memory Management 1 By : Jigar M. Pandya

Upload: ella-bragg

Post on 29-Mar-2015

230 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 5 : Memory Management 1By : Jigar M. Pandya

1

Chapter 5 : Memory Management

By : Jigar M. Pandya

Page 2: Chapter 5 : Memory Management 1By : Jigar M. Pandya

2By : Jigar M. Pandya

The need for memory management

Memory is cheap today, and getting cheaper◦But applications are demanding more

and more memory, there is never enough!

◦Basically we will do memory management for primary memory

Page 3: Chapter 5 : Memory Management 1By : Jigar M. Pandya

3By : Jigar M. Pandya

Memory Management

Memory needs to be allocated to ensure a reasonable supply of ready processes to consume available processor time

Page 4: Chapter 5 : Memory Management 1By : Jigar M. Pandya

4By : Jigar M. Pandya

Memory Management RequirementsRelocationProtectionSharingLogical organisationPhysical organisation

Page 5: Chapter 5 : Memory Management 1By : Jigar M. Pandya

5By : Jigar M. Pandya

Requirements: Relocation

The programmer does not know where the program will be placed in memory when it is executed, ◦it may be swapped to disk and return to

main memory at a different location (relocated)

Memory references must be translated to the actual physical memory address

Page 6: Chapter 5 : Memory Management 1By : Jigar M. Pandya

6By : Jigar M. Pandya

Requirements: Protection

Processes should not be able to reference memory locations in another process without permission

Impossible to check absolute addresses at compile time

Must be checked at run time

Page 7: Chapter 5 : Memory Management 1By : Jigar M. Pandya

7By : Jigar M. Pandya

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 rather than have their own separate copy

Page 8: Chapter 5 : Memory Management 1By : Jigar M. Pandya

8By : Jigar M. Pandya

Requirements: Logical OrganizationMemory is organized linearly

(usually)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 processesSegmentation helps here

Page 9: Chapter 5 : Memory Management 1By : Jigar M. Pandya

9By : Jigar M. Pandya

Requirements: Physical OrganizationCannot leave the programmer with

the responsibility to manage memoryMemory available for a program plus

its data may be insufficient◦Overlaying allows various modules to be

assigned the same region of memory but is time consuming to program

Programmer does not know how much space will be available

Page 10: Chapter 5 : Memory Management 1By : Jigar M. Pandya

10By : Jigar M. Pandya

The need for Relocation

Because of need for process swapping and memory compaction, a process may occupy different main memory locations during its lifetime.

Consequently, physical memory references (addresses) by a process cannot always be fixed.

This problem is solved by distinguishing between logical address and physical address.

Page 11: Chapter 5 : Memory Management 1By : Jigar M. Pandya

11By : Jigar M. Pandya

Logical vs. Physical Address SpaceThe concept of a logical address space that is bound

to a separate physical address space is central to proper memory management.

◦ Logical address – generated by the CPU; also referred to as virtual address.

◦ Physical address – address seen by the memory unit.

Page 12: Chapter 5 : Memory Management 1By : Jigar M. Pandya

12By : Jigar M. Pandya

Swapping A process can be swapped temporarily out of memory to a

backing store, and then brought back into memory for continued execution.

Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images.

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.

Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped.

Modified versions of swapping are found on many systems, i.e., UNIX, Linux, and Windows.

Page 13: Chapter 5 : Memory Management 1By : Jigar M. Pandya

13By : Jigar M. Pandya

Schematic View of Swapping

Page 14: Chapter 5 : Memory Management 1By : Jigar M. Pandya

14By : Jigar M. Pandya

Contiguous Allocation

Main memory usually into two partitions:◦ Resident operating system, usually held in low

memory with interrupt vector.◦ User processes then held in high memory.

Single-partition allocation◦ Relocation-register scheme used to protect user

processes from each other, and from changing operating-system code and data.

◦ Relocation register contains value of smallest physical address; limit register contains range of logical addresses – each logical address must be less than the limit register.

Page 15: Chapter 5 : Memory Management 1By : Jigar M. Pandya

15By : Jigar M. Pandya

Dynamic Storage-Allocation Problem

First-fit: Allocate the first hole that is big enough. Best-fit: Allocate the smallest hole that is big

enough; must search entire list, unless ordered by size. Produces the smallest leftover hole.

Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.

How to satisfy a request of size n from a list of free holes.

First-fit and best-fit better than worst-fit in terms of speed and storage utilization.

Page 16: Chapter 5 : Memory Management 1By : Jigar M. Pandya

16By : Jigar M. Pandya

Fragmentation

External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous.

Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used.

Reduce external fragmentation by compaction◦Shuffle memory contents to place all free memory

together in one large block.◦Compaction is possible only if relocation is

dynamic, and is done at execution time.

Page 17: Chapter 5 : Memory Management 1By : Jigar M. Pandya

17By : Jigar M. Pandya

Paging

It is memory management scheme .

Every Process is Divided in to number of pages.

Divide physical memory into fixed-sized blocks

called frames

Divide logical memory into blocks of same size

called pages.

Page 18: Chapter 5 : Memory Management 1By : Jigar M. Pandya

18By : Jigar M. Pandya

Paging In Continuous Memory management

◦ We have to assign a whole block of the process size in

the memory.

◦ Some time it may be not available in a single block.

In Paging process is divided in to pages so there may be

free pages available in the memory of same size.

A process can put its page at any frame in

The memory .

When CPU runs a process it will generate

logical addresses.

But there should be some mapping between Logical

Address and the frame in the Mem.

Page 19: Chapter 5 : Memory Management 1By : Jigar M. Pandya

19By : Jigar M. Pandya

Paging Every Logical address will be divided In to two

parts.

1) p = page number

2) d = offset

In Example if Page size is 10 then

For process of size 5

◦ P= 0

◦ d = 5

For process of size 12

◦ P= 1

◦ d = 2

Offset 2 in page 1 will be same as offset 2 in

frame 3 because p1 is loaded in to frame no 3

Page 20: Chapter 5 : Memory Management 1By : Jigar M. Pandya

20By : Jigar M. Pandya

Paging

Page 21: Chapter 5 : Memory Management 1By : Jigar M. Pandya

21By : Jigar M. Pandya

Paging CPU will generate a Logical address in p & d form.

There Will Be A page map table to map the logical

address to the frame number

Logical address page number will be index of PMT.

Using that we can get frame number.

combine that frame number with offset will give

you physical address in memory

Page 22: Chapter 5 : Memory Management 1By : Jigar M. Pandya

22By : Jigar M. Pandya

Free Frames

Before allocation After allocation

Page 23: Chapter 5 : Memory Management 1By : Jigar M. Pandya

23By : Jigar M. Pandya

Implementation of Page Table

Page table is kept in main memory.

Page-table base register (PTBR) points to the page table.

Page-table length register (PRLR) indicates size of the page table.

In this scheme every data/instruction access requires two memory accesses. One for the page table and one for the data/instruction.

The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs)

Page 24: Chapter 5 : Memory Management 1By : Jigar M. Pandya

24By : Jigar M. Pandya

Associative Memory

Associative memory – parallel search

Page # Frame #

Page 25: Chapter 5 : Memory Management 1By : Jigar M. Pandya

25By : Jigar M. Pandya

Paging Hardware With TLB(translation look-aside buffers)

Page 26: Chapter 5 : Memory Management 1By : Jigar M. Pandya

26By : Jigar M. Pandya

Advantage of Paging

If I want to user 100 KB then I don’t need a single block of

100KB

I can use 10 free frame of 10KB that may not in

continuous memory location.

So some how we can avoid the problem of fragmentation.

Page 27: Chapter 5 : Memory Management 1By : Jigar M. Pandya

27By : Jigar M. Pandya

Paging When we do paging we are dividing the code in to

number of pages.

So it may happen if there is a for loop

Then some portion may be in first page, remaining may

be in second page.

That will also run the code .

But if we want to separate code in modular format then

we can use new concept that is called …

SEGMENTATION

Page 28: Chapter 5 : Memory Management 1By : Jigar M. Pandya

28By : Jigar M. Pandya

SEGMENTATION What is Module ??

If a large program is there then we are dividing that in to

small small function that functions are called the

modules. A program is a collection of segments. A segment is a

logical unit such as:main program,procedure, function,method,object,local variables, global variables,common block,

Page 29: Chapter 5 : Memory Management 1By : Jigar M. Pandya

29By : Jigar M. Pandya

User’s View of a Program

Page 30: Chapter 5 : Memory Management 1By : Jigar M. Pandya

30By : Jigar M. Pandya

User’s View of a Program In segmentation there won’t be same size

frames in memory

it will be of different size segment and will be

created when ever it is needed.

So no fixed partition.

Page 31: Chapter 5 : Memory Management 1By : Jigar M. Pandya

31By : Jigar M. Pandya

Logical View of Segmentation

1

4

2

3

user space physical memory space

1

3

2

4

Page 32: Chapter 5 : Memory Management 1By : Jigar M. Pandya

32By : Jigar M. Pandya

Segmentation Architecture Logical address consists of a two tuple:

<segment-number, offset>

Segment table – maps two-dimensional physical addresses; each table entry has:

◦ base – contains the starting physical address where the segments reside in memory.

◦ limit – specifies the length of the segment.

Segment-table base register (STBR) points to the segment table’s location in memory.

Segment-table length register (STLR) indicates number of segments used by a program;

Page 33: Chapter 5 : Memory Management 1By : Jigar M. Pandya

33By : Jigar M. Pandya

Segmentation Architecture

• Offset can not go beyond the size of the limit register

Page 34: Chapter 5 : Memory Management 1By : Jigar M. Pandya

34By : Jigar M. Pandya

Segmentation Memory Management

Page 35: Chapter 5 : Memory Management 1By : Jigar M. Pandya

35By : Jigar M. Pandya

Example of Segmentation

Page 36: Chapter 5 : Memory Management 1By : Jigar M. Pandya

By : Jigar M. Pandya 36

Segmentation Vs paging

Page 37: Chapter 5 : Memory Management 1By : Jigar M. Pandya

37By : Jigar M. Pandya

First The request will be divided in to segment

Then each segment will be divided in to pages.

So we need to manage segment table and also the page table for each segment.

Segmentation with Paging

Page 38: Chapter 5 : Memory Management 1By : Jigar M. Pandya

38By : Jigar M. Pandya

Segmentation with Paging

Page 39: Chapter 5 : Memory Management 1By : Jigar M. Pandya

By : Jigar M. Pandya 39

Virtual MemoryVirtual memory – separation of user

logical memory from physical memory.◦ Only part of the program needs to be in memory

for execution.◦ Logical address space can therefore be much

larger than physical address space.◦ Allows address spaces to be shared by several

processes.◦ Allows for more efficient process creation.

Virtual memory can be implemented via:◦ Demand paging ◦ Demand segmentation

Page 40: Chapter 5 : Memory Management 1By : Jigar M. Pandya

By : Jigar M. Pandya 40

Thrashing

Thrashing a process is busy swapping pages in and out.

If a process does not have “enough” pages, the page-fault rate is very high. This leads to:◦ low CPU utilization.

◦ operating system thinks that it needs to increase the degree of multiprogramming.

◦ another process added to the system.