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?