CMSC 421: Principles of Operating Systems

Homework 4: Memory Allocator

This homework is due on Thursday, April 2, at 11:59:59 PM (Eastern daylight time). You must use the submit to turn in your homework like so: submit cs421_jtang hw4 hw4.c mmap.data

Your program must be named hw4.c, and it will be compiled on Ubuntu 14.04 as follows:

  gcc --std=c99 -Wall -O2 -pthread -o hw4 hw4.c
There must not be any compilation warnings in your submission; warnings will result in grading penalties. In addition, your code must be properly indented and have a file header comment, as described on the coding conventions page.

In this homework, you are writing a memory allocator from scratch. You will implement the first-fit algorithm, and as proof that your code works you will also submit a memory-mapped file.

Part 1: Memory Map

Your program takes a single command-line argument, a filename. Your program must do the following:

  1. Open the requsted file for reading and writing. If the file does not exist, create it.
  2. Truncate the file to 256 bytes.
  3. Create a memory-map (via mmap()) to the file for its entire 256 byte contents.
Test the above to ensure correctness. For example, if you were to run your program like so: ./hw4 mmap.data, you can use the xxd command to view the binary contents of the data file after program completion, like so: xxd mmap.data.

Part 2: Memory Allocator

Next, you will implement a first-fit memory allocator. You must implement these functions with the given signature:

/**
 * Allocate and return a contiguous memory block that is within the
 * memory mapped region. The size of the block will be at least as
 * large as @a size.
 *
 * This is a thread-safe function.
 *
 * @param size Number of bytes to allocate. If @c 0, your code may do
 * whatever it wants; malloc() of 0 is "implementation defined",
 * meaning it is up to you if you want to return @c NULL, segfault,
 * whatever.

 * @return Pointer to allocated memory, or @c NULL if no space could
 * be found.
 */
void *my_malloc(size_t size);

/**
 * Deallocate a memory region that was returned by my_malloc(). You
 * may do whatever you want if the passed in pointer was not a pointer
 * returned by my_malloc()
 *
 * This is a thread-safe function.
 *
 * @param ptr Pointer to memory region to free. If @c NULL, do
 * nothing.
 */
void my_free(void *ptr);
  

For my_malloc(), you are to implement a first-fit allocation strategy. Your program has 16-byte page frames; thus my_malloc() will round up to the next 16-byte boundary when reserving space. There are a total of 16 frames, so the total allocatable space is 256 bytes. Your function allocates using the space returned by the memory map from step 1 above.

The my_free() function deallocates space that was returned by my_malloc(). Thus, if my_malloc() allocated 5 frames and returned the address to the first frame, calling my_free() will deallocate all 5 frames (not just the first).

Part 3: Threading

The final step is to ensure my_malloc() and my_free() are thread-safe. If a thread is in the middle of my_malloc()/my_free(), another thread calling my_malloc()/my_free() must block.

To test your implementation, your program must spawn three threads simultaneously (or as simultaneously as possible). Those three threads must run the following code:

/* thread 1 */
void *m1, *m2;
m1 = my_malloc(30);
memset(m1, 'A', 30);
sleep(1);
m2 = my_malloc(50);
memset(m2, 'B', 50);
sleep(2);
my_free(m1);

/* thread 2 */
void *m1, *m2;
m1 = my_malloc(64);
memset(m1, 'C', 64);
sleep(3);
my_free(m1);
sleep(1);
m1 = my_malloc(1);
memset(m1, 'D', 1);
m2 = my_malloc(80);
memset(m2, 'E', 80);

/* thread 3 */
void *m1, *m2;
sleep(2);
m1 = my_malloc(128);
if (!m1) {
    m2 = my_malloc(95);
    memset(m2, 'F', 95);
}
my_free(m1);
  

In your main(), run the above three threads and wait for them to complete (via pthread_join()). If implemented correctly, your program ought not segfault.

Submitting Work

Once your code is finished, run it with the command-line argument mmap.data. You must submit both the original code file and this resulting memory map file.

Other Hints and Notes

Extra Credit

Sorry, there is no extra credit available for this assignment.