Lab 3: Memory Management

Due: Nov 19th at 5PM

Memory Management

The goal of this lab is to write a simple memory management simulator based on the topics covered in class. You must write a memory manager that supports both segmentation and paged memory allocation. For simplicity, assume that processes do not grow or shrink, and that no compaction is performed by the memory manager.

  1. Segmentation: Write a segmentation based allocator which allocates three segments for each process: text, data, and heap. The memory region within each segment must be contiguous, but the three segments do not need to be placed contiguously. Instead, you should use either a best fit, first fit, or worst fit memory allocation policy to find a free region for each segment. The policy choice is yours, but you must explain why you picked that policy in your README. When a segment is allocated within a hole, if the remaining space is less than 16 bytes then the segment should be allocated the full hole. This will cause some internal fragmentation but prevents the memory allocator from having to track very small holes.

    You should consider the following issues while designing your memory manager:

  2. Paging: Next write a paging based memory allocator. This should split the system memory into a set of fixed size 32 byte pages, and then allocate pages to each process based on the amount of memory it needs. Paging does not require free block coalescing, but you should explain in your README your choice of algorithm and data structure for tracking free pages.

  3. Fragmentation: your code should track the level of internal fragmentation for either memory allocator. You should also track the number of process allocations which fail due to either external fragmentation or insufficient free memory regardless of fragmentation. You should run the same input file for each of your memory allocators and compare the level of fragmentation in each.

    Answer the following questions in your README file:


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 segmentation if policy==0, paging if policy==1
 
}
 
public int allocate(int bytes, int pid, int text_size, int data_size, int heap_size)
{ // allocate this many bytes to the process with this id
  //   assume that each pid is unique to a process
  // if using the Segmentation allocator: text_size, data_size, and heap_size
  //   are the size of each segment. Verify that:
  //      text_size + data_size + heap_size = bytes
  // if using the paging allocator, simply ignore the segment size variables
  // return 1 if successful
  // return -1 if unsuccessful; print an error indicating
  //   whether there wasn't sufficient memory or whether 
  //   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
  // the output will depend on the memory allocator being used.

  // SEGMENTATION Example: 
  // Memory size = 1024 bytes, allocated bytes = 179, free = 845
  // There are currently 10 holes and 3 active process
  // Hole list:
  // hole 1: start location = 0, size = 202
  // ...
  // Process list:
  // process  id=34, size=95 allocation=95
  //    text start=202, size=25
  //    data start=356, size=16
  //    heap start=587, size=54
  // process id=39, size=55 allocation=65
  // ...
  // Total Internal Fragmentation = 10 bytes
  // Failed allocations (No memory) = 2
  // Failed allocations (External Fragmentation) = 7
  // 
  
  // PAGING Example:
  // Memory size = 1024 bytes, total pages = 32 
  // allocated pages = 6, free pages = 26
  // There are currently 3 active process
  // Free Page list:
  //   2, 6, 7, 8, 9, 10, 11, 12...
  // Process list:
  //  Process id=34, size=95 bytes, number of pages=3
  //    Virt Page 0 -> Phys Page 0  used: 32 bytes
  //    Virt Page 1 -> Phys Page 3  used: 32 bytes
  //    Virt Page 2 -> Phys Page 4  used: 31 bytes
  //  Process id=39, size=55 bytes, number of pages=2
  //    Virt Page 0 -> Phys Page 1  used: 32 bytes
  //    Virt Page 1 -> Phys Page 13  used: 23 bytes
  //  Process id=46, size=29 bytes, number of pages=1
  //    Virt Page 0 -> Phys Page 5  used: 29 bytes
  //
  // Total Internal Fragmentation = 13 bytes
  // Failed allocations (No memory) = 2
  // Failed allocations (External Fragmentation) = 0
  //
}

}

Data structures

Both of your memory managers should maintain a processList that lists all currently active processes, the process Id and size of each process. For the segmentation allocator, you should also track the start location of each segment. For the paging allocator you must track what physical page is mapped to each virtual page within the process, as well as the number of bytes used in each page.

You are free to use any data structures (arrays, linked list, doubly linked list, etc) to implement these lists, but you should explain why you pick the data structure you choose. This decision will also affect the use of search algorithms in the segmentation allocator.

Input file

You program should take input from a 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 text data heap  // allocate so much memory to this process (split into segments if using Segmentation) 
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 80 100 54
A 458 2 300 98 60
A 30  3 15 8 7
D 1
P
A 890 4 200 450 240
D 3
P
A 70 5 55 5 10
D 2
D 5
D 4
P

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.
    Your README should also discuss the questions from the Fragmentation section listed above.
    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. Individual Group Assessment (for students working in groups only)
  5. 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...
  6. 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.

  7. 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