CS 321 Spring 2013  >  Assignment 4

# CS 321 Spring 2013 Assignment 4

Assignment 4 is due at 5 p.m. Tuesday, April 4. It is worth 25 points.

## Procedures

E-mail answers to the exercises below to ggchappell@alaska.edu, using the subject “SA4”.

• Your answers should consist of three files: mapalloc.h and mapalloc.cpp, from Exercise A, and randacc.cpp, from Exercise B. These files (or a single archive file containing them) should be attached to your e-mail message.
• Be sure to include your name in your e-mail.
• I may not read your homework e-mail immediately. If you wish to discuss the assignment (or anything else) with me, send me a separate message with a different subject line.

## Exercises (25 pts total)

### Exercise A — Simple Memory Manager

#### Purpose

In this exercise, you will write simple memory allocation & deallocation functions, using mmap to get the memory.

#### Instructions

Write a C++ package to do memory allocation and freeing, much like the standard library functions malloc and free.

• Call your functions mapAlloc and mapFree. Prototype them in the header mapalloc.h, and implement them in the source file mapalloc.cpp.
• mapAlloc takes a std::size_t, indicating a number of bytes to be allocated. It returns a (void *). The function attempts to allocated a block of (at least) the given size. The return value is a pointer to the allocated block, if the allocation was successful; otherwise, it is NULL.
• mapFree takes a (void *), which is a pointer to a block previously allocated with mapAlloc. It returns nothing. The function deallocates the given block.
• Allocation must be done using mmap; deallocation must be done using munmap.
• If we do
char * p = (char *)mapAlloc(n);
and the result is not NULL, then the bytes with addresses p through p+n-1 must be allocated memory with both read and write access. Furthermore, successive calls to mapAlloc must return pointers to distinct, nonoverlapping blocks.
• Function mapFree may assume that it is given a pointer that was previously returned by mapAlloc, and that mapFree will never be called twice with the same pointer. So you do not need to test for these conditions.
• Your package must not include a main function.
• You may include other helper functions, if you wish, but do not prototype these in the header.

#### Notes

• Function munmap takes the size of the block to deallocate, as a parameter. However, mapFree is not given the size of the block. So you will need to store this information somewhere. How you store it, is up to you, but be intelligent about it.

### Exercise B — Memory-Mapped Random-Access File

#### Purpose

In this exercise, you will write a program that does random-access file I/O using mmap.

#### Instructions

Write a complete C++ program to do random-access I/O on a file. The file will contain 100 records, each holding an integer. The user is repeated allowed the choice of reading and printing the value in a specified record, or writing a user-provided value to a specified record. Data written by the program should be available for reading the next time the program is executed.

• The source for your program should be in file randacc.cpp.
• Your program should present a menu allowing the user the choice of reading or writing a record, or quitting the program. Once an operation (other than quitting) is performed, the menu should be presented again.
• The read option asks the user for the index of a record (a number from 0 to 99) and prints the value found in the appropriate record in the file.
• The write option asks the user for the index of a record and an integer to be written (legal values are up to you, but there must be more than one legal value). It then writes the value to the appropriate record in the file.
• File I/O must be done using mmap. It must be random-access, meaning that the program computes the location of a record and reads/writes there, without reading previous records.
• This means that each record must have a fixed length, in bytes.
• When the program is run, if the file does not exist, then it should be created, with the appropriate length.
• If the file already exists when the program is run, then it should not be erased, so that data from a previous run may be read.
• The user interface must be reasonably friendly. Tell the user what is going on, and what values may be entered. When out-of-range values are entered, indicate this, and do not perform the read/write operations.

#### Notes

• Things not specified are up to you. You may give your data file any name you wish. You may choose record lengths and storage format. There are no requirements for internal function names (but do follow the coding standards).

### Further Notes on the Assignment

• If you are using Windows, then function mmap should be available under Cygwin. Alternatively, you can write your code using NetRun; however, NetRun will not allow the file access required in Exercise B.
• These functions are the kind of thing I would normally write a test program for. However, there is no way that I can do this (think about it). You will need to test your code yourself. Please do.
• Coding standards are as in Assignment 2.

CS 321 Spring 2013: Assignment 4 / Updated: 27 Mar 2013 / Glenn G. Chappell / ggchappell@alaska.edu