CMPS 105: Systems Programming

Programming Assignment #2: Efficient File Copying


Remember: your programming assignment must be turned in online.

The Basics

The goal of this assignment is to get familiar with file I/O and, of course, to gain more familiarity with systems programming, man, system calls, etc.

In this assignment, you are to implement an efficient file copying program. It will be called like this:

copy foo bar

where "foo" is the name of the file to be copied and "bar" is the name of the new file that will be the copy of it.


The Details

To implement your program, you will need the following system calls :creat(), open(), close(), read(), and write(). You may not use fopen(), fclose(), etc.

Use the man pages to read up on how to use these functions. These are defined in section two of the manual, and have the same name as other commands or functions, so you should call man like this: "man -s2 open" to tell it to give you the open description from section 2.

Your program should be as efficient as possible. That means that it should not copy blocks that are all zeroes. Just don't write them, and seek past them to the next block (or past all of the zeroes if there are multiple blocks of them). Unix zero fills files when there are no blocks in the middle.

To test the efficiency of your program, you should create a few files to test it with:

1. file1: a file with 4K of data.

2. file2: a file with 1 MB of data.

3. file 3: a 1 MB file with only 4K of actual data, where 2K is at the front, and 2K is at the end. To create this file you will have to write 2K, then seek to the end of the file minus 2K, and write an additional 2K of data.

Note that we don't care what the data is: just write whatever happens to be in the memory buffer when you create it.

After you have done this, time your copy program on the three files. Hopefully, file1 will be the fastest, file2 will take the longest, and file3 will take about half as long as file3. Report your results in a file called Output and (if true) speculate in your design document on the reason for the relative performance differences. Try copying the files with cp and compare the performance of your file copier with that of cp.

Note: You must check and correctly handle all return values. This means that you need to read the man pages for each function to figure out what the possible return values are, what errors they indicate, and what you must do when you get that error.


What to turn in

Your code, a working makefile, your design document, and your output file. In addition, include a README file to explain anything unusual to the TA — testing procedures, etc.

REMEMBER: Do not submit object files, assembler files, or executables. Every file in the submit directory that could be generated automatically by the compiler or assembler will result in a deduction from your programming assignment grade.


sbrandt@cse.ucsc.edu