Upload
mian-hassan
View
218
Download
0
Embed Size (px)
Citation preview
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
1/67
Department of Computer Science
DCS
COMSATS Institute ofInformation Technology
Virtual MemoryRab Nawaz Khan adoonAssistant Professor
COMSATS Lahore
Pakistan
Operating System Concepts
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
2/67
Department of Computer Science
Virtual Memory
In computing, virtu l memory is a memorymanagement technique developed formultitasking kernels.
Virtual memory system provides processes with the
illusion that they have more memory than is builtinto the computer.
There are two types of addresses in virtual memorysystems.
Those referenced by processes and, Those available in main memory
The addresses that processes references are calledvirtual addresses.
2
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
3/67
Department of Computer Science
Virtual Memory
The addresses that processes references arecalled virtual addresses.
Those available in the main memory are called,Real addresses or physical addresses. Whenever a process accesses a virtual address, they system
must translate in to real address.
Virtual memory systems contain special purpose hardwarecalled the Memory Management Unit (MMU), that quickly maps
virtual addresses to real addresses.
3
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
4/67
Department of Computer Science
Virtual memory
4
Chip
Address Space Virtual Memory
Disk Drive
PhysicalMemory
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
5/67
Department of Computer Science
How to increase Virtual Memory
5
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
6/67
Department of Computer Science
Virtual memory
The purpose of virtual memory is to enlarge theaddress space, the set of addresses a programcan utilize.
virtual memory might contain twice as many
addresses as main memory.
A program using all of virtual memory, therefore,would not be able to fit in main memory all at once.
Nevertheless, the computer could execute such a
program by copying into main memory thoseportions of the program needed at any given pointduring execution.
6
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
7/67
Department of Computer Science
Virtual Memory
To facilitate copying virtual memory into realmemory, the operating system divides virtualmemory into pages, each of which contains afixed number of addresses.
Each page is stored on a disk until it is needed.
When the page is needed, the operating systemcopies it from disk to main memory, translating thevirtual addresses into real addresses.
7
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
8/67
Department of Computer Science
Virtual Memory
The process of translating virtual addresses intoreal addresses is called mapping.
The copying of virtual pages from disk to mainmemory is known as pagingor swapping.
8
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
9/67
Department of Computer Science
Virtual Memory
Application programs and data are broken upinto small segments, called pages.
Only a small number of these pages are actuallyresident in RAM; the rest are stored in a temporary
area on the hard disk known as a swap file. When the application refers to data that is not
currently in a page in RAM, a page fault occurs.
The operating system then decides which pages in
RAM are least likely to be needed soon, and removesthese pages.
The required data is then moved from the swap file into thepage frames left empty from the previous pages.
9
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
10/67
Department of Computer Science
Virtual Memory
10
RAM
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
11/67
Department of Computer Science
Virtual Memory
When a program is executing, the CPU is
continuously fetching and decoding millions ofinstructions every second. To obtain the nextinstruction or piece of data, the CPU uses thefollowing search path.
Look for the instruction in the L1 cache (built into the CPU). If not found in L1 cache, look in the L2 cache (found on
SRAM chips).
If not found in L2 cache, look for the instruction in RAM(disk cache).
If not found in the disk cache, look for the instruction inRAM (where the program is resident).
If not found in resident RAM ("page fault"), go to the HDDand get the appropriate page(s) from the swap file.
11
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
12/67
Department of Computer Science
Virtual Address Space
A process virtual address space is V is the rangeof virtual addresses that a process mayreference.
The range of the real addresses called, real or
physical address space R.
The number of addresses in V is represented |V| andthe number of addresses in R is denoted |R|.
In virtual memory systems, it is normally the case
that |V| >> |R|, i.e. the virtual address space ismuch larger than the real address space.
12
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
13/67
Department of Computer Science
Two level storage
13
Other types of
Auxiliary storage
Processor (s) Main memory andCaches
Secondary or auxiliary orBacking storage
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
14/67
Department of Computer Science
Implementation detail
A key to implementing virtual memory systemsis how to map virtual addresses to physicaladdresses.
The system must map or translate the virtual
address into real address as process execute.
The system must perform the translation veryquickly, otherwise the performance degradedgracefully.
14
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
15/67
Department of Computer Science
Dynamic Address Translation (DAT)
To convert virtual addresses to physicaladdresses during execution.
The system that exhibit the property of DAT, thecontiguous addresses in a processs virtual address
space need not to be contiguous in physical memory,this is called artificial contiguity.
15
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
16/67
Department of Computer Science
Artificial contiguity
16
Virtual Memory
Physical Memory
Address MappingMechanism
Co
ntiguousVirtual
memory
Locations
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
17/67
Department of Computer Science
Terminology
17
Virtual address Space Real Address Space
A processs virtual address spacerefers to the set of addresses a
process may reference to accessmemory when running on avirtual memory system.
Process do not see the physicaladdresses, which locate physical
locations in main memory.
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
18/67
Department of Computer Science
Block Mapping
DAT mechanisms must maintain addresstranslation maps indicating which regions of aprocesss virtual address space V are currentlyin main memory and where there are located.
If this mapping had to contain entries for everyaddress in V, then this required more space inavailable memory, so this is infeasible.
For this purpose the information is grouped in blocks.
The system then keep track where in main memoryeach virtual memory block has been placed.
18
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
19/67
Department of Computer Science
Mapping
Either the block size should be all the same orof different sizes.
When blocks are of fixed size, they are called pagesand the associated virtual memory organization is
called paging. When blocks may of different sizes, they are called
segments and the associated virtual memoryorganization is called segmentation.
Some system combine the two techniques,implementing segments as variable size blockscomposed of fixed size pages.
19
M i i l dd l
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
20/67
Department of Computer Science
Mapping virtual address to realaddress
20
Virtual MemoryPhysical Memory
Address mappingmechanism
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
21/67
Department of Computer Science
Block Mapping
In the block mapping the system representsaddresses as ordered pairs.
To refer to a particular item in the processs virtualaddress space, a process specifies the block in which
the item resides and the displacement/offset of theitem from the start of the block.
A virtual address vis denoted by an ordered pairv(b,d), where b is the block number and d is the
displacement from the start of the block.
21
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
22/67
Department of Computer Science
Block mapping
Virtual address format in a block mappingsystem
22
Block Number
B
Displacement
d
Virtual Address
V=(b,d)
Vi t l Add T l ti (Bl k
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
23/67
Department of Computer Science
Virtual Address Translation (BlockMapping)
The translation of V=(b,d) to real memoryaddress rproceed as follows,
The system maintains in memory a block map tablefor each process and entries are kept in sequential
order.At context switch time the system determines the
real address, a, that corresponds to the address inmain memory of the new process, block map table.
The system load this address into a high speedspecial purpose register called block map table originregister (BMTOR).
23
Vi t l Add T l ti (Bl k
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
24/67
Department of Computer Science
Virtual Address Translation (BlockMapping)
During execution the process reference v=(b,d).
The system add the block number bto the baseaddress aof the processs block map table to formthe real address of the entry for block b in the blockmap table.
This entry contains the address b, for the start ofthe block bin main memory.
The system then adds the displacement d to theblock start address, b, to form the desired real
address r. Thus the system compute the real address r from the virtual
address v through the equation r = b+ d where b is storedin the block map table cell located at the real memoryaddress a+b.
24
Vi t l Add T l ti (Bl k
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
25/67
Department of Computer Science
Virtual Address Translation (BlockMapping)
25
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
26/67
Department of Computer Science
Issues
Two addition should be computed very fast, notordinary machine language instruction addition.
Accessing the information from the table forentries should be so fast.
If not, then the systems performance will bedegraded gracefully.
26
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
27/67
Department of Computer Science
Paging
A virtual address in a paging system is v=(p,d),where pis the number of the page in virtualmemory on which the referenced item residesand d is the displacement within page pat
which the referenced item is located.
27
Page Number
p
Displacement
d
Virtual AddressV=(p,d)
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
28/67
Department of Computer Science
Paging
A process may run if the page it is currentlyreferencing is in main memory.
When the system transfers a page from secondarystorage to RAM, it places the page in the RAM block,
called a page frame, that has the same size as theincoming page.
28
Dynamic Address Translation
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
29/67
Department of Computer Science
Dynamic Address Translation(Paging)
DAT under paging proceeds as follows,A running process references a virtual memory
address v=(p,d).
A page mapping mechanism, looks up page pin the
processs page map table and determines that pagepis in page frame p.
P is a page frame number, not a physical memoryaddress.
29
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
30/67
Department of Computer Science
Page fault
Whenever a process reference a page, that isnot in the main memory, the processorgenerates a page fault, which invokes theoperating system to load the missing page into
memory from secondary storage.
30
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
31/67
Department of Computer Science
Page table entry (PTE)
A page table is the data structure used by avirtual memory system in a computer operatingsystem to store the mapping between virtualaddresses and physical addresses.
31
Page resident bit
r
Secondary storage address
(if page is not in RAM), s
Page frame number
If page is in RAM, p
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
32/67
Department of Computer Science
Page table entry
A resident bit ris set to 0 if the page is not inthe main memory and 1 if it is.
If the page is not in the RAM, then sis itssecondary storage address.
If the page is in main memory, then pis itsframe number.
32
Page address translation by direct
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
33/67
Department of Computer Science
Page address translation by directmapping
A process references virtual address v=(p,d)
At context switch time the OS loads the mainmemory address of a processs page table into thepage table origin register (PTOR).
To determine the main memory address that
corresponds to the referenced virtual address, thesystem first adds the processs page table baseaddress, b, to the reference page number, p.
Thus b+pis the main memory address of the PTE for
page p. This PTE indicates that virtual page pcorresponds to
page frame p. The system then concatenates pwiththe displacement dto form the real address.
33
Paging address translation by direct
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
34/67
Department of Computer Science
Paging address translation by directmapping
34
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
35/67
Department of Computer Science
Issues
In direct mapping RAM is to be accessed twice. Once for accessing the page table and then to access
the real address from the frame, actually slows theprocessor speed.
As there is a Page table for each process is kept inmain memory, so in this method the RAM is occupiedby these Page tables of the processes instead ofprocesses.
Solution: To keep these table into the content addressed memory i.e.
associative memory.
35
Paging address translation by
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
36/67
Department of Computer Science
Paging address translation byassociative memory
One way to increase the performance of theDAT is to place the entire page table into acontent addressed associative memory.
Which has a cycle time greater than main memory.
A process refer to the virtual address v=(p,d).
Every entry in the associative memory is searchedsimultaneously for page p.
The search returns pas the page frame
corresponding to page p.
Pis then concatenated with d to form the realaddress.
36
Paging address translation by
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
37/67
Department of Computer Science
Paging address translation byassociative memory
37
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
38/67
Department of Computer Science
Issues
This mapping has only one drawback that it isvery expansive.
Only small amount of memory can be offered by thecomputer system, so we cant keep large table on
associative memory.
38
Paging address translation with
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
39/67
Department of Computer Science
Paging address translation withcombined associative/direct mapping
So in the direct mapping all the page tables arein RAM, so many process cant reside inmemory.
It also slows down the processor speed. Then
associative mapping was developed, which was fastbut very expansive.
If a process is very large and contains many pagesthen all the page entries cant be included in the
associative memory. Due to this combined paging address translation was
an option.
39
Paging address translation with
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
40/67
Department of Computer Science
Paging address translation withcombined associative/direct mapping
It is often called TLB (translation look aside buffer).
In combined approach the page map tables aredivided into two parts.
One is in RAM contain those pages which are not
frequently used, and another is in Associativememory, the pages that has cluster of statements.
40
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
41/67
Department of Computer Science
Steps
Running process references virtual addressv=(p,d).
The address translation mechanism first sendscontrol to the associative page map.
If pis found then the frame number of thecorresponding page found simultaneously and thisframe number is concatenated with displacement dto form real address r.
If there is TLB miss, the processor must access mainmemory to obtain the page frame number.
41
Paging address translation with combined
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
42/67
Department of Computer Science
Paging address translation with combinedassociative/direct mapping
42
PTORVirtual AddressV=(p,d)
Performedo
nly
Ifnomatch
in
associativem
ap
Direct Map(All pages)
Translation lookaside Buffer (TLB), AssociativeMemory
Attempt
associative
SearFirs
t
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
43/67
Department of Computer Science
Sharing in paging systems
In multiprogramming/time sharing systems, it iscommon for the user to execute the sameprogram concurrently.
If the system allocated the individual copies, then
much of the memory would be wasted. The obvious solution is to share those pages
common to individual processes.
43
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
44/67
Department of Computer Science
Sharing in paging systems
The system must carefully control sharing toprevent one process from modifying data thatanother process is accessing.
Mostly today system that implements sharing,
programs are divided into separate procedure anddata areas.
Non modifiable procedures / pure procedures / reentrantprocedures
These can be shared.
Modifiable procedures
Cant shared without careful concurrency control
44
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
45/67
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
46/67
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
47/67
Department of Computer Science
Segmentation
Dividing virtual address spaces into variable-length segments.
A virtual address here consists of a segment numberand an offset within the segment.
Each segment consists of contiguous locations, However the segments need not to be the same size nor
must they be placed adjacent to one another in mainmemory.
47
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
48/67
Department of Computer Science
Virtual memory
Paged Virtual Memory
Memory divided into fixed sized pages
Each page has a base physical address
Segmented Virtual Memory
Memory is divided into variable length segments Each segment has a base physical address + length
48
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
49/67
Department of Computer Science
Segmentation
In a VM segmentation system, we will have tomaintain only those segments in main memorythat a program requires to execute at a certaintime.
The remainder of the segments reside on secondarystorage.
Under pure segmentation, the system transfers asegment from secondary storage as a complete unit
and places all the locations within that segment incontiguous location in main memory.
Incoming segment can be placed in any availablearea in RAM that is large enough to hold it.
49
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
50/67
Department of Computer Science
Segmentation
The placement for segmentation is identical tothose used in variable partitionmultiprogramming.
50
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
51/67
Department of Computer Science
Segmentation
A virtual memory segmentation address is anordered pair v=(s,d), where sis the segmentnumber in which the referenced item residesand dis the displacement within segment sat
which the reference item is located.
51
Segment Number
s
Displacement
d
Virtual AddressV=(s,d)
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
52/67
Department of Computer Science
Quiz
Segmented virtual memory systems do notincur fragmentation??? (True/False)
52
Answer:False: Segmented virtual memory systems can incur external fragmentationexactly as in variable partition multiprogramming systems.
Segmentation address translation
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
53/67
Department of Computer Science
Seg e tat o add ess t a s at oby direct mapping
A process references a virtual address v=(s,d)to determine where in main memory thereferenced segment resides.
The system adds the segment number sto the
SMTOR. The resulting value b+s is the location of the
segments table map entry.
Each entry contains several pieces of information
about the segment, which the mapping mechanismuses to translate the Virtual address to physicaladdress.
53
Segmentation address translation
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
54/67
Department of Computer Science
gby direct mapping
If the segment currently resides in mainmemory, the entry contains the segments mainmemory starting address s.
The system adds the displacement dto this address
to form the referenced locations real memoryaddress r=s+d.
We cannot simply concatenate dto s, becausesegments are of variable size.
54
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
55/67
Department of Computer Science
Direct Mapping
55
Segment Number
s
Displacement
d
Virtual AddressV=(s,d)
Base address, b, of
Segment table
SMTOR
+
+
Segment Number
S
Displacement
d
SegmentMap Table
b + s
sb
s + d
s
d
s
Real Addressr=(s,d)
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
56/67
Department of Computer Science
Segment map table entry (SMTE)
56
r a l s
Segmentresident bit
SecondaryStorage addressIf not present inRAM
SegmentLength
Protectionbits
Base addressof Segment
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
57/67
Department of Computer Science
Segmentation
Resident bit
It shows whether or not the segment is currently inmain memory, if it is then sis the main memoryaddress at which the segment begins.
Secondary storage address
If segment is not present in RAM, then ais thesecondary address from which the segment must beretrieved before the process may proceed.
Segment length It specifies the length of the segment referenced. If
it is out of the range then an overflow exception isgenerated.
57
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
58/67
Department of Computer Science
Segmentation
Protection bits These are checked to ensure that the operation
being attempted is allowed. If it is then the baseaddress, s, of the segment in main memory is added
to the displacement, d, to form the real addressr=s+d.
If the operation is not allowed, then a segmentprotection exception is generated, causing the OS togain control of the system and terminate theprocess.
58
Segmented Address Translation
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
59/67
Department of Computer Science
gwith Associate Mapping
Running process references virtual addressv=(s,d).
The system sends the control to the associativememory and every entry in the associative memoryis searched simultaneously for segment s.
If r=1, then displacement dis checked against thelength of the segment.
If d
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
60/67
Department of Computer Science
gwith Associate Mapping
60
Segment Number
s
Displacement
d
Virtual AddressV=(s,d)
s+
Segment Number
S
Displacement
d
Associative Memory
s
Real Addressr=(s,d)
s + d
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
61/67
Department of Computer Science
Segmentation/paging Systems
In this technique the program is first dividedinto segment of variable length logically.
Segments are usually arranged across multiplepages.
All the pages of the segment need not be in mainmemory at once, and virtual memory pages that arecontiguous in virtual memory need not to becontiguous in main memory.
Under this scheme the virtual address is
implemented as an ordered pair, v = (s,p,d), where sis the segment#, pis the page within the segmentand dis the displacement within the page at whichthe desired item is located.
61
DAT in a Segmentation/Paging
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
62/67
Department of Computer Science
g g gSystem
62
DAT in a Segmentation/Paging
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
63/67
Department of Computer Science
System
A process references virtual address v=(s,p,d). The most recently referenced pages have entries in
the associative memory map i.e. TLB.
The translation mechanism performs an associative
search to locate (s,p). If found then it returns p, thepage frame in which page presides.
This value is concatenated with dto form the realaddress r.
63
DAT in a Segmentation/Paging
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
64/67
Department of Computer Science
System
If the TLB does not contain an entry for (s,p),the processor must perform a complete directmapping as follow,
The base address bof SMT (in RAM) is added tosegment s, to form the address b+s.
This address corresponds to the physical memorylocation of the segments entry in the SMT.
The SMT entry indicates the base address sof thepage table (in RAM) for segment s.
The processor adds the page pto the base addresss, to form the address of the page table entry forpage pof segment s.
64
DAT in a Segmentation/Paging
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
65/67
Department of Computer Science
System
This table entry indicates that pis the pageframe number corresponding to virtual page p.
This frame number pis concatenated with thedisplacement dto form the real address r.
65
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
66/67
8/12/2019 Lecture 11 Virtual Memory by Rab Nawaz Jadoon4
67/67