Lab 3: Memory Management

Due: Thu April 8, 6pm


Memory Management

The goal of this lab is to write a simple memory manager based on the topics covered in class. This lab has two parts.
  1. (75 pts) Write a memory manager that supports contiguous memory allocation. Implement both first-fit and best-fit policies in your memory manager. For simplicity, assume that processes do not grow or shrink, and that no compaction is performed by the memory manager. You should consider the following issues while designing your memory manager:

  2. (25 pts) Implement compaction in your memory manager. It is sufficient to implement compaction for the best-fit policy. Your memory manager should compact memory every time it sees external fragmentation (this will occur when a new process asks for memory and your memory manager is unable to allocate it due to fragmentation). A simple compaction policy that moves all currently running processes to the start of main memory and creating one hole at the high region of memory will get you full credit.


Getting Started

Unlike Lab 2, this assignment does not require you to use any special (e.g., synchronization) features of Java. Use the following template as a starting point:


class MemoryManager
{

public MemoryManager(int bytes, int policy)
{  // intialize memory with these many bytes.
    // Use first-fit if policy==0, best-fit if policy ==1

}

public int allocate(int bytes, int pid)
{ // allocate these many bytes to the process with this id
  //  assume that each pid is unique to a process
  // return 1 if successful
  // return -1 if unsuccessful; print an error indicating
  // whether there wasn't sufficient memory or whether 
  // there you ran into external fragmentation

}

public int deallocate(int pid)
{ //deallocate memory allocated to this process
  // return 1 if successful, -1 otherwise with an error message

}


public void printMemoryState()
{ // print out current state of memory 
  // Example: 
  // Memory size = 1024 bytes, allocated bytes = 24, free = 1000
  // There are currently 10 holes and 3 active process
  // Hole list:
  // hole 1: start location = 0, size = 202
  // ...
  // Process list:
  // process  id=34, start location=203, size=35
  // ...

}

}

Data structures

Your memory manager class should maintain two lists: a holeList and a processList The holeList is a list of holes, with the start location and size of each hole. The processList is the list of currently active process containing the process Id, the start location and size of each process. You are free to use any data structures (arrays, linked list, doubly linked list, etc) to implement these lists. This decision will also affect the use of search algorithms to search through these lists.

Input file

You program should take input from a input file and perform actions specified in the file, while printing out the result of each action. The format of the input file is as follows:


memorySize policy    //initialize memory to this size and use this policy
A  size pid          // allocate so much memory to this process
D   pid                // deallocate memory for this process
P                        // print current state of memory

An actual file may look as follows

8192 1
A 234 1
A 458 2
A 30  3
D 1
P
A 890 4
D 3
P
A 70 5
D 2
D 5
D 4
P
Sample input files are available: sample1.txt and sample2.txt.

What to submit

All of the following files must be submitted on SPARK as a zip file to get full credit for this assignment.
  1. Your zip file should contain a copy of all source files.
  2. Your zip file should contain a copy of a README file identifying your lab partner (if you have one) and containing an outline of what you did for the assignment. It should also explain and motivate your design choices. Keep it short and to the point, while explaining explain your design decisions, data structures and algorithms.
    If your implementation does not work, you should also document the problems in the README, preferably with your explanation of why it does not work and how you would solve it if you had more time. Of course, you should also comment your code. We can't give you credit for something we don't understand!
  3. Fnally, your zip file should contain a copy showing sample output from your programs.
  4. Important:All files for the first and second parts of this lab should be distinct from one another. Feel free to keep them in separate sub-directories. This will allow us to grade each part separately.
  5. Individual Group Assessment (for students working in groups only)
  6. A percent of your lab grade will come from your participation in this project as a member of your group.
    What you need to turn in (each person individually):
    Include in your zip file a copy of your assessment of the division of labor in the group in the format shown below.  For a 2 person group, if you give yourself a 50% and your partner gives you a 50%, you will get the full credit for group participation.  If you give your partner a 40% and your partner gives himself or herself a 40%, he or she will get fewer points for group participation.  And so on...
  7. Note: We will strictly enforce policies on cheating. Remember that we routinely run similarity checking programs on your solutions to detect cheating. Please make sure you turn in your own work.

    You should be very careful about using code snippets you find on the Internet. In general your code should be your own. It is OK to read tutorials on the web and use these concepts in your assignment. Blind use of code from web is strictly disallowed. Feel free to check with us if you have questions on this policy. And be sure to document any Internet sources/ tutorials you have used to complete the assignment in your README file.

  8. Late Policy: Please refer to the course syllabus for late policy on labs assignments. This late policy will be strictly enforced. Please start early so that you can submit the assignment on time.

Prashant Shenoy
Last modified: Tue Mar 23 10:10:46 EDT 2010