Académique Documents
Professionnel Documents
Culture Documents
MEMORY MANAGEMENT
Segmentation
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
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
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
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
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
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
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
12
14
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
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
17
18
20
21
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.
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
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
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
CHAPTER FOUR
MEMORY MANAGEMENT
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
Paging
Both unequal fixed-size and variable-size
partitions are inefficient in the use of memory
Fixed-sized Internal fragmentation
Dynamic External fragmentation
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
37
Reading assignment
Virtual Memory
39
CHAPTER FOUR
MEMORY MANAGEMENT
Basics of memory Management
Swapping
Virtual memory
Page replacement
Segmentation
40
44
46
48
Advantage
Less overhead
49
Arrival
time
51
52
Clock
54
57
61
63
64
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
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
Segmentation
75