unit - iii memory management and virtual memory. contents : swapping demand paging hybrid system...

70
Unit - III Memory management and Virtual memory

Upload: egbert-godfrey-allison

Post on 04-Jan-2016

263 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Unit - III

Memory management and Virtual memory

Page 2: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Contents :• Swapping• Demand paging• Hybrid System with swapping and demand paging• Memory management requirements• Memory partitioning• Paging• Segmentation• Security Issues• Hardware and control structures• Operating system software• Linux & Windows memory management

Page 3: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Process State Transition :

1

29

7

34

6 5

8

User Running

Preempted

Zombie

Asleep in

Memory

Sleep, Swapped Ready to Run, Swapped

forkCreated

Ready toRun in Memory

KernelRunning

not enough mem(swapping system only)

swapout

swapin

wakeup

swapout

wakeup enough mem

sleep

rescheduleprocess

preempt

return

returnto user

system call,interruptinterrupt,

interrupt return

Page 4: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Swapping :

Fig. Swapping of processes

Page 5: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Swap Space :

• Swap device -> block device configuration

• Allocation temporary not like files.

• I/O faster for group of block

• Map data structure

• Follows first fit algorithm of continuous blocks

Page 6: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Allocating Swap Space :

1 10000 101 9900

151 9850251 9750

Allocate 100 unit

Allocate 100 unit

Allocate 50 unit

Map

Address Unit

Page 7: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Allocate Swap Space :• malloc( address_of_map, number_of_unit)• for (every map entry)

• if (current map entry can fit requested units)• if (requested units == number of units in entry)

• Delete entry from map• else

• Adjust start address of entry• return original address of entry

• return -1

Page 8: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Freeing Swap Space :

251 975050 unit free at 101

Map

Address Unit101 50

251 9750

Case 1: Free resources fill a hole,

but not contiguous to any resources in the map

Page 9: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Freeing Swap Space :

251 975050 unit free at 101

Map

Address Unit 101 50

251 9750

100 unit free at 1

1 150

251 9750

Case 2: Free resources fill a hole,

and immediately precedes an entry in the map

Page 10: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Freeing Swap Space :

251 975050 unit free at 101

Allocate 200 unit

Map

Address Unit 101 50

251 9750

100 unit free at 1

1 150

251 9750

1 150

451 9550

1 10000

300 unit free at 151 Case 3: Free resources fill a hole, and completely fills the gap between entries in the map

Page 11: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Swapping Process Out• Memory Swap device• Kernel swap out when it needs memory

1. When fork() called for allocate child process2. When called for increase the size of process3. When process become larger by growth of its stack4. Previously swapped out process want to swap in but not

enough memory

Page 12: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Swapping Process Out

:

128k 401k

:

66k 595k

65k 573k

:

1k 432k

0 278k

Virtual Addresses Swap device684

688

Text

Data

Stack

Physical Addresses

Fig. Mapping process onto the swap device

Page 13: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Swapping Process In

:

128k 401k

:

66k 595k

65k 573k

:

1k 432k

0 278k

Virtual Addresses Swap device684

688

Text

Data

Stack

Physical Addresses

Fig. Swapping a process into memory

Page 14: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Fork Swap• There may not be enough memory when fork() called

• Child process swap out and “ready-to-run”

• Swap in when kernel schedule it

Page 15: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Expansion Swap• Reserves enough space on the swap device• Adjust the address translation mapping of the process (virtual

address)• Swaps out on newly allocated space in swapping device• When the process swaps the process into memory, it will

allocate physical memory according to new address translation map

Page 16: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Swapping Operation :

Page 17: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Demand paging :

• Not all page of process resides in memory

• Principle of locality

• Working set, page fault, page hit.

• Page replacement strategies (LRU/OPR)

• The kernel suspends the execution of the process until it reads

the page into memory and makes it accessible to the process

Page 18: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Data Structure for Demand Paging :

• Page table entry

• Disk block descriptors

• Page frame data table (pfdata)

• Swap use table

Page 19: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Page Table Entry and Disk Block Descriptor

Disk Block DescriptorPage Table Entry

Page address age Cp/wrt mod ref val prot

Region

Page Table Entry

Page Table

Type(swap,file,demand fill 0/1)

Block numSwap device

Disk Block Descriptor

Page 20: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

• Contains the physical address of page and the following bits:• Valid: whether the page content legal• Reference: whether the page is referenced recently• Modify: whether the page content is modified• copy on write: kernel must create a new copy when a

process modifies its content (required for fork)• Age: Age of the page• Protection: Read/ write permission

Page Table Entry

Page 21: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

pftable entries :• Page state- on swap device or executable file• Reference count-no of processes referencing current page• Logical device and block no• Pointers to other pftable entries

Page 22: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Memory Management Requirements

• Memory management is intended to satisfy the following requirements:• Relocation• Protection• Sharing• Logical organization• Physical organization

Page 23: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Relocation

• Relocation is the process of adjusting program addresses to match the actual physical addresses where the program resides when it executes• Why is relocation needed?• Programmer/translator don’t know which other

programs will be memory resident when the program executes

Page 24: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Relocation

• Why is relocation needed? (continued)• Active processes need to be able to be swapped in and out

of main memory in order to maximize processor utilization• Specifying that a process must be placed in the same

memory region when it is swapped backin would be limiting

• Consequently it must be possible to adjust addresses whenever a programis loaded.

Page 25: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Protection• Processes need to acquire permission to reference memory

locations for reading or writing purposes• Location of a program in main memory is unpredictable• Memory references generated by a process must be checked at run

time• Mechanisms that support relocation also support protection

Page 26: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Sharing

• Advantageous to allow each process access to the same copy of the program rather than have their own separate copy

• Memory management must allow controlled access to shared areas of memory without compromising protection

• Mechanisms used to support relocation support sharing capabilities

Page 27: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Logical Organization

• Main memory is organized as a linear (1-D) address space consisting of a sequence of bytes or words.

• Programs aren’t necessarily organized this way• Paging versus segmentation

Programs are written in modules

• modules can be written and compiled independently• different degrees of protection given to modules (read-

only, execute-only)• sharing on a module level corresponds to the user’s way of

viewing the problem

Page 28: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Physical Organization

• Two-level memory for program storage:• Disk (slow and cheap) & RAM (fast and more expensive)• Main memory is volatile, disk isn’t

• User should not have to be responsible for organizing movement of code/data between the two levels.

Page 29: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Physical Organization

Cannot leave the programmer with the

responsibility to manage memory

Memory 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 30: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Memory Partitioning• Virtual memory management brings processes into main memory for execution by the processor

involves virtual memory based on segmentation and paging

• Partitioned memory management used in several variations in some now-obsolete operating

systems does not involve virtual memory

Page 31: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Fixed Partitioning• Equal-size partitions• The operating system can swap

out a process if all partitions are full and no process is in the Ready or Running state

Page 32: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Disadvantages

• A program may be too big to fit in a partition • program needs to be designed with the use of overlays

• Main memory utilization is inefficient • any program, regardless of size, occupies an entire partition• internal fragmentation • wasted space due to the block of data loaded being

smaller than the partition

Page 33: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Unequal Size Partitions

• Using unequal size partitions helps lessen the problems• programs up to 16M can be accommodated

without overlays• partitions smaller than 8M allow smaller

programs to be accommodated with less internal fragmentation

Page 34: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Memory Assignment

Page 35: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

• The number of partitions specified at system generation time limits the number of active processes in the system• Small jobs will not utilize partition space efficiently

Disadvantages :

Page 36: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

• Partitions are of variable length and number• Process is allocated exactly as much memory as it

requires• This technique was used by IBM’s mainframe operating

system.

Dynamic Partitioning :

Page 37: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements
Page 38: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

• memory becomes more and more fragmented• memory utilization declines

External Fragmentation

• technique for overcoming external fragmentation• OS shifts processes so that they are contiguous• free memory is together in one block• time consuming and wastes CPU time

Compaction

Dynamic Partitioning :

Page 39: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Placement Algorithms

Best-fit

• chooses the block that is closest in size to the request

First-fit

• begins to scan memory from the beginning and chooses the first available block that is large enough

Next-fit

• begins to scan memory from the location of the last placement and chooses the next available block that is large enough

Page 40: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements
Page 41: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Buddy System

• Comprised of fixed and dynamic partitioning schemes• Space available for allocation is treated as a

single block• Memory blocks are available of size 2K words, L

≤ K ≤ U, where • 2L = smallest size block that is allocated • 2U = largest size block that is allocated; (generally 2U is the size of the

entire memory available for allocation)

Page 42: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Buddy System Example

Page 43: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements
Page 44: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Addresses

• reference to a memory location independent of the current assignment of data to memory

Logical

• address is expressed as a location relative to some known point

Relative

• actual location in main memory

Physical or Absolute

Page 45: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

• Partition memory into equal fixed-size chunks that are relatively small

• Process is also divided into small fixed-size chunks of the same size

Pages

• chunks of a process

Frames

• available chunks of memory

Paging :

Page 46: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements
Page 47: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Page Table

• Maintained by operating system for each process• Contains the frame location for each page in the process• Processor must know how to access the page table for the current

process• Used by processor to produce a physical address

Page 48: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Logical Addresses

Page 49: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Logical-to-Physical Address Translation - Paging

Page 50: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Segmentation

• A program can be subdivided into segments may vary in length there is a maximum length

• Addressing consists of two parts: segment number an offset

• Similar to dynamic partitioning• Eliminates internal fragmentation

Page 51: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Logical-to-Physical Address Translation - Segmentation

Page 52: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Security Issues

If a process has not declared a portion of its memory to be sharable, then no other process

should have access to the contents of that portion

of memory

If a process declares that a portion of memory may be shared by other designated processes then the security

service of the OS must ensure that only the

designated processes have access

Page 53: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Buffer Overflow Attacks• Security threat related to memory management• Also known as a buffer overrun• Can occur when a process attempts to store data beyond the limits

of a fixed-sized buffer• One of the most prevalent and dangerous types of security attacks

Page 54: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Defending Against Buffer Overflows

• Prevention• Detecting and aborting• Countermeasure categories:

Compile-time Defenses • aim to harden programs to resist attacks in new

programs

Run-time Defenses • aim to detect and abort attacks in existing programs

Page 55: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Translation Lookaside Buffer• Each virtual memory reference can cause two physical

memory accesses• One to fetch the page table• One to fetch the data

• To overcome this problem a high-speed cache is set up for page table entries• Called a Translation Lookaside Buffer (TLB)

• Contains page table entries that have been most recently used

Page 56: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Translation Lookaside Buffer

Page 57: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Translation Lookaside Buffer• Given a virtual address, processor examines the TLB• If page table entry is present (TLB hit), the frame number is

retrieved and the real address is formed• If page table entry is not found in the TLB (TLB miss), the page

number is used to index the process page table.

• First checks if page is already in main memory • If not in main memory a page fault is issued

• The TLB is updated to include the new page entry

Page 58: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Translation Lookaside Buffer

Page 59: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Translation Lookaside Buffer

Page 60: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Translation Lookaside Buffer

Page 61: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Page Size• Smaller page size, more pages required per process• More pages per process means larger page tables• Larger page tables means large portion of page tables in

virtual memory• Small page size, large number of pages will be found in main

memory• As time goes on during execution, the pages in memory will all

contain portions of the process near recent references. Page faults low.

• Increased page size causes pages to contain locations further from any recent reference. Page faults rise.

Page 62: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Basic Replacement Algorithms• FIFO• LRU• OPR• Clock Policy• Additional bit called a use bit• When a page is first loaded in memory, the use bit is set to 1• When the page is referenced, the use bit is set to 1• When it is time to replace a page, the first frame encountered

with the use bit set to 0 is replaced.• During the search for replacement, each use bit set to 1 is

changed to 0

Page 63: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Clock Policy

Page 64: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Clock Policy

Page 65: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Resident Set Size• Fixed-allocation• Gives a process a fixed number of frames within which to execute• When a page fault occurs, one of the pages of that process must

be replaced• Variable-allocation• Number of pages allocated to a process varies over the lifetime of

the process

Page 66: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Fixed Allocation, Local Replacement Scope• Decide ahead of time the amount of allocation to give a

process• If allocation is too small, there will be a high page fault rate• If allocation is too large there will be too few programs in main

memory• Processor idle time• Swapping

Page 67: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Variable Allocation, Local Replacement Scope• When new process added, allocate number of page frames

based on application type, program request, or other criteria• When page fault occurs, select page from among the resident

set of the process that suffers the fault• Reevaluate allocation from time to time

Page 68: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Variable Allocation, Global Replacement Scope• Easiest to implement• Adopted by many operating systems• Operating system keeps list of free frames• Free frame is added to resident set of process when a page

fault occurs• If no free frame, replaces one from another process

Page 69: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Cleaning Policy• Demand cleaning• A page is written out only when it has been selected for

replacement• Precleaning• Modified pages are written before their frame is needed• Pages are written out in batches

Page 70: Unit - III Memory management and Virtual memory. Contents : Swapping Demand paging Hybrid System with swapping and demand paging Memory management requirements

Cleaning Policy• Best approach uses page buffering• Replaced pages are placed in two lists

• Modified and unmodified• Pages in the modified list are periodically written out in batches• Pages in the unmodified list are either reclaimed if referenced

again or lost when its frame is assigned to another page