ICS412 - Fall 2009 - Programming Assignment #1 -

Work alone
You are expected to do your own work on all homework assignments. You may (and are encouraged to) engage in general discussions with your classmates regarding the assignments, but specific details of a solution, including the solution itself, must always be your own work. (See the statement of Academic Dishonesty on the course's syllabus.)

What to turn in?
Turn in an archive (.zip, .tar, .tar.gz, .tgz, etc.) that contains: The archive must be turned in via e-mail to henric@hawaii.edu and to altunkay@hawaii.edu with a subject line like "ICS412: PA#1" before 11:59PM on the day the assignment is due.


Question #1: cp and system calls (10pts)
The UNIX cp system program copies one file to another. This is done by reading chunks of bytes from the source into a buffer of some size, and then writing the content of the buffer in the destination. This is done until the entire source has been copied into the destination (at the last iteration the buffer may be only partially full since the size of the source in bytes may not be perfectly divisible by the buffer size). Using a system call tracing facility (i.e., strace on a Linux system, or ktrace on a Max OS X system), and a little bit of ingenuity, determine the size of the buffer used by the cp implementation on the machine you're using. Describe the procedure you used, explain why it does indeed allow you to determine the size of the buffer, and say which buffer size was discovered. Hint: this can be done by hand, but could be easier using a script to discover buffer size somewhat automatically (using Perl, Python, whatever scripting language you like).

Question #2: mycp.c (10 pts)
Implement a program, mycp, that takes three command line arguments: an integer > 0 and two file names (i.e., paths). The program copies the content of the first file into the second file. If a file with the second name already exists, then this file is overwritten. This program should be written using the standard Posix API, which is supported by all Linux Systems. The program should gracefully exit if the first file cannot be opened for reading, or the second file cannot be opened for writing, or if any other error occurs. You can choose to use a high-level API (fopen, fread, fwrite, fclose) or a lower-level API (open, read, write, close). All those system calls are located in the man pages (section 3 for the former, section 2 for the latter). The first argument is used to specify the size of the buffer used for reading/writing.

Question #3: Counting System Calls (5pts)
Trace system calls both for cp and for mycp when copying the same file, with mycp using the same buffer size as cp. Are the number of system calls equal between the two programs? If not, can you tell which extra system calls are placed by cp or by mycp? Try to venture some reason why cp uses more or fewer system calls?


henric@hawaii.edu