Unit 3 Memory Management & Virtual Memory.pdf - Google Drive

0 downloads 127 Views 6MB Size Report
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to op
Unit No: 3 Memory Management & Virtual Memory

Pavan R Jaiswal



  

    

Swapping Demand paging Memory management requirements Memory partitioning Paging, segmentation Security issues Hardware & control structures Linux & Windows memory management Android memory management

Memory Mgmt & Virtual Memory

2



Memory management policies ◦ Allocating swap space

◦ Freeing swap space ◦ Swapping

◦ Demand paging

Memory Mgmt & Virtual Memory

3



Primary memory is a precious resource that frequently cannot

contain all active processes in the system 

The memory management system decides which processes should reside (at least partially) in main memory



It monitors the amount of available primary memory and may periodically write processes to a secondary device called the swap device to provide more space in primary memory



At a later time, the kernel reads the data from swap device back to main memory

Memory Mgmt & Virtual Memory

4

Kernel Process Table

Kernel Region Table

A Process

Per Process Region Table Text File Descriptor Table

Data Stack

U Area

Fig 1 Data structures of process Memory Mgmt & Virtual Memory

5

per process region table

Kernel region table

u area

Kernel process table

main memory

Fig 1 Data structures of process Memory Mgmt & Virtual Memory

6

 Swapping

◦ Easy to implement ◦ Less system overhead  Demand

Paging

◦ Greater flexibility Memory Mgmt & Virtual Memory

7



The swap device is a block device in a configurable section of a disk



Kernel allocates contiguous space on the swap device

without fragmentation 

It maintains free space of the swap device in an in-core table, called map



The kernel treats each unit of the swap map as group of disk blocks



As kernel allocates and frees resources, it updates the map accordingly Memory Mgmt & Virtual Memory

8

Address

1

Unit

10000

Allocate 100 unit

101

9900

Map Allocate 50 unit

251

9850

Allocate 100 unit

151

9750

Fig 2 Allocating swap space Memory Mgmt & Virtual Memory

9

Address 251

Unit 9750

50 unit free at 101

101

50

251

9750

Map

Case 1: Free resources fill a hole, but not contiguous to any resources in the map

Fig 3 freeing swap space Memory Mgmt & Virtual Memory

10

Address 251

Unit 9750

50 unit free at 101

101

50

251

9750

Map 100 unit free at 1

1 251

150 9750

Case 2: Free resources fill a hole, and immediately precedes an entry in the map Memory Mgmt & Virtual Memory

11

Address

Unit

251

9750

50 unit free at 101

101

50

251

9750

Map 100 unit free at 1

1

451

150

Allocate 200 unit

9550 300 unit free at 151

1

10000

1 251

150 9750

Case 3: Free resources fill a hole, and completely fills the gap between entries in the map

Memory Mgmt & Virtual Memory

12



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

Memory Mgmt & Virtual Memory

13





Memory  Swap device Kernel swap out when it needs memory 1. When fork() is called for allocating child process 2. When called for increasing the size of process 3. When process become larger by growth of its stack 4. Previously swapped out process want to swap in but not enough memory Memory Mgmt & Virtual Memory

14



The kernel must gather the page addresses of data at primary memory to be swapped out



Kernel copies the physical memory assigned to a process to the allocated space on the

swap device 

The mapping between physical memory and swap device is kept in page table entry Memory Mgmt & Virtual Memory

15

Physical Addresses

Virtual Addresses

Text

Swap device 684

0

278k

1k

432k :

Data 65k

573k

66k

595k

690

:

Stack 128k

401k :

Fig 4 Mapping process onto the swap device Memory Mgmt & Virtual Memory

16

Physical Addresses

Virtual Addresses

Text

Swap device 684

0

278k

1k

432k :

Data 65k

573k

66k

595k

690

:

Stack 128k

401k :

Fig 5 Swapping a process into memory Memory Mgmt & Virtual Memory

17



There may not be enough memory when fork() called



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



Swap in when kernel schedule it

Memory Mgmt & Virtual Memory

18



It reserves enough space on the swap device to contain the

memory space of the process, including the newly requested space 

Then it adjust the address translation mapping of the process



Finally, it swaps the process 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

Memory Mgmt & Virtual Memory

19



Not all pages of process resides in memory



Locality of reference



When a process accesses a page that is not part of its working set, it incurs a page fault.



The kernel suspends the execution of the

process until it reads the page into memory and makes it accessible to the process Memory Mgmt & Virtual Memory

20



Page table entry



Disk block descriptors



Page frame data table



Swap use table

Memory Mgmt & Virtual Memory

21

Fig 6 PTE and DBD Memory Mgmt & Virtual Memory

22



Contains the physical address of page and the

following bits: ◦ Valid: whether the page contents are 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 contents (required for fork) ◦ Age: Age of the page ◦ Protection: Read/ write permission

Memory Mgmt & Virtual Memory

23



Swap Device number as there may be several swap devices



Block number that contains page

Swap device

Block num

Type

Memory Mgmt & Virtual Memory

24



Basic requirements of Memory Management



Memory Partitioning



Basic blocks of memory management ◦ Paging ◦ Segmentation



Page replacement algorithms

Memory Mgmt & Virtual Memory

25



Memory is cheap today, and getting cheaper ◦ But applications are demanding more and more memory that is never enough!



Memory

Management,

involves

swapping

blocks of data from secondary storage. 

Memory I/O is slow compared to a CPU

Memory Mgmt & Virtual Memory

26



Memory needs to be allocated to ensure a

reasonable supply of ready processes to consume available processor time

Memory Mgmt & Virtual Memory

27



Relocation



Protection



Sharing



Logical organisation



Physical organisation Memory Mgmt & Virtual Memory

28



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

Memory Mgmt & Virtual Memory

29

Memory Management Terms

Term

Description

Frame

Fixed-length

Page

Fixed-length

Segment

Variable-length block of data that

memory.

block

of

main

block of data in secondary memory (e.g. on disk).

resides in secondary memory.

Memory Mgmt & Virtual Memory

30

Fig 7 Addressing requirements of process Memory Mgmt & Virtual Memory

31



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



It is not possible to check absolute addresses at compile time



It must be checked at run time

Memory Mgmt & Virtual Memory

32



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 having their own separate copy

Memory Mgmt & Virtual Memory

33



Memory is organized linearly



Programs are written in modules ◦ Modules can be written and compiled independently



Different degrees of protection given to modules (read-only, execute-only)



Share modules among processes



Segmentation helps here

Memory Mgmt & Virtual Memory

34



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 Memory Mgmt & Virtual Memory

35



Fixed partitioning



Dynamic partitioning



Simple paging



Simple segmentation



Virtual memory paging



Virtual memory segmentation

Memory Mgmt & Virtual Memory

36



Equal-size partitions ◦ Any process whose size is less than or equal to the partition size can be loaded

into an available partition 

The operating system can swap a process out of a partition ◦ If none are in a ready or running state

Memory Mgmt & Virtual Memory

37



A program may not fit in a partition. ◦ The programmer must design the program with overlays



Main memory use is inefficient. ◦ Any program, no matter how small, occupies an entire partition. ◦ This results in internal fragmentation.

Memory Mgmt & Virtual Memory

38



both problems ◦ but doesn’t solve completely



In Fig ◦ Programs up to 16M can be accommodated without overlay ◦ Smaller programs can be placed in smaller partitions, reducing internal fragmentation

Memory Mgmt & Virtual Memory

39



Equal-size ◦ Placement is trivial (no options)



Unequal-size ◦ Can assign each process to the smallest partition within which it will fit ◦ Queue for each partition

◦ Processes are assigned in such a way that memory wastage within partition can be minimized

Memory Mgmt & Virtual Memory

40

Fig 8 Memory assignments for fixed partitioning Memory Mgmt & Virtual Memory

41



The number of active processes are limited by the system ◦ i.e limited by the pre-determined number of

partitions 

A large number of very small processes will not use the space efficiently ◦ In either fixed or variable length partition methods

Memory Mgmt & Virtual Memory

42



Partitions are of variable length and number



Process is allocated exactly as much memory as required

Memory Mgmt & Virtual Memory

43



External Fragmentation



Memory

external

to

all

processes is fragmented 

Can resolve using compaction ◦ OS moves processes so that they are contiguous ◦ Time consuming and wastes CPU

time

Fig 9 Dynamic partitioning Memory Mgmt & Virtual Memory

44



Operating system must decide which free block to allocate to a process



Best-fit algorithm ◦ Chooses the block that is closest in size to the request ◦ Since smallest block is found for process, the

smallest amount of fragmentation is left ◦ Memory compaction must be done more often

Memory Mgmt & Virtual Memory

45



First-fit algorithm ◦ Scans memory from the beginning and chooses the first available block that is large enough

◦ Fastest

Memory Mgmt & Virtual Memory

46



Next-fit ◦ Scans memory from the location of the last placement ◦ More often allocate a block of memory at the end of memory where the largest block is found ◦ The largest block of memory is broken up into smaller blocks ◦ Compaction is required to obtain a large block at the end of memory

Memory Mgmt & Virtual Memory

47

Figure 10 Example memory configuration before & after allocation of 16 MB block Memory Mgmt & Virtual Memory

48



Entire space available is treated as a single block of 2U



If a request of size s where 2U-1 < s