Vous êtes sur la page 1sur 75

CHAPTER FOUR

MEMORY MANAGEMENT

Basics of memory Management


Swapping
Virtual memory
Page replacement

Segmentation

Basics of memory management


Program must be brought into memory
and placed within a process for it to be run
Input queue collection of processes on
the disk that are waiting to be brought into
memory to run the program
Subdividing memory to accommodate
multiple processes
Memory needs to be allocated to ensure a
reasonable supply of ready processes to
consume available processor time
2

Basics of memory management


Memory management is the task carried out by
the OS and hardware to accommodate multiple
processes in main memory
If only a few processes can be kept in main
memory, then much of the time all processes will
be waiting for I/O and the CPU will be idle
Hence, memory needs to be allocated efficiently
in order to pack as many processes into memory
as possible
3

Basics of memory management


Memory Management Requirements

Relocation
Programmer does not know where the
program will be placed in memory when it is
executed
While the program is executing, it may be
swapped to disk and returned to main
memory at a different location (relocated)
Memory references must be translated in the
code to actual physical memory address
4

Basics of memory management


Memory Management Requirements

Relocation
binding of instructions and data to memory
addresses can be done at any step along the
way:
Compile time
Requires recompiling for relocation

Load time
Requires reloading for relocation

Execution time
Special hardware must be used
Commonly used in general purpose operating systems
5

Basics of memory management


Memory Management Requirements

Protection
Processes should not be able to reference
memory locations in another process without
permission
Impossible to check absolute addresses at
compile time Must be checked at run time
Memory protection requirement must be
satisfied by the processor (hardware) rather
than the operating system (software)
Operating system cannot anticipate all of the
memory references a program will make

Basics of memory management


Memory Management Requirements

Protection
We first need to make sure that each process has a
separate memory space
need the ability to determine the range of legal addresses
that the process may access
Memory Address Space
and ensure that the process can access only these legal
addresses

provide this protection by using two registers, usually


a base and a limit
base register holds the smallest legal physical memory
address
limit register specifies the size of the range
7

Basics of memory management


Memory Management Requirements

Sharing
Allow several processes to access the same
portion of memory
If a number of processes are executing the
same program, it is advantageous to allow
each process access to the same copy of the
program rather than have their own separate
copy
Processes that are cooperating on some task
may need to share access to the same data
structure
8

Basics of memory management


Memory Management Requirements

Logical Organization
Main memory in a computer system is organized as a
linear, or one-dimensional, address space, consisting
of a sequence of bytes or words
But programs are written in modules
If HW and OS can deal with user programs and data
in the form of modules of some sort, then a number of
advantages can be realized
Modules can be written and compiled independently
Different degrees of protection given to modules (read-only,
execute-only)
Share modules among processes

An address generated by the CPU is commonly


referred to as a logical address

Basics of memory management


Memory Management Requirements

Logical Organization
The actual address loaded into the memory-address
register of the memory is known as a physical address
The set of all logical addresses generated by a program
is a logical address space
the set of all physical addresses corresponding to these
logical addresses is a physical address space.
The run-time mapping from virtual to physical addresses
is done by a hardware device called the memorymanagement unit (MMU).
The base register is now called a relocation register.
The value in the relocation register is added to every
address generated by a user process at the time it is sent
10
to memory

Basics of memory management


Memory Management Requirements
Physical Organization
Secondary memory of large capacity can be provided
for long-term storage of programs and data, while a
smaller main memory holds programs and data
currently in use
Memory available for a program plus its data may be
insufficient
Overlaying allows various modules to be assigned the
same region of memory with a main program
responsible for switching the modules in and out as
needed
Overlaying wastes programmers time
Programmer does not know how much space will
be available
11
moving information between these two levels of
memory is a major concern of memory management

Basics of memory management


Memory management systems can be
divided into two classes:
Those that move processes back and forth
between main memory and disk during
execution (swapping and paging)
Those that do not sufficient main memory
to hold all the programs at once

Two simple memory management


schemes
Monoprogramming
Multiprogramming

12

Basics of memory management


Multiprogramming
Although the following simple memory
management techniques are not used in
modern OS, they lay the ground for a
proper discussion of virtual memory
fixed partitioning
dynamic partitioning
simple paging
simple segmentation
13

Basics of memory management


Equal-sized fixed partitions
Any program, no matter how small, occupies an
entire partition.
If there is an available partition, a process can
be loaded into that partition
because all partitions are of equal size, it does not matter
which partition is used

If all partitions are occupied by blocked


processes, choose one process to swap out to
make room for the new process
inefficient use of Main memory.
This is called internal fragmentation.

14

Basics of memory management


Unequal-sized fixed partitions
Any process whose size is less than or equal to
a partition size can be loaded into the partition
If all partitions are occupied, the operating
system can swap a process out of a partition
Two types:
Using multiple queue
Using single queue

Multiple queue
When a job arrives, it can be put into the input queue
for the smallest partition large enough to hold it
Problem
the queue for a large partition is empty but the
15
queue for a small partition is full

Basics of memory management


Unequal-sized fixed partitions
Single queue
maintain a single queue and whenever a partition
becomes free, the job closest to the front of the queue
that fits in it could be loaded into the empty partition
and run
Problem
waste a large partition on a small job

Solution
search the whole input queue whenever a partition
becomes free and pick the largest job that fits
Problem
discriminates against small jobs as being unworthy
of having a whole partition, whereas usually it is
desirable to give the smallest jobs (often interactive
jobs) the best service, not the worst.
16

Basics of memory management


Unequal-sized fixed partitions
Single queue
Ways to allow small jobs
To have at least one small partition around.
Such a partition will allow small jobs to run
without having to allocate a large partition for
them

Another approach is to have a rule stating


that a job that is eligible to run may not be
skipped over more than k times.
Each time it is skipped over, it gets one point.
When it has acquired k points, it may not be
skipped again.

17

Basics of memory management


Unequal-sized fixed partitions

18

Basics of memory management


Dynamic Partitioning
Partitions are of variable length and number
Each process is allocated exactly as much
memory as it requires
Eventually holes are formed in main memory.
This is called external fragmentation

Must use compaction to shift processes so


they are contiguous and all free memory is in
one block
Used in IBMs OS/MVT (Multiprogramming with
a Variable number of Tasks)
19

Basics of memory management


Dynamic Partitioning

A hole of 64K is left after loading 3 processes: not


enough room for another process
Eventually each process is blocked.
The OS swaps out process 2 to bring in process 4

20

Basics of memory management


Dynamic Partitioning

Another hole of 96K is created


Eventually each process is blocked.
The OS swaps out process 1 to bring in again
process 2 and another hole of 96K is created...
Compaction would produce a single hole of 256K

21

Basics of memory management


Dynamic Partitioning
Used to decide which free block to allocate to a
process
Goal: to reduce usage of compaction
Possible algorithms:
Best-fit: choose smallest hole
First-fit: choose first hole from beginning
Next-fit: choose first hole from last
placement
Worst-fit: Choose the largest available hole
22

Swapping
Two general approaches to memory management
can be used
Swapping: consists of bringing in each process in its
entirety, running it for a while, then putting it back on the
disk.
Virtual memory: allows programs to run even when
they are only partially in main memory
Concerns:
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

However, a problem occurs whenever a process tries to


grow
23

Swapping
When a memory grows:
If a hole is adjacent to the process, it can be
allocated and the process allowed to grow into the
hole
If the process is adjacent to another process
move the process to a hole in memory large enough
for it
one or more processes will have to be swapped out
to create a large enough hole

If it is expected that most processes will grow as


they run, it is probably a good idea to allocate a
little extra memory whenever a process is swapped
in or moved
when swapping processes to disk, only the
memory actually in use should be swapped
24

Swapping

25

Swapping
Reasons for swapping in and out
Time quantum expired
When a quantum expires, the memory
manager will start to swap out the process that
just finished and to swap another process into
the memory space that has been freed

Higher priority process arrives


If a higher-priority process arrives and wants
service, the memory manager can swap out the
lower-priority process and then load and
execute the higher-priority process
26

Swapping
Requirements for swapping
Address binding
If binding is done at assembly or load time, then the
process cannot be easily moved to a different location
a process that is swapped out will be swapped back
into the same memory space it occupied previously

backing store
Secondary storage must be large enough to
accommodate copies of all memory images for all
users, and it must provide direct access to these
memory images

Swapping time
The memory block with smallest swapping time is
swapped out

Idle

27

A process should be completely idle, to be swapped out

CHAPTER FOUR
MEMORY MANAGEMENT

Basics of memory Management


Swapping
Virtual memory
Page replacement

Segmentation

28

Virtual Memory
Programs are usually too big to fit in the available
memory
virtual memory is a mechanism in which the
combined size of the program, data, and stack
may exceed the amount of physical memory
available for it.
The operating system keeps those parts of the
program currently in use in main memory, and the
rest on the disk
Virtual memory can also work in a
multiprogramming system
Most virtual memory systems use a technique
called paging
29

Virtual Memory

A process executes only in main memory,


that memory is referred to as real
memory or physical memory
A programmer or user perceives a
potentially much larger memory that
which is allocated on disk referred to
as virtual memory
example a user assumes 64K memory,
but actually we have only 32K memory
30

Paging
Both unequal fixed-size and variable-size
partitions are inefficient in the use of memory
Fixed-sized Internal fragmentation
Dynamic External fragmentation

Main memory is partitioned into equal relatively


small fixed-size chunks known as frames or page
frames.
Each process is also divided into small fixed-size
chunks of the same size known as pages.
Pages could be assigned to available chunks of
memory
31

Paging

32

Paging

33

Paging
At a given point in time, a list of free frames
is maintained by the operating system
Does the unavailability of sufficient
contiguous memory space prevent the
operating system from loading a process?
No different pages can be stored on different
frames
Memory address used by programs are mapped
into real memory address called physical address
These program-generated addresses are called
virtual addresses and form the virtual address
space
34

Paging
If the processor encounters a virtual address that
is not in main memory, it generates an interrupt
indicating a memory access fault, known as page
fault.
The operating system puts the interrupted
process in a blocking state and takes control
The operating system maintains a page table
which shows a frame location for each page of the
process.
Each process will have its own page table
Consider a computer with 16 bit (0-64K) addressing, 32
KB of physical memory and 4K frame size
64 KB of virtual address space
16 virtual pages
8 page frames

35

Paging

36

Paging

When the program tries to access address


0, for example, using the instruction
MOV REG, 0
virtual address 0 falls in page 0 (0 to 4095),
which according to its mapping is page
frame 2 (8192 to 12287) physical address
Similarly,
MOV REG,8192 (p2) MOV REG,24576 (p6)
MOV REG,32780 page foult trap

37

Reading assignment

The internal operation of


the MMU

Virtual Memory

39

CHAPTER FOUR
MEMORY MANAGEMENT
Basics of memory Management
Swapping

Virtual memory

Page replacement
Segmentation
40

Page Fetch Policy


The Fetch policy determines when a page should
be brought into main memory
Demand Paging: a page is brought into main
memory only when a reference is made to a
location on that page
When a process is first started, there will be a flurry of
page faults
after a time, matters should settle down and the number
of page faults should drop to a very low level

Prepaging: pages other than the one demanded


by a page fault are brought in
ineffective if most of the extra pages that are brought in
are not referenced
41

Page Replacement Policy


Replacement policy deals with the selection of a
page in main memory to be replaced when a new
page must be brought in
Its sometimes difficult to explain because several
interrelated concepts are involved:
How many page frames are to be allocated to each
active process
Whether the set of pages to be considered for
replacement should be limited to those of the process
that caused the page fault or encompass all the page
frames in main memory
Among the set of pages considered, which particular
page should be selected for replacement
42

Page Replacement Policy


When a page-fault occurs, the OS has to:
Find the location of the desired page on the disk.
Find a free frame:
If there is a free frame, use it.
If there is no free frame, use a page-replacement algorithm
to select a victim frame.
Write the victim frame to the disk; change the page and
frame tables accordingly.

Read the desired page into the newly freed frame;


change the page and frame tables.
Restart the user process.
43

Page Replacement Algorithm


Pick a random page to evict at each page
fault
probability of removing heavily used page
it will probably have to be brought back in quickly

Other applications of page replacement


algorithms
Cache memory
Web server

44

Page Replacement Algorithm


Optimal
Each page can be labeled with the number
of instructions that will be executed before
that page is first referenced
When a page fault occurs, the page with
the highest label should be removed
Problem
impossible to implement
45

Page Replacement Algorithm


Not Recently Used (NRU)
Most computers with virtual memory have two
status bits associated with each page
R is set whenever the page is referenced (read or
written).
M is set when the page is written to (i.e., modified)

The bits are contained in each page table entry


and these bits must be updated on every memory
reference
When a process is started up, both page bits for all its
pages are set to 0 by the operating system.
Periodically (e.g., on each clock interrupt), the R bit is
cleared, to distinguish pages that have not been
referenced recently from those that have been.

46

Page Replacement Algorithm


Not Recently Used (NRU)
The bits are contained in each page table entry and these
bits must be updated on every memory reference
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 that it is better to remove a
modified page that has not been referenced in at least
one clock tick
47

Page Replacement Algorithm


Not Recently Used (NRU)
Advantages
easy to understand
moderately efficient to implement
gives a performance that, while certainly not
optimal, may be adequate

48

Page Replacement Algorithm


The First-In, First-Out (FIFO)
The operating system maintains a list of all pages
currently in memory,
with the page at the head of the list the oldest one and
the page at the tail the most recent arrival.

On a page fault, the page at the head is removed


and the new page added to the tail of the list
Problem
Not efficient as it may remove frequently used pages

Advantage
Less overhead

49

Page Replacement Algorithm


The Second Chance Page
Modification to FIFO that avoids the problem of
throwing out a heavily used page by inspect the R
bit of the oldest page
looking for an old page that has not been
referenced in the previous clock interval
If all the pages have been referenced, second
chance degenerates into pure FIFO
After getting the oldest page, checks for the R bit
If it is 0, the page is both old and unused, so it is
replaced immediately
If the R bit is 1, the bit is cleared, the page is put onto
the end of the list of pages, and its load time is . . . .
updated as though it had just arrived in memory 50

Page Replacement Algorithm

Arrival
time

51

Page Replacement Algorithm


Suppose that a page fault occurs at time 20.
The oldest page is A, which arrived at time 0,
when the process started.
If A has the R bit cleared, it is evicted from
memory, either by being written to the disk or just
abandoned
On the other hand, if the R bit is set, A is put onto
the end of the list and its load time is reset to
the current time (20). The R bit is also cleared
Problem
Moving pages around the list

52

Page Replacement Algorithm


The Clock
Keep all the page frames on a circular list in the form of a
clock, a hand points to the oldest page
When a page fault occurs, the page being pointed to by
the hand is inspected.
If its R bit is 0, the page is evicted, the new page is
inserted into the clock in its place, and the hand is
advanced one position.
If R is 1, it is cleared and the hand is advanced to the next
page.
This process is repeated until a page is found with R = 0.
Not surprisingly, this algorithm is called clock.
It differs from second chance only in the implementation
53

Page Replacement Algorithm

Clock

54

Page Replacement Algorithm


The Least Recently Used (LRU)
An algorithm based on the observation that
pages that have been heavily used in the last few
instructions will probably be heavily used again in
the next few
when a page fault occurs, throw out the page that
has been unused for the longest time
Necessary to maintain a linked list of all pages in
memory, with the most recently used page at the
front and the least recently used page at the rear
Implementations:
Store a counter, c, with each page and increment
automatically after each instruction
For a machine with n page frames maintain a matrix of
55
n n bits

Page Replacement Algorithm


Whenever page frame k is referenced, the
hardware first sets all the bits of row k to 1, then
sets all the bits of column k to 0.
At any instant, the row whose binary value is
lowest is the least recently used, the row whose
value is next lowest is next least recently used,
and so forth
The workings of this algorithm are given in the
following figure for four page frames and page
references in the order
0123210323
56

Page Replacement Algorithm


0123210323

57

Page Replacement Algorithm


The Working Set
Pages are loaded when requested by the
process, not in advance.
This strategy is called demand paging
because pages are loaded only on demand
All pages of a program may not be used
The set of pages that a process is currently
using is called its working set
A program causing page faults every few
instructions is said to be thrashing
58

Page Replacement Algorithm


The Working Set
Many paging systems try to keep track of each process
working set and make sure that it is in memory before letting
the process run
This approach is called the working set model and reduces
the page fault rate.
Pages which are not in the working set are removed during
page replacement
Two attributes are considered:
Reference bit, R
Age
The working set of a process is the set of pages it has
referenced during the past seconds of virtual time
VT - amount of CPU time a process has actually used since it started
59

Page Replacement Algorithm


The Working Set
Instead of defining the working set as those pages used
during the previous 10 million memory references, we can
define it as the set of pages used during the past 100 msec of
execution time
Each entry contains (at least) two items of information: the
approximate time the page was last used and the R
(Referenced) bit
age = current virtual time time of last use

R bit is examined, if it is 1, the current virtual time is written


into the Time of last use field in the page table
Its clearly in the working set and is not a candidate for removal

If R is 0, the page has not been referenced during the current


clock tick and may be a candidate for removal
compare age with T; and remove it if age greater than T

if R is 0 but the age is less than or equal to , the page is still


60
in the working set remove the oldest page

Page Replacement Algorithm

61

Page Replacement Algorithm


The WSClock Page Replacement Algorithm
An improved algorithm, that is based on the clock
algorithm but also uses the working set information
The data structure needed is a circular list of page
frames, as in the clock algorithm
Each entry contains the Time of last use field from
the basic working set algorithm, as well as the R bit
and the M bit
As with the clock algorithm, at each page fault the
page pointed to by the hand is examined first.
If the R bit is set to 1, the page has been used during the
current tick so it is not an ideal candidate to remove.
The R bit is then set to 0, the hand advanced to the next
62
page, and the algorithm repeated for that page.

Page Replacement Algorithm


A case when R=1

63

Page Replacement Algorithm

64

Page Replacement Algorithm

CHAPTER FOUR
MEMORY MANAGEMENT
Basics of memory Management
Swapping

Virtual memory
Page replacement

Segmentation
66

Segmentation
The virtual memory is one-dimensional because the virtual
addresses go from 0 to some maximum address, one address
after another.
For many problems, having two or more separate virtual
address spaces may be much better than having only one
These completely independent address spaces are called
segments
A user program can be subdivided using segmentation, in
which the program and its associated data are divided into a
number of segments
Different segments may have different lengths
A logical address using segmentation consists of two parts, in
this case a segment number and an offset
67

Segmentation

68

Segmentation
Segmentation is similar to dynamic
partitioning in the use of unequal-size
segments
with segmentation a program may occupy more
than one partition, and these partitions need not
be contiguous
With dynamic partitioning, a program occupies
contiguous memory partitions
Segmentation eliminates internal fragmentation
Segmentation and dynamic partitioning suffers
from external fragmentation
69

Segmentation
Whereas paging is invisible to the
programmer, segmentation is usually visible
and is provided as a convenience for
organizing programs and data
Analogous to paging, a simple segmentation
scheme would make use of a segment table for
each process and a list of free blocks of main
memory
Each segment table entry would have to give the
starting address in main memory of the corresponding
segment
length of the segment, to assure that invalid
70
addresses are not used

Segmentation

71

Segmentation
Consider an address of n X m bits, where the
leftmost n bits are the segment number and the
rightmost m bits are the offset, in the previous
figure, n= 4 and m=12.
Thus the maximum segment size is 212 = 4096.
The following steps are needed for address
translation:
Extract the segment number as the leftmost n bits of
the logical address.
Use the segment number as an index into the process
segment table to find the starting physical address of
the segment.
Compare the offset, expressed in the rightmost m bits,
to the length of the segment. If the offset is greater than
or equal to the length, the address is invalid.
The desired physical address is the sum of the starting
72
physical address of the segment plus the offset.

Segmentation
In our example, we have the logical address
0001001011110000, which is segment number 1,
offset 752.
Suppose that this segment is residing in main
memory starting at physical address
0010000000100000.
Then the physical address is
0010000000100000+001011110000=0010001100010000

To summarize, with simple segmentation, a process


is divided into a number of segments that need not
be of equal size.
When a process is brought in, all of its segments
are loaded into available regions of memory, and a
segment table is set up.
73

Segmentation
Advantages:
simplifying the handling of data structures that
are growing or shrinking
linking up of procedures compiled separately on
different segments is simplified
changing one procedures size doesnt affect the
starting address of others
facilitates sharing procedures or data between
several processes
E.g. shared library

different segments can have different kinds of


protection.
74

Segmentation

75

Vous aimerez peut-être aussi