Ch3 Memory Management
Ch3 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 P3
P1
6K P2
P4 P5
12K empty
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.
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 a. First fit algorithm allocates from the 10 KB block.
<free> 10 KB b. Best fit algorithm allocates from the 4 KB block.
P2
c. Worst fit algorithm allocates from the 16 KB block.
<free> 16 KB
P3
<free> 4 KB
OS
P1 OS
<free> 20 KB Compaction P1
P2 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 f5
Example 3.2
… …
Word 0
Word 1 Page 0 Word 0
… (P0) Page Table Word 1 Frame 3
Word … (f3)
7 Page Frame Word
Word 8 Page 0 3 7
Word 9 1 (P1) Word 16 Frame 4
1 6
Word 17 (f4)
… 2 4
Word 15 …
Word 16 Page 2 Word 23
Word 17 (P2) …
… …
Word 8 Frame 6
Word 9 (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
ratPT in Registers mat
Access
p<PTLR?
memory
ERROR
NO
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 mat
Access PT entry
in Memory at address PTBR + p Access
p<PTLR?
mat memory
. ERROR
NO
In this approach emat is:
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 in AR
p<PTLR? FOUND?
YES (hit)
rat
NO
NO (miss) Physical address
f d
ERRO Access PT entry Access memory
in Memory at address PTBR + p
mat
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 word at physical address =Segment
base + dS
YES
0 d < limit
Seg. # Limit base Attr.
NO
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.
physical
Segment Limit Base memory
0 1500 1000 0
1 200 5500
2 700 6000 1000
3 2000 3500 s0
2500
3500
Now, check segment table entry for segment 1. s3
The limit for segment 1 is 200. Since 123 < 200,
we carry on. The physical address is calculated s1 5500
as 5500 + 123 = 5623, and the memory word 5700
5623 is accessed. s2 6000
6700
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
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
memory
data-1
0
1000
editor
user-2 2500
ST-2
3500
seg lim base data-1
editor
data-2 0 1500 100
1 200 5500 data-2 5500
5700
data-3 6000
user-3
6700
ST-3
editor seg lim base
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.
s pd 0 pd limit p d PT for
segment S
ST
NO
STBR
+ limit base
ERROR f
+
f d
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:
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-9s#
s# base size shares 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
1) Draw the memory maps after step (iv) and (vi) using first fit, best-fit and worst-fit
allocation techniques, without compaction
2) 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?
given: STBR=1000
STLR=5
Associative Registers access time = 50 nsec
Memory Access time = 500 nsec
ST AR
s# base limit 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
<free> 14 K
P4
12. The following memory map is given for a computer system with variable partitioning
memory management.