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.cThere 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.
Your program takes a single command-line argument, a filename. Your program must do the following:
mmap()
) to the file for
its entire 256 byte contents.
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).
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.
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.
malloc()
(or any of its ilk) or
free()
. Using any built-in memory allocator
will result in a zero for this assignment.
my_malloc()
nor my_free()
.
open()
to open the file. Read its man page to
figure out how to automatically create the file, if it does not
already exist.
my_malloc()
, it may be useful to
see which thread is calling the function. As intended, there is
no convenient way to pass a thread ID
into my_malloc()
. This is where thread-local
storage is useful.
my_free()
frees them all.
ALIGN
macro.
Sorry, there is no extra credit available for this assignment.