Os Amit-memory Management

70
Operating System Concepts Amit Kumar Lecturer , CSE Department. G.B.Pant Govt. Engineering [email protected] 011-26382021 Operating System Concepts Lecturer , CSE Department. G.B.Pant Govt. Engineering College,Delhi [email protected]

Transcript of Os Amit-memory Management

Page 1: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 1/70

Operating System Concepts

Amit Kumar 

Lecturer , CSE Department.G.B.Pant Govt. Engineering

[email protected]

011-26382021

Operating System Concepts

Lecturer , CSE Department.G.B.Pant Govt. Engineering College,Delhi

[email protected]

Page 2: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 2/70

Module 8: Memory Management

Background

Logical versus Physical Address SpaceSwapping

Contiguous Allocation

Paging

SegmentationSegmentation with Paging

Module 8: Memory Management

Page 3: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 3/70

Background

Program must be brought into memory and placed within a process for it to be executed

Input queue collection of processes on the disk execution.

User programs go through several steps before being executed.

Program must be brought into memory and placed

within a process for it to be executed.

disk that are waiting to be brought into memory for 

User programs go through several steps before being executed.

Page 4: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 4/70

Binding of Instructions and Data to Memory

Compile time: If memory location known a priori, absolute code can be

generated; must recompile code if starting location changes.

Load time: Must generate relocatable code if memory location is not known at

compile time.

Execution time: Binding delayed until run time if the process can be moved

during its execution from one memory segment to another. Need hardware

support for address maps (e.g., base and

Address binding of instructions and data to memory addresses can

happen at three different stages.

Binding of Instructions and Data to Memory

: If memory location known a priori, absolute code can be

generated; must recompile code if starting location changes.

code if memory location is not known at

: Binding delayed until run time if the process can be moved

during its execution from one memory segment to another. Need hardware

and limit registers).

Page 5: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 5/70

Dynamic Loading

Routine is not loaded until it is called 

Better memory-space utilization; unused routine is never loaded.Useful when large amounts of code are needed to handle infrequently occurring cases.

No special support from the operating system is required implemented through program design.

space utilization; unused routine is

never loaded.Useful when large amounts of code are needed to handle infrequently occurring cases.

No special support from the operating system is required implemented through program design.

Page 6: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 6/70

Dynamic Linking

Linking postponed until execution time.

Small piece of code, stub, used to locate the appropriate memoryStub replaces itself with the address of the routine, and executes the routine.

Operating system needed to check if routine is in processes memory address.

, used to locate the appropriate

memory-resident library routine.Stub replaces itself with the address of the routine, and executes the routine.

Operating system needed to check if routine is in processes memory address.

Page 7: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 7/70

Overlays

Keep in memory only those instructions and data that are needed at any given time.

Needed when process is larger than amount of memory allocated to itImplemented by user, no special support needed from operating system, programming design of 

overlay structure is complex

Keep in memory only those instructions and data that are needed at any given time.

Needed when process is larger than amount of memory allocated to it.Implemented by user, no special support needed from operating system, programming design of 

Page 8: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 8/70

Logical vs. Physical Address Space

The concept of a logical address space that is bound to a separate

central to proper memory management.  Logical address generated by the CPU; also referred to as

  Physical address address seen by the memory unit

Logical and physical addresses are the same in compile

schemes; logical (virtual) and physical addresses differ in execution

Logical vs. Physical Address Space

that is bound to a separate physical address space is

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

address seen by the memory unit.

Logical and physical addresses are the same in compile-time and load-time address-binding

schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme

Page 9: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 9/70

Memory-Management Unit (

Hardware device that maps virtual to physical address.

In MMU scheme, the value in the relocation register is added to every address generated by a useprocess at the time it is sent to memory.

The user program deals with logical addresses; it never sees the

Management Unit (MMU)

Hardware device that maps virtual to physical address.

In MMU scheme, the value in the relocation register is added to every address generated by a use

addresses; it never sees the real physical addresses.

Page 10: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 10/70

Swapping

 A process can be swapped temporarily out of memory

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

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 thememory swapped.

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

temporarily out of memory to a backing store, and then brought

fast disk large enough to accommodate copies of all memory images for all users;

must provide direct access to these memory images.

swapping variant used for priority-based scheduling algorithms; lower-priority

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 

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

Page 11: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 11/70

Schematic View of SwappingSchematic View of Swapping

Page 12: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 12/70

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 o

logical addresses   each logical address must be less than the limit register.

Contiguous Allocation

Resident operating system, usually held in low memory with interrupt vector.

register scheme used to protect user processes from each other, and from

Relocation register contains value of smallest physical address; limit register contains range o

each logical address must be less than the limit register.

Page 13: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 13/70

Contiguous Allocation (Cont.)

Multiple-partition allocation

  Hole block of available memory; holes of various size are scattered

throughout memory.

  When a process arrives, it is allocated memory from a hole large enough to

accommodate it.

  Operating system maintains information about:

a) allocated partitions b) free partitions (hole)

OS

process 5

process 8

process 2

OS

process 5

process 2

Contiguous Allocation (Cont.)

block of available memory; holes of various size are scattered

When a process arrives, it is allocated memory from a hole large enough to

Operating system maintains information about:

a) allocated partitions b) free partitions (hole)

OS

process 5

process 2

OS

process 5

process 9

process 2

process 9

process 10

Page 14: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 14/70

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

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.

Allocation Problem

hole that is big enough.

hole that is big enough; must search entire list,

unless ordered by size. Produces the smallest leftover hole.hole; must also search entier list. Produces the

fit in terms of speed and storage utilization.

Page 15: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 15/70

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; thissize 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.

  I/O problem

Latch job in memory while it is involved in I/O.

Do I/O only into OS buffers.

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

allocated memory may be slightly larger than requested memory; thissize difference is memory internal to a partition, but not being used.

Shuffle memory contents to place all free memory together in one large block.

if relocation is dynamic, and is done at execution time.

Latch job in memory while it is involved in I/O.

Page 16: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 16/70

Paging

Logical address space of a process can be noncontiguous; process is allocated physical memory

whenever the latter is available.Divide physical memory into fixed-sized blocks called

bytes and 8192 bytes).

Divide logical memory into blocks of same size called pages

Keep track of all free frames.

To run a program of size n pages, need to find n

Set up a page table to translate logical to physical addresses.

Internal fragmentation.

Logical address space of a process can be noncontiguous; process is allocated physical memory

sized blocks called frames (size is power of 2, between 512

Divide logical memory into blocks of same size called pages.

free frames and load program.

Set up a page table to translate logical to physical addresses.

Page 17: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 17/70

Address Translation Scheme

 Address generated by CPU is divided into:

 Page number (p)

 used as an index into a page

page in physical memory.

  Page offset (d) combined with base address to define the physical memory address that is

sent to the memory unit.

Address Translation Scheme

 page table which contains base address of each

combined with base address to define the physical memory address that is

Page 18: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 18/70

Address Translation ArchitectureAddress Translation Architecture

l

Page 19: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 19/70

Paging Example

I l i f P T blI l i f P T bl

Page 20: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 20/70

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

called associative registers or translation look-aside buffers

Implementation of Page Table

PTBR) points to the page table.(PRLR) indicates size of the page table.

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

The two memory access problem can be solved by the use of a special fast-lookup hardware cach

aside buffers (TLBs)

A i i R i

Page 21: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 21/70

Associative Register

 Associative registers   parallel search

 Address translation (A´, A´´)

  If A´ is in associative register, get frame # out.

  Otherwise get frame # from page table in memory

Page #

is in associative register, get frame # out.

Otherwise get frame # from page table in memory

Frame #

Eff ti A TiEff ti A Ti

Page 22: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 22/70

Effective Access Time

 Associative Lookup = e time unit

 Assume memory cycle time is 1 microsecondHit ration   percentage of times that a page number is found in the associative registers; ration

related to number of associative registers.

Hit ratio = a

Effective Access Time (EAT)

EAT = (1 + e) a + (2 + e)(1

= 2 + e   a

Effective Access Time

percentage of times that a page number is found in the associative registers; ration

)(1   a)

M P t ti

Page 23: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 23/70

Memory Protection

Memory protection implemented by associating protection bit with each frame.

Valid-invalid bit attached to each entry in the page table:  valid indicates that the associated page is in the process logical address space, and is thus

legal page.

  invalid indicates that the page is not in the process logical address space.

Memory protection implemented by associating protection bit with each frame.

bit attached to each entry in the page table:valid indicates that the associated page is in the process logical address space, and is thus

invalid indicates that the page is not in the process logical address space.

T L l P T bl S h

T bl S h

Page 24: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 24/70

Two-Level Page-Table SchemeTable Scheme

T L l P i E lL l P i E l

Page 25: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 25/70

Two-Level Paging Example

 A logical address (on 32-bit machine with 4K page size) is divided into:

  a page number consisting of 20 bits.

  a page offset consisting of 12 bits.

Since the page table is paged, the page number is further divided into:

  a 10-bit page number.

  a 10-bit page offset.

Thus, a logical address is as follows:

where pi  is an index into the outer page table, and

the page of the outer page table.

page number page offset

 pi p2

10 10

Level Paging Example

bit machine with 4K page size) is divided into:a page number consisting of 20 bits.

Since the page table is paged, the page number is further divided into:

is an index into the outer page table, and p2  is the displacement within

page offset

12

Add T l ti S hT l ti S h

Page 26: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 26/70

Address-Translation Scheme

 Address-translation scheme for a two-level 32

Translation Scheme

level 32-bit paging architecture

M ltil l P gi g d P fM ltil l P gi g d P f

Page 27: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 27/70

Multilevel Paging and Performance

Since each level is stored as a separate table in memory, covering a logical address to a physical

one may take four memory accesses.Even though time needed for one memory access is quintupled, caching permits performance to

remain reasonable.

Cache hit rate of 98 percent yields:

effective access time = 0.98 x 120 + 0.02 x 520

= 128 nanoseconds.

which is only a 28 percent slowdown in memory access time.

Multilevel Paging and Performance

Since each level is stored as a separate table in memory, covering a logical address to a physical

Even though time needed for one memory access is quintupled, caching permits performance to

effective access time = 0.98 x 120 + 0.02 x 520

= 128 nanoseconds.

which is only a 28 percent slowdown in memory access time.

In erted Page Table

Page 28: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 28/70

Inverted Page Table

One entry for each real page of memory.

Entry consists of the virtual address of the page stored in that real memory location, withinformation about the process that owns that page.

Decreases memory needed to store each page table, but increases time needed to search the

table when a page reference occurs.

Use hash table to limit the search to one or at most a few

Entry consists of the virtual address of the page stored in that real memory location, withinformation about the process that owns that page.

Decreases memory needed to store each page table, but increases time needed to search the

or at most a few page-table entries.

Inverted Page Table ArchitectureInverted Page Table Architecture

Page 29: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 29/70

Inverted Page Table ArchitectureInverted Page Table Architecture

Shared Pages

Page 30: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 30/70

Shared Pages

Shared code

 One copy of read-only (reentrant) 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.

Private code and data

  Each process keeps a separate copy of the code and data.

  The pages for the private code and data can appear anywhere in the logical address space.

only (reentrant) code shared among processes (i.e., text editors, compilers,

Shared code must appear in same location in the logical address space of all processes.

Each process keeps a separate copy of the code and data.

The pages for the private code and data can appear anywhere in the logical address space.

Shared Pages ExampleShared Pages Example

Page 31: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 31/70

Shared Pages ExampleShared Pages Example

Segmentation

Page 32: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 32/70

Segmentation

Memory-management scheme that supports user view of memory.

 A program is a collection of segments. A segment is a logical unit such as:main program,

procedure,

function,

local variables, global variables,

common block,

stack,

symbol table, arrays

management scheme that supports user view of memory.

 A program is a collection of segments. A segment is a logical unit such as:

Logical View of SegmentationLogical View of Segmentation

Page 33: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 33/70

Logical View of Segmentation

1

3

2

4

user space

Logical View of Segmentation

1

4

2

3

physical memory space

Segmentation ArchitectureSegmentation Architecture

Page 34: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 34/70

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 tables location in memory.

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

segment number s is legal if s < STLR.

Segmentation Architecture

dimensional physical addresses; each table entry has:

contains the starting physical address where the segments reside in memory.

points to the segment tables location in memory.

indicates number of segments used by a program;

< STLR.

Segmentation Architecture (Cont )Segmentation Architecture

(Cont )

Page 35: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 35/70

Segmentation Architecture (Cont.)

Relocation.

 dynamic

  by segment table

Sharing.

  shared segments

  same segment number 

 Allocation.

  first fit/best fit

  external fragmentation

Segmentation Architecture (Cont.)

Segmentation Architecture (Cont )Segmentation Architecture

(Cont )

Page 36: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 36/70

Segmentation Architecture (Cont.)

Protection. With each entry in segment table associate:

 validation bit = 0 Þ illegal segment

  read/write/execute privileges

Protection bits associated with segments; code sharing occurs at segment level.

Since segments vary in length, memory allocation is a dynamic storage

 A segmentation example is shown in the following diagram

Segmentation Architecture (Cont.)

Protection. With each entry in segment table associate:

Protection bits associated with segments; code sharing occurs at segment level.

Since segments vary in length, memory allocation is a dynamic storage-allocation problem.

 A segmentation example is shown in the following diagram

Sharing of segments

Page 37: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 37/70

Sharing of segments

Segmentation with PagingSegmentation with Paging

MULTICS

Page 38: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 38/70

Segmentation with Paging

The MULTICS system solved problems of external fragmentation and lengthy search times by

paging the segments.Solution differs from pure segmentation in that the segment

address of the segment, but rather the base address of a

Segmentation with Paging MULTICS

The MULTICS system solved problems of external fragmentation and lengthy search times by

Solution differs from pure segmentation in that the segment-table entry contains not the base

address of the segment, but rather the base address of a page table for this segment.

MULTICS Address Translation SchemeMULTICS Address Translation

Scheme

Page 39: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 39/70

MULTICS Address Translation SchemeMULTICS Address Translation Scheme

Segmentation with PagingSegmentation with Paging

Intel 386

Page 40: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 40/70

Segmentation with Paging

 As shown in the following diagram, the Intel 386 uses segmentation with paging

for memory management with a two-level paging scheme.

Segmentation with Paging Intel 386

 As shown in the following diagram, the Intel 386 uses segmentation with paging

level paging scheme.

Intel 30386 address translationIntel 30386 address translation

Page 41: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 41/70

Intel 30386 address translationIntel 30386 address translation

Page 42: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 42/70

Comparing Memory-Management Strategies

Hardware support

PerformanceFragmentation

Relocation

Swapping

Sharing

Protection

Management Strategies

Module 9: Virtual MemoryModule 9: Virtual Memory

Page 43: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 43/70

Module 9: Virtual Memory

Background

Demand Paging

Performance of Demand Paging

Page Replacement

Page-Replacement Algorithms Allocation of Frames

Thrashing

Other Considerations

Demand Segmenation

Module 9: Virtual Memory

Background

Page 44: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 44/70

Background

Virtual memory   separation of user logical memory from physical memor 

  Only part of the program needs to be in memory for execution.  Logical address space can therefore be much larger than physical address space.

  Need to allow pages to be swapped in and out.

Virtual memory can be implemented via:

  Demand paging

  Demand segmentation

separation of user logical memory from physical memor 

Only part of the program needs to be in memory for execution.Logical address space can therefore be much larger than physical address space.

Need to allow pages to be swapped in and out.

Virtual memory can be implemented via:

Demand Paging

Page 45: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 45/70

Demand Paging

Bring a page into memory only when it is needed.

  Less I/O needed  Less memory needed

  Faster response

  More users

Page is needed Þ reference to it  invalid reference Þ abort

  not-in-memoryÞ bring to memory

Bring a page into memory only when it is needed.

Valid-Invalid Bit

Page 46: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 46/70

Valid Invalid Bit

With each page table entry a valid

(1 Þ in-memory, 0 Þ not-in-memory)

Initially valid invalid but is set to 0 on all entries.

Example of a page table snapshot.

M

Frame #

page table

With each page table entry a valid invalid bit is associated

memory)

invalid but is set to 0 on all entries.

Example of a page table snapshot.

1

1

1

1

0

0

0

valid-invalid bit

Page Fault

Page 47: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 47/70

Page Fault

If there is ever a reference to a page, first reference will trapto

OSÞ page fault

OS looks at another table to decide:

 Invalid reference Þ abort.

  Just not in memory.

Get empty frame.

Swap page into frame.

Reset tables, validation bit = 1.

Restart instruction: Least Recently Used

  block move

If there is ever a reference to a page, first reference will trap

OS looks at another table to decide:

Reset tables, validation bit = 1.

Restart instruction: Least Recently Used

What happens if there is no free frame?What happens if there

is no free frame?

Page 48: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 48/70

What happens if there is no free frame?

Page replacement   find some page in memory, but not really in use, swa

it out.  algorithm

  performance   want an algorithm which will result in minimum number of page faults.

Same page may be brought into memory several times.

What happens if there is no free frame?

find some page in memory, but not really in use, swa

want an algorithm which will result in minimum number of page faults.

Same page may be brought into memory several times.

Performance of Demand PagingPerformance of Demand Paging

Page 49: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 49/70

Performance of Demand Paging

Page Fault Rate 0 £ p £ 1.0

  if  p = 0 no page faults  if  p = 1, every reference is a fault

Effective Access Time (EAT)

EAT = (1   p) x memory access

+ p (page fault overhead

+ [swap page out ]

+ swap page in

+ restart overhead)

Performance of Demand Paging

) x memory access

(page fault overhead

+ [swap page out ]

+ swap page in

+ restart overhead)

Demand Paging ExampleDemand Paging Example

Page 50: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 50/70

Demand Paging Example

Memory access time = 1 microsecond

50% of the time the page that is being replaced has been modified andtherefore needs to be swapped out.

Swap Page Time = 10 msec = 10,000

EAT = (1 

p) x 1 + p (15000)1 + 15000P (in

Demand Paging Example

Memory access time = 1 microsecond

50% of the time the page that is being replaced has been modified and

= 10,000 msec

p) x 1 + p (15000)1 + 15000P (in msec)

Page Replacement

Page 51: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 51/70

Page Replacement

Prevent over-allocation of memory by modifying page

to include page replacement.Use modify (dirty ) bit to reduce overhead of page transfers

pages are written to disk.

Page replacement completes separation between logical memory and

physical memory   large virtual memory can be provided on a smaller 

physical memory.

allocation of memory by modifying page-fault service routine

to reduce overhead of page transfers   only modified

Page replacement completes separation between logical memory and

large virtual memory can be provided on a smaller 

Page-Replacement AlgorithmsReplacement Algorithms

Page 52: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 52/70

g p g

Want lowest page-fault rate.

Evaluate algorithm by running it on a particular string of memoryreferences (reference string) and computing the number of page faults on

that string.

In all our examples, the reference string is

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.

p g

Evaluate algorithm by running it on a particular string of memoryreferences (reference string) and computing the number of page faults on

In all our examples, the reference string is

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.

First-In-First-Out (FIFO) AlgorithmOut (FIFO) Algorithm

Page 53: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 53/70

( ) g

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process)

4 frames

1

2

3

1

2

3

4

1

2

1

2

3

1

2

3

5

1

2

4

5

44 3

( ) g

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 53 frames (3 pages can be in memory at a time per process)

5

3

4

9 page faults

4

5 10 page faults

Optimal Algorithm

Page 54: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 54/70

p g

Replace page that will not be used for longest period of time.

4 frames example

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

How do you know this?

Used for measuring how well your algorithm performs.

1

2

3

4

4 5

Replace page that will not be used for longest period of time.

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Used for measuring how well your algorithm performs.

6 page faults

Least Recently Used (LRU) AlgorithmLeast Recently Used (LRU) Algorithm

Page 55: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 55/70

y ( ) g

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Counter implementation

  Every page entry has a counter; every time page is referenced through this entry, copthe clock into the counter.

  When a page needs to be changed, look at the counters to determine which are to

change.

1

2

3

5

4

4 3

5

y ( ) g

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Every page entry has a counter; every time page is referenced through this entry, cop

When a page needs to be changed, look at the counters to determine which are to

5

4

LRU Algorithm (Cont.)LRU Algorithm (Cont.)

Page 56: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 56/70

g ( )

Stack implementation   keep a stack of page numbers in a double link

form:  Page referenced:

move it to the top

requires 6 pointers to be changed

  No search for replacement

g ( )

keep a stack of page numbers in a double link

LRU Approximation AlgorithmsLRU Approximation Algorithms

Page 57: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 57/70

pp g

Reference bit  With each page associate a bit, initially

  When page is referenced bit set to 1.

  Replace the one which is 0 (if one exists). We do not know the order,

however. Second chance

  Need reference bit.

  Clock replacement.

  If page to be replaced (in clock order) has reference bit = 1. then: set reference bit 0.

leave page in memory.

replace next page (in clock order), subject to same rules.

pp g

With each page associate a bit, initially -= 0

When page is referenced bit set to 1.

Replace the one which is 0 (if one exists). We do not know the order,

If page to be replaced (in clock order) has reference bit = 1. then:

replace next page (in clock order), subject to same rules.

Counting Algorithms

Page 58: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 58/70

g g

Keep a counter of the number of references that have been made to each

page.LFU Algorithm: replaces page with smallest count.

MFU Algorithm: based on the argument that the page with the smallest

count was probably just brought in and has yet to be used.

Keep a counter of the number of references that have been made to each

LFU Algorithm: replaces page with smallest count.

MFU Algorithm: based on the argument that the page with the smallest

count was probably just brought in and has yet to be used.

Allocation of Frames

Page 59: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 59/70

Each process needs minimum number of pages.

Example: IBM 370   6 pages to handle SS MOVE instruction:  instruction is 6 bytes, might span 2 pages.

  2 pages to handle from.

  2 pages to handle to.

Two major allocation schemes.

  fixed allocation

  priority allocation

Each process needs minimum number of pages.

6 pages to handle SS MOVE instruction:instruction is 6 bytes, might span 2 pages.

Fixed Allocation

Page 60: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 60/70

Equal allocation   e.g., if 100 frames and 5 processes, give

each 20 pages.

Proportional allocation   Allocate according to the size of 

process.

mS 

 s pa

m

 sS 

 p s

iii

i

ii

´==

=

å=

=

 for allocation

framesof number total

 processof size

2

1

2

=

=

=

=

=

a

a

 s s

m

i

e.g., if 100 frames and 5 processes, give

 Allocate according to the size of 

5964

137

127

564137

10

127

10

64

»´=

»´

Priority Allocation

Page 61: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 61/70

Use a proportional allocation scheme using priorities rather than size.

If process P i generates a page fault,  select for replacement one of its frames.

  select for replacement a frame from a process with lower priority number.

Use a proportional allocation scheme using priorities rather than size.

select for replacement a frame from a process with lower priority number.

Global vs. Local AllocationGlobal vs. Local Allocation

Page 62: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 62/70

Global replacement   process selects a replacement frame from the set of 

all frames; one process can take a frame from another.Local replacement   each process selects from only its own set of 

allocated frames.

process selects a replacement frame from the set of 

all frames; one process can take a frame from another.each process selects from only its own set of 

Thrashing

Page 63: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 63/70

If a process does not have enough pages, the page

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.

Thrashing º a process is busy swapping pages in and out.

If a process does not have enough pages, the page-fault rate is very

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

a process is busy swapping pages in and out.

Thrashing Diagram

Page 64: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 64/70

Why does paging work?

Locality model  Process migrates from one locality to another.

  Localities may overlap.

Why does thrashing occur?

S size of locality > total memory size

Process migrates from one locality to another.

size of locality > total memory size

Working-Set Model

Page 65: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 65/70

D º working-set window º a fixed number of page references

Example: 10,000 instructionWSS i (working set of Process P i ) =

total number of pages referenced in the most recent

  if D too small will not encompass entire locality.

  if D too large will encompass several localities.

  if D = ¥Þ will encompass entire program.

D = S WSS i º total demand frames

if D > m Þ Thrashing

Policy if D > m, then suspend one of the processes.

a fixed number of page references

total number of pages referenced in the most recent D (varies in time)

too small will not encompass entire locality.

too large will encompass several localities.

will encompass entire program.

> m, then suspend one of the processes.

Keeping Track of the Working SetKeeping Track of the Working Set

Page 66: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 66/70

 Approximate with interval timer + a reference bit

Example: D = 10,000  Timer interrupts after every 5000 time units.

  Keep in memory 2 bits for each page.

  Whenever a timer interrupts copy and sets the values of all reference bits to 0.

  If one of the bits in memory = 1 Þ page in working set.

Why is this not completely accurate?

Improvement = 10 bits and interrupt every 1000 time units.

 Approximate with interval timer + a reference bit

Timer interrupts after every 5000 time units.

Whenever a timer interrupts copy and sets the values of all reference bits to 0.

page in working set.

Why is this not completely accurate?

Improvement = 10 bits and interrupt every 1000 time units.

Page-Fault Frequency SchemeFault Frequency

Scheme

Page 67: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 67/70

Establish acceptable page-fault rate.

  If actual rate too low, process loses frame.

  If actual rate too high, process gains frame.

fault rate.

If actual rate too low, process loses frame.

If actual rate too high, process gains frame.

Other ConsiderationsOther Considerations

Page 68: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 68/70

Preparing

Page size selection  fragmentation

  table size

  I/O overhead

  locality

Other Consideration (Cont.)Other Consideration (Cont.)

Page 69: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 69/70

Program structure

   Array A[1024, 1024] of integer   Each row is stored in one page

  One frame

  Program 1 for  j := 1 to 1024

for i := 1 to 1024 A[i,j ] := 0;

1024 x 1024 page faults

  Program 2 for i := 1 to 1024

for  j := 1 to 1024

 A[i,j ] := 0;1024 page faults

I/O interlock and addressing

:= 1 to 1024 do

:= 1 to 1024 do] := 0;

:= 1 to 1024 do

:= 1 to 1024 do

] := 0;

Demand SegmentationDemand Segmentation

Page 70: Os Amit-memory Management

8/3/2019 Os Amit-memory Management

http://slidepdf.com/reader/full/os-amit-memory-management 70/70

Used when insufficient hardware to implement demand paging.

OS/2 allocates memory in segments, which it keeps track of throughsegment descriptors

Segment descriptor contains a valid bit to indicate whether the segment is

currently in memory.

  If segment is in main memory, access continues,

  If not in memory, segment fault.

Used when insufficient hardware to implement demand paging.

OS/2 allocates memory in segments, which it keeps track of through

Segment descriptor contains a valid bit to indicate whether the segment is

If segment is in main memory, access continues,