11 Notes On Memory Management
11 Notes On Memory Management
11 Notes On Memory Management
3 Memory Management
Chapter 3
Memory Management
In order to be able to load programs at anywhere in memory, the compiler must generate
relocatable object code. Also we must make it sure that a program in memory, addresses
only its own area, and no other program’s area. Therefore, some protection mechanism is
also needed.
This is a version of fixed partitioning that uses RRS with some time quantum. When time
quantum for a process expires, it is swapped out of memory to disk and the next process in
the corresponding process queue is swapped into the memory.
OS
2K P1 P3
P2
6K
P4 P5
empty
12K empty
Normally, a process swapped out will eventually be swapped back into the same partition.
But this restriction can be relaxed with dynamic relocation.
In some cases, a process executing may request more memory than its partition size. Say
we have a 6 KB process running in 6 KB partition and it now requires a more memory of 1
KB. Then, the following policies are possible:
• Return control to the user program. Let the program decide either quit or modify its
operation so that it can run (possibly slow) in less space.
• Abort the process. (The user states the maximum amount of memory that the process
will need, so it is the user’s responsibility to stick to that limit)
• If dynamic relocation is being used, swap the process out to the next largest PQ and
locate into that partition when its turn comes.
The main problem with the fixed partitioning method is how to determine the number of
partitions, and how to determine their sizes.
If a whole partition is currently not being used, then it is called an external fragmentation.
And if a partition is being used by a process requiring some memory smaller than the
partition size, then it is called an internal fragmentation.
OS
In this composition of memory, if a
2K P1 (2K)
new process, P3, requiring 8 KB of
6K Empty (6k) External memory comes, although there is
fragmentation
enough total space in memory, it can
not be loaded because fragmentation.
12K P2 (9K)
Internal
Empty (3K) fragmentation
In the variable partitioning method, we keep a table (linked list) indicating used/free areas in
memory. Initially, the whole memory is free and it is considered as one large block. When a
new process arrives, the OS searches for a block of free memory large enough for that
process. We keep the rest available (free) for the future processes. If a block becomes free,
then the OS tries to merge it with its neighbors if they are also free.
There are three algorithms for searching the list of free blocks for a specific amount of
memory.
First Fit : Allocate the first free block that is large enough for the new process. This is a fast
algorithm.
Best Fit : Allocate the smallest block among those that are large enough for the new
process. In this method, the OS has to search the entire list, or it can keep it sorted and stop
when it hits an entry which has a size larger than the size of new process. This algorithm
produces the smallest left over block. However, it requires more time for searching all the list
or sorting it.
Worst Fit : Allocate the largest block among those that are large enough for the new
process. Again a search of the entire list or sorting it is needed. This algorithm produces the
largest over block.
Example 3.1
Consider the following memory map and assume a new process P4 comes with a memory
requirement of 3 KB. Locate this process.
OS
P1
<free> 10 KB
a. First fit algorithm allocates from the 10 KB block.
P2 b. Best fit algorithm allocates from the 4 KB block.
<free> 16 KB c. Worst fit algorithm allocates from the 16 KB block.
P3
<free> 4 KB
OS OS OS
P1 P1 P1
P4 <free> 10 KB <free> 10 KB
<free> 7 KB P2 P2
P2 <free> 16 KB P4
<free> 16 KB P3 <free> 13 KB
P3 P4 P3
<free> 4 KB <free> 1 KB <free> 4 KB
First Fit Best Fit Worst Fit
At this point, if a new process, P5 of 14K arrives, then it would wait if we used worst fit
algorithm, whereas it would be located in cases of the others.
OS
P1 OS
<free> 20 KB P1
P2 Compaction P2
<free> 7 KB P3
P3 <free> 37 KB
<free> 10 KB
3.3 Paging
Paging permits a program to allocate noncontiguous blocks of memory. The OS divide
programs into pages which are blocks of small and fixed size. Then, it divides the physical
memory into frames which are blocks of size equal to page size. The OS uses a page table
to map program pages to memory frames. Page size (S) is defined by the hardware.
Generally page size is chosen as a power of 2 such as 512 words/page or 4096 words/page
etc.
With this arrangement, the words in the program have an address called as logical address.
Every logical address is formed of
When a logical address <p, d> is generated by the processor, first the frame number f
corresponding to page p is determined by using the page table and then the physical
address is calculated as (f*S+d) and the memory is accessed.
P3 Page Table P1 f3
P0 f4
P3 f5
p d f d
Example 3.2
Word 0 … …
Word 1 Page 0 Word 0
… (P0) Page Table Word 1 Frame 3
Word 7 … (f3)
Word 8 Page Frame Word 7
Word 9 Page 1 0 3 Word 16
… (P1) 1 6 Word 17 Frame 4
Word 15 2 4 … (f4)
Word 16 Word 23
Word 17 Page 2
… (P2) … …
Word 23
Word 8
Word 9 Frame 6
… (f6)
Word 15
… …
Every access to memory should go through the page table. Therefore, it must be
implemented in an efficient way.
Keep page table in fast dedicated registers. Only the OS is able to modify these registers.
However, if the page table is large, this method becomes very expensive since requires too
many registers.
Logical address
PTLR: Page Table Length Register
p d
Physical address
f d
YES Access PT Access
p<PTLR? in Registers memory
rat mat
NO
ERROR
Given a logical address to access the word in physical memory, first access the PT stored in
registers, which requires register access time (rat), and then find out the physical address
and access the physical memory, which requires memory access time (mat). Therefore
effective memory access time (emat) becomes:
In this method, the OS keeps a page table in the memory. But this is a time consuming
method. Because for every logical memory reference, two memory accesses are required:
1. To access the page table in the memory, in order to find the corresponding frame number.
2. To access the memory word in that frame
Logical address
PTBR: Page Table Base Register
p d PTLR: Page Table Length Register
Physical address
f d
Access PT Access
p<PTLR? entry memory
in Memory
at address mat
PTBR + p
NO
mat
. ERROR
emat= 2 * mat
These are small, high speed registers built in a special way so that they permit an
associative search over their contents. That is, all registers may be searched in one machine
cycle simultaneously. However, associative registers are quite expensive. So, a small
number of them should be used.
Logical address
PTBR: Page Table Base Register
p d PTLR: Page Table Length Register
Physical address
f d
YES Search PT
p<PTLR? in AR FOUND?
YES (hit)
rat
NO
NO (miss) Physical address
f d
ERRO Access PT Access
entry memory
in Memory
at address mat
PTBR + p
mat
When a logical memory reference is made, first the corresponding page number is searched
in associative registers. If that page number is found in one associative register (hit) then the
corresponding frame number is get, else (miss) the page table in memory is accessed to find
the frame number and that <page number, frame number> pair is stored into associative
registers. Once the frame number is obtained, the memory word is accessed.
The hit ratio is defined as the percentage of times that a page number is found in associative
registers. Hit ratio is important in performance of the system since it affects the effective
memory access time. In the case of finding the page number in associative registers, only
one memory access time is required whereas if it cannot be found two memory accesses are
needed. So, greater the hit ratio, smaller the effective memory access time. Effective
memory access time is calculated as fallows:
where
Example 3.3
Assume we have a paging system which uses associative registers. These associative
registers have an access time of 30 ns, and the memory access time is 470 ns. The system
has a hit ratio of 90 %.
Now, if the page number is found in one of the associative registers, then the effective
access time:
Because one access to associative registers and one access to the main memory is
sufficient.
On the other hand, if the page number is not found in associative registers, then the effective
access time:
ematMISS = 30 + (2 * 470) = 970 ns.
Since one access to associative registers and two accesses to the main memory iare
required.
Sharing Pages
Example 3.4
Consider a system having page size=30 MB. There are 3 users executing an editor program
which is 90 MB (3 pages) in size, with a 30 MB (1 page) data space.
To support these 3 users, the OS must allocate 3 * (90+30) = 360 MB space. However, if the
editor program is reentrant (non-self-modifying code = read only), then it can be shared
among the users, and only one copy of the editor program is sufficient. Therefore, only 90 +
30 * 3 = 180 MB of memory space is enough for this case.
3.2 Segmentation
In segmentation, programs are divided into variable size segments, instead of fixed size
pages. Every logical address is formed of a segment name and an offset within that
segment. In practice, segments are numbered. Programs are segmented automatically by
the compiler or assembler.
Main
Func 1 Func 2
Data 1
Data 1
Data 3
For logical to physical address mapping, a segment table is used. When a logical address
<s, d> is generated by the processor:
1. Base and limit values corresponding to segment s are determined using the
segment table
2. The OS checks whether d is in the limit. (0 ≤ d < limit)
3. If so, then the physical address is calculated as (base + d), and the memory is
accessed.
logical address
s d
base
d
acess the Segment S
YES word at
0 ≤ d < limit physical
Seg. # Limit base Attr. address =
NO base + d
ERROR
Example 3.5
Generate the memory map according to the given segment table. Assume the generated
logical address is <1,123>; find the corresponding physical address.
Segment tables are also implemented in the main memory or in associative registers, in the
same way it is done for page tables.
Sharing Segments
Also sharing of segments is applicable as in paging. Shared segments should be read only
and should be assigned the same segment number.
Example 3.6:
Consider a system in which 3 users executing an editor program which is 1500 KB in size,
each having their own data space.
user-1 ST-1
seg lim base
editor 0 1500 1000
1 2000 3500 physical
data-1 memory
0
1000
editor
user-2 2500
ST-2
3500
seg lim base
editor data-1
0 1500 100
1 200 5500
data-2 5500
data-2 5700
data-3 6000
6700
user-3 ST-3
seg lim base
editor 0 1500 100
1 700 6000
data-3
There is a separate PT for every segment. On the average, now there is half a page of
internal fragmentation per segment. However, more table space is needed. In the worst
case, again three memory accesses are needed for each memory reference.
The flowchart for accessing a word with logical address <s,p,d> is shown below.
0 ≤ pd ≤ limit PT for
s pd p d segment S
ST
STBR NO
+
ERROR f
limit base
+
f d
d
f
QUESTIONS
1. Why do we need memory management and CPU scheduling algorithms for a multiuser
system ? Can we do without these algorithms? Explain briefly.
b. List the memory management methods discussed, and indicate the types of fragmentation
caused by each method.
3. Consider a multiuser computer system which uses paging. The system has four
associative registers. the content of these registers and page table for user_12 are given
below:
PTLR[12]:5 PTBR[12]:50000
PAGE SIZE :1024 words
For the following logical addresses generated by user_12's program, calculate the physical
addresses, explain how those physical addresses are found, and state the number of
physical memory accesses needed to find the corresponding word in memory. If a given
logical address is invalid, explain the reason.
4. The following memory map is given for a computer system with variable partitioning
memory management.
free (30K)
Find and draw the resulting memory maps, after each step of the above job sequence is
processed, for :
5. Consider a computer system which uses paging. Assume that the system also has
associative registers.
c. With the above associative register and memory access times, calculate the minimum hit
ratio to give an effective memory access time less than 320 nanoseconds.
6. A system which utilizes segmentation gives you the ability to share segments. If the
segment numbers are fixed, then a shared segment table is needed, and the ST must be
modified. We shall assume that the system is capable of dynamic relocation, but to reduce
the overhead, we want to avoid it unless it is absolutely necessary. The following example is
given for such a system :
ST-6 ST-9
s# base size shares s# base size shares
0 - - 256 0 190 100 -
1 0 100 - 1 - - 256
2 100 90 - 2 290 10 -
3 600 15 -
SST
s# base size no. of sh.
256 400 200 2
Assume maximum number of segments per process is 256, and segments are numbered
starting with 0.
a. What would be done when a segment previously unshared, becomes a shared segment?
7. In the X-2700 computer, logical addresses are 24 bits long. The machine implements
paged segmentation with a maximum segment size of 64K words and 4K-word pages:
a. Show the logical address structure indicating the segment, page and displacement bits.
c. If a process has to be fully loaded into memory to execute, what is the minimum physical
memory capacity?
d. If the memory unit contains 65536 frames, show the physical address structure.
e. Show the functional block structure of a suitable architecture for implementing paged
segmentation in the X-2700. Indicate the sizes of all necessary tables.
8. Given the memory map in the figure, where areas not shaded indicate free regions,
assume that the following events occur:
P4
a. Draw the memory maps after step (iv) and (vi) using first fit, best-fit and worst-fit allocation
techniques, without compaction
b. Draw the same memory maps as in part (a) if compaction is performed whenever
required. Also show the maps after each compaction.
9. In a paging system , the logical address is formed of 20 bits. the most significant 8 bits
denote the page number, the least significant 12 bits denote the offset. Memory size is 256K
bits.
d. Give the structure of the page table of a process executing in this system. Assume 2 bits
are reserved for attributes.
e. How many bits are required for each page table entry?
f. If physical memory is upgraded to 1024 K bits, how will your answer to c and e change?
STBR=1000
STLR=5
Associative Registers access time = 50 nsec
Memory Access time = 500 nsec
ST AR
s# base limit s# base limit
0 10000 1000 0 10000 1000
1 12000 2000 1 12000 2000
2 25000 4000
3 15000 8000
4 38000 4000
Assume data can be written into associative registers in parallel with a memory read or write
operation. For replacement of data in associative registers, LRU policy is used.
For each of the following logical addresses, find the corresponding physical address to be
accessed, and the total execution time required for that access, including the time spent for
address translation operations. Also indicate which memory locations are accessed during
address translation. Clearly show all changes made in associative registers.
11.
P1 Consider the memory map given in the figure. If worst fit
policy is to be applied, then what will be the memory map
<free> 9K after arrival of the processes
P2
P5=3K, P6=5K, P7=7K P8=6K.
<free> 20 K
Indicate if compaction is needed.
P3
<free> 14 K
P4
12. The following memory map is given for a computer system with variable partitioning
memory management.