CS372: Solutions for Homework 8

Problem 1:

Segmented memory schemes (as well as user-level memory allocators like malloc() and, later in class, some disk placement schemes) use various policies for fitting variable sized allocation units into variable-sized spaces. Concoct a scenario in which Best-fit allocation outperforms First-fit, Worst-fit, and Buddy allocation (see the book for details on these algorithms). Repeat for First-fit v. the others, Worst-fit v. the others, and Buddy allocation v. the others.

Solution:
This solution is after Andreas Reifert, a distinguished student in the class of Fall 1999.
(x) A yK  - means: in step x we allocate yK of memory
(x) D (y) - means: in step x we deallocate the memory allocated in step y

Best fit outperforms:
(1) A 5K
(2) A 8K
(3) A 3K  - buddy cannot do this
(4) D (1)
(5) D (3)
(6) A 3K  - first and worst take the 5K part, best the 3K part
(7) A 5K  - first and worst cannot do this, best can                           #

Worst fit outperforms:
(1) A 3K
(2) A 8K
(3) A 5K  - buddy cannot do this
(4) D (1)
(5) D (3)
(6) A 2K  - first and best take the 3K part, worst the 5K part
(7) A 3K  - first and best take the 5K part, worst a 3K
(8) A 3K  - first and best cannot do this, worst can

First fit outperforms:
 (1) A 4K
 (2) A 2K
 (3) A 2K
 (4) A 3K
 (5) A 5K  - buddy cannot do this
 (6) D (1)
 (7) D (3)
 (8) D (5)
 (9) A 1K  - best takes the 2K part, worst the 5K part, first the 4K part
(10) A 3K  - best takes the 4K part, worst a 4K part, first the 3K part
(11) A 2K  - best takes the 5K part, worst the 4K part, first the 2K part
(12) A 5K  - best and worst cannot do this, first can

Buddy outperforms:
(1) A 2K
(2) A 4K
(3) A 8K
(4) D (1) - only buddy can merge the 2K with the neighbouring 2K to a 4K part
(5) A 4K  - best, worst and first cannot do this, buddy can

Problem 2 (based on reading; not covered in lecture)

Describe the data structures required to implement best-fit memory allocation.

Solution:
Each partition will need the following data structure:

typedef struct
{
    unsigned base_address;         // starting address of the partition
    unsigned size;                &nb\ sp;      // size of the partition in bytes
    enum {free, in_use} status;   // status of the partition
}
PartitionRecord;

Then, we need to implement a data structure that contains the partition records and such that:
* partition records are sorted by size
* partition records can be inserted into the data structure efficiently
* partition records can removed from the data structure efficiently
* the smallest partition larger than a particular size can be located efficiently
* allows a partition to be checked against its adjacent neighbors so that it can be merged with them if they are all free (or a subset thereof)

There is no known data structure that can achieve all these feats simultaneously. There are however two reasonable alternatives:

  1. B-trees (log n search, log n insertion, and log n removal)
  2. A bucket table, where a bucket contains all partition records of partitions that are within a given size range. This case, O(n) search, O(n) insertion, and O(n) deletion, but the expected time would be much less in practice, almost O(1) (unlike B-trees).
 In addition, you will need a doubly-linked list of partitions sorted by size to facilitate merging of free partitions.

Problem 3

This question refers to an architecture using segmentation with paging. In this architecture, the 32-bit virtual address is divided into fields as follows:

4 bit segment number 12 bit page number 16 bit offset

Here are the relevant tables (all values in dexadecimal):

Segment Table Page Table A Page Table B
0Page Table A 0CAFE 0F000
1Page Table B 1DEAD 1D8BF
x(rest invalid) 2BEEF x(rest invalid)
3BA11
x(rest invalid)

Find the physical address corresponding to each of the following virtual addresses (answer "bad virtual address" if the virtual address is invalid):

  1. 00000000
  2. 20022002
  3. 10015555

Solution:
Need solution