# Homework 5 Solutions – Memory Management

## COP 4610/CGS5765, Introduction to Operating Systems, Fall 2003, Florida State University

¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾

Points: 100 points

Due: Week 14, Tuesday, November 25, 2003

1.       (10 pnts) Problem 4 in the textbook on page 451.

Here is a solution:

a.      2200 byte block

b.      1000 byte block

c.      2200 byte block

d.      2200 byte block

e.      2200 byte block

f.        2200 byte block

2.      (10 pnts) Problem 5 in the textbook on page 451.

The free list is a list of blocks eligible for allocation. The list could be organized so that when it is searched for a particular strategy, the search time is small.

1. For best fit, the free list should be sorted so that the smallest block appears first in the list and the largest block appears last. The best fitting block is the first block encountered in the list that is big enough to accommodate the request.
2. For worst fit, the free list should be sorted so that the largest block appears first in the list and the smallest block appears last. The worst fitting block is the first block encountered in the list since it is the one that will leave the largest remaining block after making the allocation.
3. For first fit, the free list keeps blocks in the order in which they appear in the primary memory --- low-address blocks first and high-address blocks last.
4. For next fit, the list should be ordered as in first fit, but the list should be circular. A pointer to the current head of the list is used to begin the search for a block on each request; when a block is allocated, the pointer is left at that location in the list until the next allocation request occurs.

3.      (10 pnts) Problem 6 in the textbook on page 452.

Here is a solution

1. 0x00456789 in Sa.  0x00600000 + 0x00456789 = 0x00a56789
2. 0x0000089a in Sd.  0x01010000 + 0x0000089a = 0x0101089a
3. 0x00043210 in Sb.  0x00180000 + 0x00043210 = 0x001c3210
4. 0x00010234 in Sc.  0x01000000 + 0x00010234 = 0x01010234 (This address overlaps Sd.)
5. 0x000bcdef in Sa.  0x00600000 + 0x000bcdef = 0x006bcdef
6. 0x01010000 in Sd.  0x01010000 + 0x01010000 = 0x02020000

4.      (10 pnts) Problem 2 in the textbook on pages 506.

Since the virtual address size is the page size times the number of pages, then it is reasonable to assume that 2g+h represents the fact that there are 2g pages or address per page, and 2h addresses per page or pages. Similarly, since the process uses 2 h+k addresses as a set of page frames of the same size, the number of page frames or the page frame size is 2h and the page frame size or the number of page frames allocated to the process is 2k. Since 2h appears in both expressions, and since a page frame must be the same size as a page, we infer that 2h is the page and page frame size, 2g is the number of pages in the virtual address space, and that 2k is the number of page frames allocated to the process.

A virtual address is g+h bits wide.

5.      (10 pnts) Problem 3 in the textbook on page 506.

1KB = 1,024 bytes.

a.       899 is offset 899 in page 0.

b.      23456 is offset 928 in page 22.

c.       0x3F244 is offset 0x244 (580 decimal) in page 0xFC (252 decimal).

d.      0x0017C is offset 0x17C (380 decimal) in page 0.

6.      (15 pnts) Problem 4 in the textbook on page 506 (note that it should be 64-entry page middle directory, instead of 32-entry one).

If you assume a 32-entry page middle directory, your answer will be different.

The size of the page middle directory should be 64 instead of 32 entries.  This gives a template for an address as follows:

bits 0:9 - 10-bit offset

bits 10:17 - 8-bit pte

bits 18:23 - 6-bit pmd

bits 24:31 - 8-bit pgd

1. 0x12345678 ® pgd=0x12; pmd=0x0d; pte=0x15; offset=0x278
2. 0x456789ab ® pgd=0x45; pmd=0x19; pte=0xd2; offset=0x1ab
3. 0xba987654 ® pgd=0xba; pmd=0x26; pte=0x1d; offset=0x254
4. 0x87654321 ® pgd=0x87; pmd=0x19; pte=0x50; offset=0x321

1.      (25 pnts) Problem 11 in the textbook on pages 506-507.

The page reference stream is 3 2 4 3 4 2 2 3 4 5 6 7 7 6 5 4 5 6 7 2 1.

1. Belady’s optimal algorithm incurs 10 faults.
`Frame  3  2  4  3  4  2  2  3  4  5  6  7  7  6  5  4  5  6  7  2  1`
 `0` `3` `3` `3` `3` `3` `3` `3` `3` `3` `5` `5` `5` `5` `5` `5` `5` `5` `5` `7` `7` `7` `1` ` ` `2` `2` `2` `2` `2` `2` `2` `2` `2` `6` `6` `6` `6` `6` `6` `6` `6` `6` `2` `2` `2` ` ` ` ` `4` `4` `4` `4` `4` `4` `4` `4` `4` `7` `7` `7` `7` `4` `4` `4` `4` `4` `1`
` `
1. The LRU algorithm incurs 10 faults.
`Frame  3  2  4  3  4  2  2  3  4  5  6  7  7  6  5  4  5  6  7  2  1`
 `0` `3` `3` `3` `3` `3` `3` `3` `3` `3` `3` `6` `6` `6` `6` `6` `6` `6` `6` `6` `6` `1` `1` ` ` `2` `2` `2` `2` `2` `2` `2` `2` `5` `5` `5` `5` `5` `5` `5` `5` `5` `5` `2` `2` `2` ` ` ` ` `4` `4` `4` `4` `4` `4` `4` `4` `4` `7` `7` `7` `7` `4` `4` `4` `7` `7` `7`
` `
1. The FIFO algorithm incurs 12 faults.
`Frame  3  2  4  3  4  2  2  3  4  5  6  7  7  6  5  4  5  6  7  2  1`
 `0` `3` `3` `3` `3` `3` `3` `3` `3` `3` `5` `5` `5` `5` `5` `5` `4` `4` `4` `7` `6` `6` `1` ` ` `2` `2` `2` `2` `2` `2` `2` `2` `2` `6` `6` `6` `6` `6` `6` `5` `5` `5` `2` `2` `2` ` ` ` ` `4` `4` `4` `4` `4` `4` `4` `4` `4` `7` `7` `7` `7` `7` `7` `6` `7` `7` `1`
` `
1. The working set algorithm with window size 6 incurs 9 faults.
`Frame  3  2  4  3  4  2  2  3  4  5  6  7  7  6  5  4  5  6  7  2  1`
 `0` `3` `3` `3` `3` `3` `3` `3` `3` `3` `3` `3` `3` `3` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `1` `1` ` ` `2` `2` `2` `2` `2` `2` `2` `2` `2` `2` `2` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `2` `2` `2` ` ` ` ` `4` `4` `4` `4` `4` `4` `4` `4` `4` `4` `4` `4` ` ` `4` `4` `4` `4` `4` `4` `3` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `5` `5` `5` `5` `5` `5` `5` `5` `5` `5` `5` `5` `4` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `6` `6` `6` `6` `6` `6` `6` `6` `6` `6` `6` `5` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `7` `7` `7` `7` `7` `7` `7` `7` `7` `7` `alloc` `1` `2` `3` `3` `3` `3` `3` `3` `3` `4` `5` `6` `5` `4` `3` `4` `4` `4` `4` `5` `6`
` `
1. The working set algorithm page frame allocation is shown as the last row in the table above.

2.      (10 pnts) Problem 17 in the textbook on page 507.

Note that this is just one possible scenario and there are many other possible solutions.

Suppose the page replacement algorithm is LRU, the page frame allocation is 3, and the loop is loaded in 2 pages, but every instruction references 2 data pages, e.g., the code has the form:

`  for (...) {`
`      y = f(x);`
`  ---- page boundary ----`
`      y = g(x);`
`  }`

Since the 2 data pages are referenced on every instruction, these pages will not be unloaded, leaving only one page for the instructions. When the process executes the top half of the loop, the page containing the code for the top half of the loop, including y = f(x), is loaded, but when the code for the bottom half of the loop is executed, including y = g(x), the first code page is unloaded and the second is loaded. That is, each time through the loop there will be a missing page fault for the code pages.