Out:Saturday, November 9, 2002
Due:Wednesday, November 20, 2002 Friday, November 22, 2002 11:59 PM
lmalloc_end()
to
interface section. (Previously, it was mentioned elsewhere, but was
inadvertantly left out of the interfaces section.) Updated deadline
to reflect extension.Sometimes, when using a computer that is shared with other users, you
might want to limit the size of your process. If your process allocates
too much memory, then it might adversely affect other users of the system.
Linux and other Unices provide mechanisms for limiting the resources
that a process can use. You can set limits on the size of the process'
stack, the size of its heap, the number of file handles it can have open,
and several other things. To see your current limits, type:
ulimit -a
from a bash prompt. You can set limits for
yourself, as a regular user, and the system administrator can set hard
limits that you must stay under. Take a look at the
man page for ulimit
for more on this.
The trouble is that if your process reaches one if its limits, it might
not be able to continue running. For example, if you limit your heap size
to 16K, and your program tries to allocate 64K with malloc
,
the call to malloc
will just fail and return a NULL pointer.
To see this, type the following (minus the comments) from a bash prompt.
ulimit -S -d 1 # Limit the heap size to 1K (soft limit) man ulimit # Try to start a program. This will fail. ulimit -S -d umlimited # Restore your limit to what it was before
This problem means that you cannot limit your process' heap size and still allow your program to continue if it needs more heap space.
In this assigment, you will solve this problem by writing your own heap allocator. The heap allocator you probably use most often is malloc which is part of the standard C library. On Linux, malloc comes by way of glibc, the GNU implementation of the standard C library. Note that malloc, itself, is not part of the kernel. Your heap allocator will be quite a bit different from malloc. In particular, it will allow a process to specify a limit to its total heap usage without causing the application to terminate if it exceends the limit. It will do this by utilizing some of the techniques used by the virtual memory system to make the process think it has an infinitely large heap.
For background on malloc, I strongly suggest reading this paper by Poul-Henning Kamp about the FreeBSD implementation of malloc. It's only 6 pages, so it should be a quick and fruitful read. For background on the binary buddy strategy for sub-page allocation, take a look at this 1193-word article on allocation.
Your heap allocator, called lmalloc
, will consist of the following interface.
size_of_heap
is given in bytes. Signal that the system should use a particular page
replacement strategy. (Tiny hint: lmalloc_init(..)
will call the standard
malloc(..)
.)lmalloc
allocates a block of memory for the application on the
virtual heap. If there is not enough space on the virtual heap to fulfill the request,
it will use page replacement to pick a block from the virtual heap and page (write) it out to
disk to make room for the allocation request. Also, if the request is at least half
a page (as specified in the lmalloc_init(..) call), it should just allocate a whole page.
If the request is less than half a page, it should use sub-page allocation to fulfill the
request.free
.)lmem_d
does the same thing as lmem, except that it notifies
the lmalloc system that the value has been changed. Thus, your system will need
to set a dirty bit somewhere.This system is not interreplaceable with the standard malloc
. In other words,
you could not just replace malloc
calls with lmalloc
calls
in a working program and expect it to work. It should
allow the application developer to do anything (s)he could do with malloc
but it requires a few special considerations. Theoretically, you could use lmalloc
and standard malloc
in the same program, but that would defeat the purpose of lmalloc
.
lmalloc_init(...)
at the
beginning of their program and lmalloc_end()
at the end of their program.
Example:#include "lmalloc.h"
int main() {
lmalloc_init(32768, 1024, FIFO, "pagefile");
/* Do stuff, manipulate data, have fun */
lmalloc_end();
return 0;
}
lmem
returns. In other words, the application developer
will always refer to memory using lmem
(or lmem_d
) and will use lhandle
s in
place of pointers in any data structures. Thus, a linked list node will contain
data and a next lhandle
.To verify your implementation, you will write a total of five programs that use your library for heap allocation:
fifo
,
lru
, or other
to specify which page replacement strategy to use. You should be
able to run your tests like this.time fifo fifo
time fifo lru
time fifo other
time lru fifo
time lru lru
time lru other
time other fifo
time other lru
time other other
lmalloc
library so that it uses lmalloc
to allocate space for its internal data structures. Warning: This
can be tricky. There are a couple ways to look at this. Let's have
some discussion about this problem.
lmalloc_init(..)
. If need be,
your implementation can use one additional file with a name derived
from the first file (e.g. "pagefile" and "pagefile-helper").
You don't absolutely need two files, but you may find it more convenient**.lmem
should be a O(1) operation when the page is in memory.gcc -c lmalloc.c -o lmalloc.o
.
This makes it so others can use your library without the source code. This
is the way most libraries are done. To see this on any Linux machine,
look in the /usr/lib
directory. You'll find a whole bunch
of object files.**For this project, you will need a partner. Consider working with somebody you don't know very well. This often leads to better productivity. It always leads to making connections with people with similar professional interests who you might not otherwise get to know.
Collaboration can be a great thing in a project because it lets you tackle a bigger problem, learn more, and learn how to express your technical ideas. Being able to clearly express technical ideas is a super-important for all scientists and engineers. Collaboration also solves the usual problem of wanting to discuss your solution with somebody without spoiling somebody else's fun. However, problems can arise. Maybe one person is contributing more than the other. Maybe one person is over-committed and unavailable. Maybe one person over-writes the other's files. Maybe the duo cannot decide on an acceptable solution. Usually these problems are few and far between, but if you have any such trouble, please get in touch with one of the TAs as soon as possible. All we can really do is try to facilitate communication, but usually that's all it takes.
As far as grading is concerned, both members of the duo are playing the same song, so generally we will award one score per performance.
Just merge all of these into one write-up file and turn it in electronically with your code. The following formats are okay: PDF, HTML, text file, Word document, StarOffice/OpenOffice document. The whole thing will probably be between one and two pages.
lmalloc_init, lmalloc, lfree, lmem, lmem_d,
and lmalloc_end
. It should explain
to an application developer how to use your library. It should also explain
to the application developer any constraints he/she should be aware of, such
as a minimum/maximum allocation size, minimum/maximum page size, etc.Turnin is enabled. To turnin your files, assuming they are in a directory called proj3 and you are in section AA, enter...
turnin -ccse451=AA -v -pproject3 proj3
You should have..
lmalloc.o
.Please, turn in only one submission per group. If there is any confusion, please send mail to Alex.
Last updated 11/19/02 by Alex Quinn (aquinn@cs)