You are on page 1of 64

Yongchuan Liu

School of Computer Science


Outline

◆ Introduction to memory management


◆ Memory abstraction
◆ Virtual memory
◆ Page replacement algorithms
◆ Design issues for paging systems
◆ Segmentation
◆ Exercise
◆ Assignments

2
3
Introduction to memory management
◆ What is memory management?

In a uniprogramming system, main memory is divided into two


parts: one part for the operating system (resident monitor, kernel)
and other part for the program currently being executed. In a
multiprogramming system, the “user” part of memory must be
further subdivided to accommodate multiple processes. The task
of subdivision is carried out dynamically by the operating system
and is known as memory management.

4
Introduction to memory management
◆ Some concepts about memory management
Frame A fixed-length block of main memory.
A fixed-length block of data that resides in secondary memory (such as
Page disk). A page of data may temporarily be copied into a frame of main
memory.
A variable-length block of data that resides in secondary memory. An
entire segment may temporarily be copied into an available region of main
Segment memory (segmentation) or the segment may be divided into pages which
can be individually copied into main memory (combined segmentation and
paging).

5
Introduction to memory management
◆ Memory management requirements
➢ Relocation
➢ Protection
➢ Sharing
➢ Logical organization
➢ Physical organization

6
Introduction to memory management
◆ Relocation

7
Introduction to memory management
◆ Protection
➢ Each process should be protected against unwanted
interference by other processes, whether accidental or
intentional.
➢ Normally, a user process cannot access any portion of
the operating system, neither program nor data.
➢ The memory protection requirement must be satisfied by
the processor (hardware) rather than the operating
system (software).

8
Introduction to memory management
◆ Sharing
➢ Any protection mechanism must have the flexibility to
allow several processes to access the same portion of
main memory.

9
Introduction to memory management
◆ Logical organization
➢ Memory in a computer system is organized as a linear,
or one-dimensional, address space, consisting of a
sequence of bytes or words. It does not correspond to
the way in which programs are typically constructed.
➢ Most programs are organized into modules. If the
operating system and computer hardware can
effectively deal with user programs and data in the form
of modules of some sort, then a number of advantages
can be realized.

10
Introduction to memory management
◆ Physical organization
➢ Computer memory is organized into at least two levels,
referred to as main memory and secondary memory.
➢ In this two-level scheme, the organization of the flow of
information between main and secondary memory is a
major system concern.
➢ The task of moving information between the two levels
of memory should be a system responsibility.

11
12
Memory abstraction
◆ Memory management is needed
➢ The ideal memories: private, infinitely large, infinitely
fast, nonvolatile.
➢ The second choice: memory hierarchy.
➢ OS abstracts this hierarchy into a useful model and
manage it. That is memory abstraction.

13
Memory abstraction
➢ The memory hierarchy
• Going down the hierarchy:
✓ decreasing cost per bit g-
Re r s
e
i st
Inb
✓ increasing capacity Me o a r d Ca
ch
e
mo in
ry M a or y
m
✓ increasing access time Me

✓ decreasing frequency of access Ou ic Di


sk
t et
Sto boar gn OM
ra g d M a D- R W
to the memory by the processor e C D -R W
C
D-
R M
D V D- R A y
a
DV lu-R
B

Of ap
e
S t o f - li n e e ti
cT
rag gn
e Ma

14
Memory abstraction
◆ No Memory Abstraction
➢ Three simple ways of organizing memory with an operating
system and one user process.

15
Memory abstraction
➢ Running multiple programs without a memory abstraction
Even with no memory abstraction, it is possible to run multiple
programs at the same time. What the operating system has to do is
save the entire contents of memory to a disk file, then bring in and
run the next program. As long as there is only one program at a time
in memory, there are no conflicts.

16
Memory abstraction
➢ Illustration of the relocation problem.
• (a) A 16-KB program.
• (b) Another 16-KB program.
• (c) The two programs loaded consecutively into memory.

16412 – 16384 = 28
The first instruction executed is
JMP 28, which jumps to the ADD
instruction in the first program,
instead of the CMP instruction it is
supposed to jump to.

17
Memory abstraction
◆ A memory abstraction: address spaces
➢ Exposing physical memory to processes has two major
drawbacks:
• If user programs can address every byte of memory, they can
easily trash the operating system.
• It is difficult to have multiple programs running at once.

➢ Address space is the set of addresses that a process can


use to address memory. Each process has its own
address space, independent of those belonging to other
processes.

18
Memory abstraction 3

◆ Base and Limit Registers


➢ Base and limit registers can be used to give each process a
separate address space.
When a process is run, the base register
is loaded with the physical address
where its program begins in memory
and the limit register is loaded with the
length of the program.

16384 + 28 = 16412

19
Memory abstraction
◆ Swapping
➢ In practice, the total amount of RAM needed by all the
processes is often much more than can fit in memory.
➢ swapping: bringing in each process in its entirety,
running it for a while, then putting it back on the disk.

20
Memory abstraction
➢ A problem: how much memory should be allocated for a process
when it is created or swapped in?
• If processes are created with a fixed size that never changes, then
the allocation is simple: the operating system allocates exactly
what is needed, no more and no less.

21
Memory abstraction
• However, processes’ data segments can grow: it is probably a
good idea to allocate a little extra memory whenever a process is
swapped in or moved. (figure (a))
• If processes can have two growing segments, an alternative
arrangement suggests itself. (figure (b))

22
Memory abstraction
◆ Memory management with bitmaps or linked lists
➢ (a) A part of memory with five processes and three holes. The
tickmarks show the memory allocation units. The shaded regions (0 in
the bitmap) are free.
➢ (b) The corresponding bitmap.
➢ (c) The same information as a list.

23
Memory abstraction
◆ Algorithms used to allocate memory for a
created process
➢ First fit: The memory manager scans along the list of segments
until it finds a hole that is big enough.

➢ Next fit: It works the same way as first fit, except that it keeps
track of where it is whenever it finds a suitable hole.

➢ Best fit: It searches the entire list, from beginning to end, and
takes the smallest hole that is adequate.

➢ Worst fit: The opposite of best fit.

➢ Quick fit: It maintains separate lists for some of the more


common sizes requested.
24
25
Virtual memory
◆ Basic idea
➢ Each program has its own address space, which is broken up into
chunks called pages.
➢ Each page is a contiguous range of addresses. These pages are
mapped onto physical memory, but not all pages have to be in
physical memory at the same time to run the program.
➢ When the program references a part of its address space that is in
physical memory, the hardware performs the necessary mapping
on the fly.
➢ When the program references a part of its address space that is
not in physical memory, the operating system is alerted to go get
the missing piece and re-execute the instruction that failed.

26
Virtual memory
◆ Virtual memory terminology
A storage allocation scheme in which secondary memory can be
addressed as though it were part of main memory. The addresses a
program may use to reference memory are distinguished from the
addresses the memory system uses to identify physical storage sites,
Virtual memory and program-generated addresses are translated automatically to the
corresponding machine addresses. The size of virtual storage is
limited by the addressing scheme of the computer system and by the
amount of secondary memory available and not by the actual number
of main storage locations.
The address assigned to a location in virtual memory to allow that
Virtual address
location to be accessed as though it were part of main memory.
Virtual address The virtual storage assigned to a process.
space
The range of memory addresses available to a process.
Address space

Real address The address of a storage location in main memory.

27
Virtual memory
◆ MMU (Memory Management Unit)
➢ A part of the CPU chip.
➢ When virtual memory is used, the virtual addresses do not go
directly to the memory bus. Instead, they go to an MMU that
maps the virtual addresses onto the physical memory addresses.

28
Virtual memory
➢ An example of how to map the virtual addresses onto the
physical memory addresses for the MMU.

• pages: The virtual address space consists


of fixed-size units called pages.
• page frames: The corresponding units in
the physical memory are called page
frames.
• The pages and page frames are generally
the same size.
• page table: The relation between virtual
addresses and physical memory addresses
is given by the page table.

Page frame no.

29
Virtual memory
➢ The internal operation of the MMU with 16 4-KB pages.

30
Virtual memory
◆ Structure of a page table entry
➢ A typical page table entry.

31
Virtual memory
◆ Page size
➢ Determining the best page size requires balancing
several competing factors. There is no overall optimal
page size.

32
Virtual memory
➢ Advatages for a small page size.
A randomly chosen text, data, or stack segment will not fill an integral number
of pages. On the average, half of the final page will be empty. The extra space
in that page is wasted. This wastage is called internal fragmentation. With n
segments in memory and a page size of p bytes, np/2 bytes will be wasted on
internal fragmentation. This reasoning argues for a small page size.

Think about a program consisting of eight sequential phases of 4 KB each.


• With a 32-KB page size, the program must be allocated 32 KB all the time.
• With a 16-KB page size, it needs only 16 KB.
• With a page size of 4 KB or smaller, it requires only 4 KB at any instant.

In general, a large page size will cause more wasted space to be in memory
than a small page size.

33
Virtual memory
➢ Advatages for a large page size.
Small pages mean that programs will need many pages, and thus a large page
table. A 32-KB program needs only four 8-KB pages, but 64 512-byte pages.
Transfers to and from the disk are generally a page at a time, with most of the
time being for the seek and rotational delay, so that transferring a small page
takes almost as much time as transferring a large page. It might take 64 × 10
msec to load 64 512-byte pages, but only 4 × 12 msec to load four 8-KB
pages.

34
Virtual memory
◆ Speeding up paging
➢ Major issues faced:
• The mapping from virtual address to physical address must be
fast.
• If the virtual address space is large, the page table will be large.

35
Virtual memory
◆ Multilevel Page Tables
➢ Modern computers use virtual addresses of at least 32
bits, with 64 bits becoming the norm for desktops and
laptops.
➢ Large virtual address space needs a large number of
pages.
➢ In order to avoid keeping all the page tables in memory
all the time, multilevel page tables are needed.

36
Virtual memory
◆ How does the two-level page table works?

37
38
Page replacement algorithms
◆ Introduction

When a page fault occurs, the operating system has to choose a


page to evict (remove from memory) to make room for the
incoming page.

While it would be possible to pick a random page to evict at each


page fault, system performance is much better if a page that is not
heavily used is chosen. If a heavily used page is removed, it will
probably have to be brought back in quickly, resulting in extra
overhead.

39
Page replacement algorithms
◆ 8 page replacement algorithms
➢ Optimal algorithm (impossible to actually implement)
➢ Not recently used algorithm
➢ First-in, first-out (FIFO) algorithm
➢ Second-chance algorithm
➢ Clock algorithm
➢ Least recently used (LRU) algorithm
➢ Working set algorithm
➢ WSClock algorithm

The related contents are in p209-222 of the textbook.

40
Page replacement algorithms
◆ Not recently used (NRU) algorithm
➢ When a page fault occurs, the operating system inspects all the
pages and divides them into four categories based on the current
values of their R and M bits:
• Class 0: not referenced, not modified.
• Class 1: not referenced, modified.
• Class 2: referenced, not modified.
• Class 3: referenced, modified.

The NRU algorithm removes a page at random from the lowest-


numbered nonempty class. Implicit in this algorithm is the idea
that it is better to remove a modified page that has not been
referenced in at least one clock tick (typically about 20 msec)
than a clean page that is in heavy use.
41
Page replacement algorithms
◆ First-in, first-out (FIFO) algorithm

The operating system maintains a list of all pages currently in


memory, with the most recent arrival at the tail and the least
recent arrival at the head. On a page fault, the page at the head is
removed and the new page added to the tail of the list.

42
Page replacement algorithms
◆ Least recently used (LRU) algorithm

When a page fault occurs, throw out the page that has been
unused for the longest time.

The idea: pages that have been heavily used in the last few
instructions will probably be heavily used again soon. Conversely,
pages that have not been used for ages will probably remain
unused for a long time.

43
Page replacement algorithms
◆ Summary of Page Replacement Algorithms

44
45
Page replacement algorithms
◆ The related contents
➢ Local versus Global Allocation Policies, p222
➢ Load Control, p225
➢ Page Size, p225
➢ Separate Instruction and Data Spaces, p227
➢ Shared Pages, p228
➢ Shared Libraries, p229
➢ Mapped Files, p231
➢ Cleaning Policy, p232
➢ Virtual Memory Interface, p232

46
47
Segmentation
◆ Examples of tables generated by compiler

48
Segmentation 1

◆ A question
➢ What happens if a program has a much larger than usual number
of variables but a normal amount of everything else?
The chunk of address space allocated for the symbol table may
fill up, but there may be lots of room in the other tables.

49
Segmentation
➢ A straightforward and quite general solution:
Provide the machine with many completely independent address
spaces, which are called segments. Each segment consists of a
linear sequence of addresses, starting at 0 and going up to some
maximum value. The length of each segment may be anything
from 0 to the maximum address allowed. Different segments may,
and usually do, have different lengths. Moreover, segment
lengths may change during execution. The length of a stack
segment may be increased whenever something is pushed onto
the stack and decreased whenever something is popped off the
stack.

50
Segmentation
◆ A segmented memory
➢ A segment is a logical entity, which might contain a procedure, or
an array, or a stack, or a collection of scalar variables, but usually
it does not contain a mixture of different types.
➢ A segmented memory allows each table to grow or shrink
independently of the other tables.

51
Segmentation
◆Advantages for the segmentation
➢ A segmented memory can simplify the handling of data structures
that are growing or shrinking.
➢ A segmented memory can greatly simplify the linking of
procedures compiled separately.
➢ It lends itself to sharing among processes. A programmer can
place a utility program or a useful table of data in a segment that
can be referenced by other processes.
➢ It lends itself to protection. Because a segment can be constructed
to contain a well-defined set of programs or data, the programmer
or system administrator can assign access privileges in a
convenient fashion.

52
Segmentation
◆ Comparison of paging and segmentation

53
Segmentation
◆ Implementation of Pure Segmentation

54
Segmentation
◆ Segmentation with paging
In a combined paging/segmentation system, a user’s address
space is broken up into a number of segments, at the discretion of
the programmer. Each segment is, in turn, broken up into a
number of fixed-size pages, which are equal in length to a main
memory frame. If a segment has length less than that of a page,
the segment occupies just one page. From the programmer’s
point of view, a logical address still consists of a segment number
and a segment offset. From the system’s point of view, the
segment offset is viewed as a page number and page offset for a
page within the specified segment.

55
Segmentation
➢ Address translation in a segmentation/paging system

56
Segmentation
◆ Two instances for segmentation with paging
➢ Segmentation with Paging: MULTICS
Text book: p243-247

➢ Segmentation with Paging: The Intel x86


Text book: p247-252

57
58
Problem 1
➢ A process has 3 page frames (F0, F1, F2) and 5 pages
(from page 0 to 4), and the with the reference string is: 2,
4, 3, 2, 0, 2, 1, 2. If FIFO page replacement is used by
the operating system and the 3 page frames are initially
empty.

59
Problem 1
(1) FIFO page replacement is used. Please fill in the form which
trace the contents in the page frames when a reference ocurr. The
content in a page frame is the page number 0~4, and "-" represents
the page frame is free.

(2) FIFO page replacement is used. Please caculate the page fault
rate.
60
Problem 1
(3) LRU page replacement is used. Please fill in the form which
trace the contents in the page frames when a reference ocurr.

(4) LRU page replacement is used. Please caculate the page fault
rate.

61
62
Assignments
◆ Assignments
➢ Textbook, p254-259, 4, 20, 28, 36, 45.
➢ For each exercise, copy the question and then answer it.
➢ Send your assignments to yongchuan@hbut.edu.cn
before October 22.

63
64

You might also like