Chapter 16 - people

jaspersugarlandSoftware and s/w Development

Dec 14, 2013 (4 years and 18 days ago)

96 views


1

Chapter 16

Segmentation, Paging, and Virtual Memory


Topics covered

1.

Memory utilization

2.

Address translation

3.

Segmentation



Translating segment addresses



Addressing the operating system



Segmentation and memory management

4.

Paging

5.

Segmentation and paging

6.

Virtual
memory



Addressing virtual memory



Virtual
-
equals
-
real area



Thrashing



Implementing virtual memory


1.0 Memory Utilization



Multiprogramming implies several programs sharing memory and executing concurrently.
Limiting factor is usually memory space. Fixed
-
pa
rtition memory management wastes space.
Dynamic memory management is more efficient, but tends to leave small fragments of
unused space spread throughout memory. More of this space can be utilized if programs are
broken into independently addressed segme
nts or pages and loaded into non contiguous
memory.



With virtual memory, programs are stored on a secondary device, and only active pages or
segments are loaded.


2.0 Address Translation



Each memory location in a computer is assigned a unique
absolute addr
ess
. Usually, the
bytes or words are numbered sequentially, starting with zero. Hardware fetches and stores
data by absolute address. Specifying an absolute address can force a program to wait until a
particular region of memory is available (and that m
ight
never

happen).



Relative addressing

is a common solution. Programs are written as though they begin at
address 0, and every location in the program is addressed relative to its
entry point.
When
the program is loaded into memory, the absolute addres
s of its entry point is loaded into a

2

base register. As the program runs and instructions are fetched, the addresses found in the
operands are expressed in relative terms. Before an instruction executes, the processor
computes the absolute address of any

referenced location by adding its displacement to the
base address
(Fig. 1.1).


Figure 1.1
As a program runs, the absolute address of any referenced memory location is
computed by adding a displacement to the contents of a base register.









Base




10000




+ Displacement




100




= Absolute address


10100




Address translation

is performed by hardware during I
-
time (Instruction

time), immediately
before the instruction is executed.


3.0 Segmentation



With
segmentation
, programs are di
vided into independently addressed segments and
stored in noncontiguous memory. If a program is loaded as a set of independently addressed
segments, the operating system must keep track of segment’s entry point, and that
complicates address translation.


Figure 1.2

Segmentation complicates address translation because programs are broken into
independent segments and stored in noncontiguous memory.











Base register


10000

Operating System


Other programs


Prog
ram A


Segment #1


20K


Other Programs



Program A


Segment #2


10K


Address


B


100


3

3.1 Translating Segment Addresses




Traditionally, the expanded address was an absolute address.
On a segmented system, the
expanded address consists of two parts: a segment number and a displacement within the
segment. To compute the absolute address, hardware extracts the segment number, uses
that number to search a
segment table
, finds the segmen
t’s entry point address and, finally,
adds the displacement to it, yielding an absolute address.



When a program is loaded, the operating system builds a segment table listing the entry
points of each of its segments. Later, when the program gets control o
f the processor, the
address of its segment table is loaded into the
segment table location register
. During
program execution, this register and the segment table are used by the processor to
dynamically translate addresses.



For example, step through the

dynamic address translation

process on a computer that
uses a 16
-
bit address consisting of a 4
-
bit segment number and a 12
-
bit displacement (Fig.
1.3). The first program in memory is the operating system. Next come several application
programs. Focus o
n program A. Its segment table, located in the operating system and
pointed to by the segment table location register, holds the entry points of the program’s three
segments (Fig. 1.3a).



Imagine the processor has just fetched an instruction. One operand
contains the address
shown in (Fig. 1.3 b). After checking the operation code, the instruction control unit expands
this address, adding the contents of the base register and the displacement. Now the final
address translation process begins. Using the h
igh
-
order 4 bits, the program’s segment table
(pointed to by the segment table location register) is searched, yielding the segment’s entry
point address. The last 12 bits are then added to this base address, giving an absolute
address in the memory.


Fig
ure 1.3

An example of dynamic address translation.


a.

The operating system creates a segment table when it loads a program. Each program has
its own segment table. When a program gets control, the address of its segment table is
loaded into the segment tabl
e location register.






4













Segment table for program A


b.

As the program runs, the segment table provides a base address for computing absolute
addresses.















Segment start address


21,504

Plus displacement



500


Absolute address


22,004




3.2 Addressing the Operating System

Because the operating system begins at memory address 0, its segment/displacement and
absolute addresses are identical.


3.3 Segmentation and Memory Management



The operating system
maintains a table (something like the one pictured in Fig 1.4) listing
regions of memory that are in use and regions that are free. Before it loads a new program,
the operating system searches this table to locate free space.


0 Resident OS


1 OS Tables

2 Transient OS

3 Program A


4 Program B


5 Program A


Segment table location register

Segment


Address



0


12,288

1

17,408

2

21,504

Base

Displacement

2

50
0

Segment


Address



0


12,288


1


17,408

2


21,504


Segment table location
register


5



However, if no segment this

small enough to fit into an available memory fragment, that space
will be wasted. Also, because memory management involves tracking numerous, variable
-
length units of storage, segmentation adds to the operating system’s complexity.


Fig 1.4

The operating

system manages memory space by maintaining a table of free space.
Before loading a program, the operating system searches this table. Note that segment size can
vary.









4.0 Paging



Under
paging
, a program is broken into
fixed
-
length

pages. Page s
ize is generally small (2K
to 4K), and chosen with hardware efficiency in mind. Given small, fixed
-
length pages,
fragmentation is no longer a problem, and memory management is relatively easy to
implement.


Figure1.5

To compute an absolute address under
paging, break the address into page and
displacement portions, use the page number to find the page’s entry point address in a page
table, and add the displacement to this entry point address.








Start address

Length

Status


OK

16K

In use

16K

8K

Free

24K

16K

In use

40K

4K

Free


Note: Status is usually represented by a simple bit flag


6









Page

Start address

0

aaaaaa

1

bb
bbbb

2

cccccc



Page 0


Page 1


Page 2



4.1 Paging and Memory Management



Paging simplifies memory management because all pages are the same length, so the
system never has to check size. The operating system maintains a
page frame table

holdi
ng flags that indicate each page’s status (free or in use). For example, to locate a free
page, the operating system might search the page frame table for a “0” status bit, assign the
first free page to the program, and then change the status bit to a “1”.



Note the difference between this page frame table and the program page table described
earlier. The page frame table is used by the operating system allocate memory space. The
program page table supports dynamic address translation for programs already
in memory.


5.0 Segmentation and Paging



Segmentation complements actual program logic. Paging is more hardware oriented, with
page size geared to a system’s memory allocation scheme; consequently, it’s a bit easier to
manage loading pages. Why not comb
ine segmentation and paging? Programs could then
be broken into logical segments, and the segments subdivided into fixed
-
length pages. Thus,
a programmer could plan a segment structure, while the system loads pages.

Base


D
isplacement

Page


Displacement

Page start address
plus displacement
equals actual address


7



With
segmentation and paging
, addresse
s are divided into a segment number, a page
number within that segment, and a displacement within that page (Fig. 1.6). After the
instruction control unit expands relative address, dynamic address translation begins. First,
the program’s segment table is

found (through the segment table location register) and
searched for the segment. This table, in turn, gives the address of the segment’s page table.
The page table is then searched for the page’s base address, which is added to the
displacement to get
an absolute address.



Dynamic address translation under segmentation and paging involves a great deal of
overhead. Fortunately, there are shortcuts. When a well
-
designed program references an
address on a particular page, the chances are very good that th
e next instruction will
reference the same page. Most segmentation and paging system take advantage of this
relationship to boost dynamic address translation.


Figure 1.6

A segment and paging address is divided into three parts: a segment, a page, and a
d
isplacement. Thus, dynamic address translation involves two table look
-
up operations.



8


Base

Displacement







3

Segment

Page

Displacement


1




2

Segment table




Page table






Page table












Page table








When
an address is translated, its page base address is stored in one of several
page
address registers
.


6.0 Virtual Memory



If a processor can execute only one instruction at a time, it should not be necessary for every
instruction in a program to be in memory

before that program can begin executing. Chapter 5
briefly discussed overly structures in which only portions of a program are in memory at any
given time. Those early overly structures were precursors of modern
virtual memory

systems.



The word virtual
means “not in actual fact.” Virtual memory seems like
real memory
, but it
isn’t. On a traditional system, programs are loaded from a library directly into memory. On a
virtual memory system, programs are loaded from a library into
virtual

memory (Fig. 1
6.11).
Selected portions are then paged into real memory for execution.

Absolute address equals
page real address +
displacement

Segment

Page table
address

1

Aaaaa

2

Bbbbb

3

cccccc


Page

Real address

1

Rrrrr

2

ssss


Page

Real address

1

Nnnn

2

mmmm


Page

Real address

1

Xxxxx

2

Yyyyy

3

zzzzz



9



To the programmer, virtual memory looks just like real memory. Normally, disk space is used
to physically simulative virtual memory. The disk space is broken into fixed
-
length parti
tions
or variable
-
length region, and one program is loaded into each one. Pages or segments are
then transferred between virtual and real memory; the programmer has no control over the
physical paging process.


Figure 1.7
On a virtual memory system, progr
ams are loaded into partitions or regions on
secondary virtual memory, and then paged into real memory for execution.










6.1 Addressing Virtual Memory



To read a page from virtual into real memory, the operating system searches the page frame
table for a free page. Once it finds one, the page’s status is changed to “busy”, and the page
-
in operation begins. Finally, the physical address of the selected page frame is recorded in
the segment and page tables.

Figure 1.8
The contents of virtual me
mory can be assigned segment and page addresses.


On disk

















Library

Program A

Virtual memory




Partition A

(Program A)




O

Real memory

Page
A
o

Seg. 0




1


Operating System


2 192K



3

Program A



4 128K



5



6



7



8




Page 0



Page 1

Page 2


Page 3


Page 4


Page 5


Page 6




10



What happens when a virtual address points to a page that is not in real storage? Each page
table entry contains a flag called (by IBM, at least) the invalid bit. Imagine that
0 bit indicates
the page is in real storage and a 1 bit means the page is in virtual memory. When hardware
encounters a 1 bit at the end of the translation process, a
page fault

is recognized and an
interrupt generated, causing a link to the operating sys
tem’s page
-
in module. If a survey of
the page frame table shows no real memory available for the new page, some other page
must be paged
-
out. While many different schemes have been implemented, most are based
on a least currently accessed algorithm.



Brin
ging pages into memory only when they are referenced is called
demand paging
. An
option called preparing involves predicting the demand for a new page and bring it into
memory before it is actually needed. Many preparing algorithms assume that segment ho
ld
logically related code; for example, if instructions on page1 are currently executing, the
chances are that instructions on page 2 will be executed next. While far from perfect, such
techniques can significantly speed up a program.


6.2 Virtual
-
Equals
-
Real Area



It makes sense to load the operating system in such a way that virtual, real, and absolute
address for key control fields and resident modules are identical. This is possible only in low
memory, so the operating system begins at address 0 both

in virtual and real. The region of
memory where virtual, real, and absolute address match is called the
virtual
-
equals
-
real
area.


6.3 Thrashing



When real memory is full, a demand for a new page means that another must be paged
-
out.
If this happens freq
uently, the system can find itself doing so much paging that little time is left
for the useful work. This is called
thrashing
, and it can seriously degrade system
performance. The solution is to remove a program or two from real memory until the system
settles down.


6.4 Implementing Virtual Memory



In a traditional multiprogramming system, the operating system and several application
programs are loaded into memory. On a virtual memory system, programs are loaded into
secondary storage, and then paged i
nto real memory as necessary.



Virtual memory is divided into two components. The first is exactly equal to the amount of
real memory on the system, and its contents are physically stored in real. It contains the
resident operating system and the transi
ent program area (called the
page pool
). The

11

second component consists of space over and above real memory capacity. This second
part is physically stored on the external paging device. It contains the application programs.


Figure 1.9
Virtual memory i
s a model that holds space for the operating system and several
application programs. The operating system and selected pages occupy real memory.
Application programs are physically stored on an external paging device.








External paging



Virt
ual memory



Real memory



device










Virtual program partitions






Or regions



Available direct access storage



Figure 1.10
Pages are swapped between the external paging device and the real
-
memory page
pool.








External pag
ing







Real memory



device













Paging









Virtual program partitions






Or regions





Resident operating
system


Page pool
(Transient area)


Virtual program
partitions or
regions

Resident operating
sys
tem


Page pool
(Transient area)

Resident operating
system


Page pool
(Transient area)


12

Figure 1.11
In this example, 4 megabytes of virtual memory will be supported by a computer
containing only 1 megabyte of real memory.










Available real memory = 1000K










The resident operating system is loaded into real memory at initial program load or boot time.
Application programs are loaded into partitions or regions on the external paging device by
the operating system
’s scheduling routine. Selected pages are then swapped between the
real memory page pool and the external paging device (Fig.1.11).




____________

Resident operating system

(250K)


Page pool (Transient area)

(750K)


Virtual program partitions or
regions

(3000K)