• No results found

OS Unit -3.ppt

N/A
N/A
Protected

Academic year: 2020

Share "OS Unit -3.ppt"

Copied!
88
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Unit III Memory Management

(3)

Objectives

 We know that CPU can be shared by a set of processes. As a result of CPU scheduling, we can improve both the CPU utilization and the speed of the computer’s response to the users.

 To realize this increase in performance, however we must keep several processes in memory; that is , we must share memory.

 The main objective here is to understand the various ways of managing memory.

(4)

Address Binding

 Program must be brought (from disk) into memory for it to be executed.

 Depending on the memory management in use, the process may be moved between disk and memory during its execution.

 The collection of processes on the disk that is waiting to be brought into memory for execution forms the input queue.

 In most cases, a user program will go through several steps where binding takes place at every level.

(5)

Binding of Instructions and Data to Memory

 Address binding of instructions and data to memory addresses can happen at

three different stages

Compile time: If you know at compile time where the process will reside in memory, then 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

(6)
(7)

Logical vs. Physical Address Space

 The 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, one which

(8)

Logical vs. Physical Address Space

Logical address space : The set of all logical addresses generated by a

program is a logical address space.

Physical address space: The set of all physical addresses corresponding

(9)

Memory-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 user process at the time it is sent to memory

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

(10)
(11)

Swapping

 A process can be swapped temporarily out of memory to a backing store, and then

brought back into memory for continued execution.

 Assume a multiprogramming environment with round robin scheduling implementation.

 The quantum must be sufficiently large that reasonable amounts of computing are

done between swaps.

 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.

 Normally, a process that is swapped out will be swapped back into the same memory

(12)

Swapping

 Backing store – fast disk large enough to accommodate copies of all

memory images for all users; must provide direct access to these memory images

(13)
(14)

Contiguous Allocation

 Main memory usually into two partitions:

 Resident operating system

 User processes

 Relocation registers used to protect user processes from each other, and from changing operating-system code and data

 Relocation/Base register contains value of smallest physical address

 Limit register contains range of logical addresses – each logical address must be less than the limit register

(15)
(16)

Base and Limit Registers

(17)
(18)
(19)

Memory Management

 1. Keep track of what parts of memory are in use.

 2. Allocate memory to processes when needed.

 3. Deallocate when processes are done.

(20)
(21)
(22)

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 in a table about:

a) allocated partitions b) free partitions (hole)

(23)

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. There are

many solutions to this problem. The set of holes is searched to

determine which hole is best to allocate.

(24)

Best Fit

Best fit: The allocator places a process in the smallest block of unallocated memory in which it will fit. For example, suppose a process requests 12KB of memory and the memory manager currently has a list of unallocated blocks of 6KB, 14KB, 19KB, 11KB, and 13KB blocks. The best-fit strategy will allocate 12KB of the 13KB block to the process.

Advantage

Memory utilization is much better than first fit as it searches the smallest free partition first available.

Disadvantage

(25)

Worst Fit

Worst fit: The memory manager places a process in the largest block of unallocated memory available. Using the same example as above, worst fit will allocate 12KB of the 19KB block to the process, leaving a 7KB block for future use.

In worst fit approach is to locate largest available free portion so that the portion left will be big enough to be useful. It is the reverse of best fit.

Advantage

Reduces the rate of production of small gaps.

Disadvantage

(26)

First Fit

First fit: There may be many holes in the memory, so the operating system, to reduce the amount of time it spends analyzing the available spaces, begins at the start of primary memory and allocates memory from the first hole it encounters large enough to satisfy the request. Using the same example as above, first fit will allocate 12KB of the 14KB block to the process.

Advantage

Fastest algorithm because it searches as little as possible.

Disadvantage

(27)
(28)

Example

The shaded areas are allocated blocks; the white

areas are free blocks.

The next FOUR memory requests are 20K, 50K, 10K

and 30K (loaded in that order).

Using the following placement algorithms, show the

partition allocated for the requests.

First-fit

Best-fit

Next-fit

40K

80K

30K

50K

30K

1

(29)

29

Example: First-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

(30)

Example: First-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

Allocate for 50K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

(31)

31

Example: First-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

Allocate for 50K

Allocate for 10K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

20

50

(32)

Example: First-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

Allocate for 50K

Allocate for 10K

Allocate for 30K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

20

50

(33)

33

Example: Best-fit

20K, 50K, 10K and 30K (in that order).

40K

80K

30K

50K

30K

1

(34)

Example: Best-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

(35)

35

Example: Best-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

Allocate for 50K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

(36)

Example: Best-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

Allocate for 50K

Allocate for 10K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

20

50

(37)

37

Example: Best-fit

20K, 50K, 10K and 30K (in that order).

Allocate for 20K

Allocate for 50K

Allocate for 10K

Allocate for 30K

40K

80K

30K

50K

30K

1

0K

2

0K

20

K

2

0K

10

K

1

0K

2

0K

20

50

(38)

Contiguous Allocation Techniques

 Given five memory partitions of 100Kb, 500Kb, 200Kb, 300Kb, 600Kb (in order), how would the first-fit, best-fit, and worst-fit algorithms place

(39)

Contiguous Allocation Techniques

 First-fit:

 212K is put in 500K partition  417K is put in 600K partition

 112K is put in 288K partition (new

partition 288K = 500K - 212K)

 426K must wait

Best-fit:

212K is put in 300K partition 417K is put in 500K partition 112K is put in 200K partition 426K is put in 600K partition

Worst-fit:

212K is put in 600K

partition 417K is put in 500K partition 112K is put in 388K partition

426K must wait In this example,

(40)

FRAGMENTATION

All the

preceding algorithms suffer from:

External Fragmentation

As processes are loaded and removed from memory the free memory is broken into little pieces and enough total space exists to satisfy

a

request, but it is not contiguous.

(41)

Internal Fragmentation

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

 One solution to Reduce external fragmentation is 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 as it requires changing the base register to reflect the new base address.

 This scheme can be expensive.

 Another possible solution is to permit the logical address space

(42)
(43)

Paging

 Paging is a memory management scheme that permits the physical address

space of a process to be noncontiguous;

 Divide physical memory into fixed-sized blocks called frames (size is power of 2,

between 512 bytes and 16 MB per page)

 Divide logical memory into blocks of same size called pages

 When a process is to be executed, its pages are loaded into any available

frames from the backing store. The backing store is divided into fixed sized

(44)
(45)
(46)
(47)

Paging Example

(48)

Paging

 No External Fragmentation.

 Internal fragmentation is still there.

(49)

Frame Table

 Since the operating system is managing physical memory, it must be aware of the allocation details of physical memory.: which frames are allocated, which frames are available, how many total frames are there and so on.

(50)
(51)

Hardware Support

 Each operating system has its own methods for storing page tables. Most

allocate a page table for each process. And a pointer to the page table is stored with the other register values in the process control block.

 The hardware implementation of the page table can be done in several

ways. In the simplest case, the page table is implemented as a set of dedicated registers.

 The use of registers for page table is satisfactory if the page table is

(52)

Implementation of Page Table

 Most computers, however allow the page table to be very large (e.g. 1 million

entries) . For these machines, use of fast registers to implement the page table is infeasible. Rather, the Page table is kept in main memory and a

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

(53)
(54)

Effective Access Time

 If the page number is found in the TLB, that is known as TLB hit. Otherwise, it is known as TLB miss.

 The percentage of times that a particular page number is found in the TLB is called TLB hit ratio.

(55)
(56)

Consider a paging hardware with a TLB. Assume that the entire page table and all the pages are in the physical memory. It takes 10 milliseconds to search the TLB and 80 milliseconds to access the physical memory. If the TLB hit ratio is 0.6, the effective memory access time (in milliseconds) is _________.

(A) 120

(B) 122

(57)
(58)
(59)

Memory Protection

 Memory protection in a paged environment is accomplished by associating

protection bit with each frame.

 Normally, these bits are kept in the page table. One bit can define a page to be

read – write or read – only.

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 a legal page

(60)
(61)

Structure of the Page Table

 Hierarchical Paging

 Hashed Page Tables

(62)

Hierarchical Page Tables

 Break up the logical address space into multiple page tables

(63)
(64)

Two-Level Paging Example

 A logical address (on 32-bit machine with 1K page size) is divided into:  a page number consisting of 22 bits

 a page offset consisting of 10 bits

 Since the page table is paged, the page number is further divided into:  a 12-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 p2 is the displacement within the page of the outer page table

page number page offset

pi p2 d

(65)
(66)
(67)

Hashed Page Tables

 Common in address spaces > 32 bit

 The virtual page number is hashed into a page table

 This page table contains a chain of elements hashing to the same location

because the

 same hash function can have same value for different page no. .

 Each element contains

 (1) the virtual page number

 (2) the value of the mapped page frame

 (3) a pointer to the next element

 Virtual page numbers are compared in this chain searching for a match

(68)
(69)

Inverted Page Table

 Usually, each process has an associated page table. The page table has one entry for each real page of memory

 Since, the table is sorted by virtual address, the operating system is able to calculate where in the table the associated physical address entry is and to use that value directly.

 Drawback is that large amount of physical memory is used just to keep track of how other physical memory is used. To solve this, use INVERTED PAGE TABLE.

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

(70)

Inverted Page Table

 An alternate approach is to use the Inverted Page Table structure that consists of one

page table entry for every frame of the main memory. So the number of page table entries in the Inverted Page Table reduces to the number of frames in physical memory and a single page table is used to represent the paging information of all the processes.

 Through inverted page table, the overhead of storing an individual page table for every

process gets eliminated and only a fixed portion of memory is required to store the paging information of all the processes together.

 This technique is called as inverted paging as the indexing is done with respect to the

(71)

Inverted Page Table

Advantages and Disadvantages:

Reduced memory space –

Inverted Page tables typically reduces the amount of memory required to store the page tables to a size bound of physical memory. The maximum number of entries could be the number of page frames in the physical memory.

Longer lookup time –

(72)
(73)

Segmentation

 An important aspect of memory management that became unavoidable with

paging is the separation of user’s view of memory and the actual physical memory.

 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 method object

local variables, global variables stack

(74)

Segmentation

 Elements within a segment are identified by their offset from the beginning of the segment: the first statement of the program, the seventh stack frame entry in the stack and so on.

 Segmentation is a memory management technique that supports this user view of memory.

 A logical address space is a collection of segments. Each segment has a name and length.

(75)
(76)

Segmentation Architecture

 Logical address consists of a two tuple:

<segment-number, offset>,

Segment table – maps physical addresses; each table entry has:

base – contains the starting physical address where the segments

reside in memory

(77)
(78)
(79)

Segmentation

 For example, segment 2 is 400 bytes long and begins at location 4300. thus a reference to byte 53 of segment 2 is mapped onto location

4300+53 = 4353.

 A reference to segment 3 byte 852 is mapped to 3200+852 = 4052.

(80)

External Fragmentation

 Segmentation may cause external fragmentation, when all blocks of free memory are too small to accommodate a segment.

 In this case, the process may simply have to wait until some memory ( or at least a larger hole) becomes available

(81)

Example: The Intel Pentium

 Supports both segmentation and segmentation with paging

 In Pentium systems, the CPU generates logical addresses which are

 Given to segmentation unit

 Which produces linear addresses

 Linear address given to paging unit

 Which generates physical address in main memory

 Segmentation and paging units form the equivalent of MMU

(82)
(83)

Intel Pentium Segmentation

 The Pentium architecture allows a segment to be as large as 4 GB and the

Maximum number of segments per process is 16 KB.

The logical address space of a process is divided into two partitions.

First 8 KB – private to that process (Local descriptor table)

Last 8 KB – shared among all the processes. (Global descriptor table).

Each entry in the LDT and GDT consists of an 8-byte segment descriptor with detailed information about a particular segment , including the base location and limit of that segment.

The logical address is a pair (selector, offset) where the selector is a 16-bit number:

In which s designates the segment number

G indicates whether the segment is in GDT or LDT P deals with the protection.

(84)

Intel Pentium Segmentation

The segment number in selector part points to the appropriate entry in the GDT or LDT.

The base and limit information about the segment in question is used to generate the linear address.

First, the limit is checked for address validity. If the address is not valid, a memory fault is generated.

(85)

Intel Pentium Paging

 The Pentium architecture allows a page size of either 4 KB or 4 MB.

 For 4 KB pages, the Pentium uses a two level paging scheme in which the division of the 32 bit linear address is as follows:

(86)

Pentium Paging Architecture

The ten high order bits reference an entry in the outermost page table, which the Pentium terms the page directory.(The CR3 register points to the page directory for the current process)

The page directory entry points to the inner page table that is indexed by the contents of the innermost ten bits in the linear address.

(87)

Pentium Paging Architecture

One entry in the page directory is the page size flag, which if set indicates that the size of the page frame is 4 MB.

(88)

References

Related documents

Thus, we predicted that depressed participants would exhibit a linear increase in recall rates of the negative target words as a function of the number of times they

• To perform virtual to physical address translation we need to look-up a page table.. • Since page table is in memory, need to

When a page fault occurs, the OS loads the faulted page from disk into a page frame of physical memory. At some point, the process used all of the page frames it is allowed

preferred represent bias scores calculated from the observers gaze. Bias scores represent the average viewing time of the non-preferred subtracted from the preferred viewing time.

11.3.1 When regularly called for jury duty in the manner provided by law, a unit member shall be granted a leave of absence without loss of pay for the time s/he is required

The inverted page table has one entry for each frame Page table size determined by size of physical memory Entries contain page address and process identification The non-inverted

 With paging, physical memory is also split into some number of pages called a page frame..  Page table per process is needed to translate the virtual address to

• Voids in mineral aggregate for specimens containing (RAP + WEO) are higher than the ones containing only RAP, that means VMA increases by increasing the WEO content in the