



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




START EARLY You will not finish if you start the night before this project is due.
You will submit your solution via your subversion repository and demonstrate it in lab section. I will give you more info on how and what to submit.
When we talk about executing code on a computer we make a distinction between application code (user-level code), and OS code (kernel-code). With the exception of this project, most of or work will be to extend the kernel code of the Pintos operating system. However, for this project will look at some systems programming issues at the user-level. Pintos, despite being a state-of-the-art OS in most respects, currently lacks a user-level memory allocator. Programs running on Pintos cannot make use of malloc() and free() (the C equivalents of C++’s new and delete), which obviously is a sad state of affairs. In this project, you will implement a user-level memory allocator for Pintos. Along the way, you will learn about user-level memory allocation strategies and you’ll learn how to use some of Pintos’s code. A memory allocator divvys up a large, continuous piece of memory into smaller pieces, called blocks. Blocks can be of different sizes. To allocate memory, an allocator needs to find a free block of sufficient size. If memory is deallocated (freed), the block in which the memory was contained is returned to the pool of free memory. You will develop your solutions within the Pintos source tree. You can download a copy of pintos from:
http://www.cs.usfca.edu/benson/cs326/prj0/src/pintos.tar.gz
You should use a linked list to keep track of free blocks. The free list should be sorted by address. Initially, all available memory is kept in a single, large block that is added to the free list. You should use Pintos’s list implementation in lib/kernel/list.c|h for this project. You should read these two files to familiarize yourself with the API. In particular, pay attention to the functions it provides to maintain sorted lists. You will use this implementation in all future Pintos projects.
Here is the interface to your memory allocator (this is from the given file memalloc.h):
/* Initialize memory allocator to use ’length’ bytes of memory at ’base’. */ void mem_init(uint8_t *base, size_t length);
/* Allocate ’length’ bytes of memory. */ void * mem_alloc(size_t length);
/* Free memory pointed to by ’ptr’. */ void mem_free(void *ptr);
/* Return the number of elements in the free list. */ size_t mem_sizeof_free_list(void);
/* Dump the free list. */ void mem_dump_free_list(void);
The mem_init() function initializes your memory allocator. It requires a pointer to a continuous chunk of memory (more later) and the number of bytes in the continuous chunk. The mem_alloc() and mem_free() functions work just like malloc() and free() except that all allocations and deallocations use the global memory specified in the call to mem_init(). Your implementation will need to keep track of all allocations and deallocations. You will maintain a free list for bookkeeping and it should be maintained in increasing memory address order (see below). Initially, the free list will contain one memory block that is the size of the global memory. Each time mem_alloc() or mem_free() are called, the free list should be updated appro- priately. The mem_alloc() function should use a first-fit approach to finding a free block to accommodate a request (see below). If an allocation request cannot be satisfied, mem_alloc() should return NULL. The mem_sizeof_free_list() function should return the number of elements on the free list. The mem_dump_free_list() function should output a human readable list of free blocks. For each block the output should included the start address of the block and the length, in addition each block should appear on a separate line. The format should be:
address length
For example:
0x500120 1048576 0x600120 1048576
Your allocator should use a first-fit strategy. If a request is made to allocate a certain number of bytes, your allocator should look for the first available block that has a size large enough to accommodate the request. If the block is larger than the memory that is requested, it must be split. To split a block, you should shorten the block’s length by the amount you’ll need for the to-be-allocated memory. The to-be-allocated memory comes from the top of the free block. In this way, you do not need to manipulate the free list when splitting
cess them at any given point in time. In the Pintos kernel, you would use lock_acquire()/lock_release() for this purpose. For now, we will use the POSIX thread API which is provided by Linux. You need to use the functions pthread_mutex_lock() and pthread_mutex_unlock(). You will also need to initialize the mutex or mutexes you will be using. To see how to do that, read the man pages for pthread_mutex_lock(P) and pthread_mutex_init(P). These are the only pthread functions you will need to use. We recommend that you first develop and test your allocator with one thread, and then add the necessary protection to pass the multi-threaded part of the test harness. (Tests 1, 2, and 4 are run by a single thread, only Test 3 exercises the allocator concurrently by multiple threads.)
You will work in the pintos/src/prep directory for this assignment. Copy the pintos source tree into a directory of your choice:
$ mkdir cs $ cd cs $ mkdir prj $ prj $ wget -nv http://www.cs.usfca.edu/benson/cs326/prj0/src/pintos.tar.gz 10:16:44 URL:http://www.cs.usfca.edu/~benson/cs326/prj0/src/pintos.tar.gz [230819/230819] -> "pintos.tar.gz.1" [1] $ tar xzf pintos.tar.gz
You must add a file called memalloc.c to that directory that implements the memory allocator’s interface, described in memalloc.h. Please read this file for specifics. memalloc.h also contains struct free_block and struct used_block definitions you may use to represent free and used blocks of memory, respectively. You build the test harness using make. This will build the test_mem executable. ./test_mem will run it.
This assignment will count for 100 points. Unlike for future projects, you are not required to submit a design document with this project. The points break down as follows:
30 points: passing Test 1. 30 points: passing Test 1 and 2. 10 points: passing Test 1, 2, and 3 - (includes proper use of locks for thread safety) 10 points: passing Test 1, 2, 3, and 4. 10 points: proper use of Pintos’s list implementation. 10 points: adherence to coding standards outlined in Pintos project documentation.