Assignment 1 - cs201 - Jim Binkley - winter quarter 09 - due in two weeks ------------------------------------------------------------------------- overview: write 2 small programs that use stdio and UNIX low-level system calls, 1. mycat and 2. mycp. These programs will both use stdio (standard i/o) and UNIX system calls to do i/o. Thus the two programs will have two modes of running (if you use the -s switch, the programs should use stdio, if no -s, they should use bare bone UNIX system calls to do i/o. In part 2, there will be a timing exercise with mycp in which you time the program. This does not need to have a runtime switch. The three timing exercises will be: 1. use the gettimeofday(2) system call in your program, 2. use the shell-level time(1) command, and 3. use gprof. Use these three timing modes with the mycp program only, and use them in both -s and no -s modes. The results should be put in a ASCII text report turned in with your code and Makefile. -------------------------------------------------------------- part 1: mycat and mycp basic programs (no timing) program 1: "mycat" syntax: mycat [-s] file1 file2 ... fileN mycat should take any number of files and simply write the output to stdout. So for example if you have file1 hi file2 world file3 hola terra and do this: % mycat file1 file2 file3 The output should be: hi world hola terra mycat should be programmed with two modes depending upon if you use the optional -s switch or not. If -s, use stdio, if not use basic UNIX system calls. -s: use fopen/fclose/fread/fwrite No -s: use open/close/read/write program 2: mycp syntax: % mycp [-s] readfile writefile mycp should always take two file arguments and only two file arguments. The first file should be opened for reading and copied to the second file which should be truncated and written to. Therefore readfile is copied to writefile (and not concatenated). If -s is used use stdio (use fopen/fclose/fread/fwrite) to do the job. Otherwise use UNIX system calls (open/close/read/write). In addition (IMPORTANT), basically always read and write 1 character, and do no more i/o than that with either I/O system. Note that we have to modify mycp in part 2 to finish the assignment, although this will be easy (not done until you add gettimeofday calls). Important: in order to make gprof do the right thing in part 2, we need to wrap the read/write, and fread/fwrite system calls for mycp (only, not the other program). Basically put a function call around those calls; e.g., myread for read, mywrite for write, etc. Here is an example: int myread(int rf, char *buf, int count) { int rc; rc = read(rf, buf, count); return(rc); } int mywrite(int rw, char *buf, int count) { int rc; rc = write(rw, buf, count); return(rc); } ------------------------------------------------------------------ part 2: timing. We want to use time(1), gettimeofday(2), and gprof(1) to investigate the runtime behavior of our program in terms of timing, and how expensive (or how many times) functions in the mycp program are called. 1. simply use the external time command, to time your program; e.g., % time mycp file1 file2 % time mycp -s file1 file2 Put this result in your final report timing section. 2. modify the code in mycp to put a gettimeofday(2) call at the beginning and at the end. This gives you a start time and an end time. Then add a print to show the difference in results. You want to print out both seconds and microseconds say like this: total execution time: S(secs):M(usecs) Again do this for both -s and no -s and put it in your report. 3. now run gprof on both mycp -s and mycp without. Compare the results (you are basically comparing the stdio library calls to direct use of UNIX system calls). Your report should have a paragraph that explains the difference as well as the flat call graph for each of the two runs. ------------------------------------------------------------------ turning in the assignment: 1. print out all needed files and turn them in on turn-in day at class (to me). 2. also email to jrb@cs.pdx.edu, a gzipped tgar archive of your assignment *as an attachment*. E.g, if your files are in: cs201hw1 % tar czvf mytar.tar.gz cs201hw1 Make the subject head: Subject: cs201 hw1 tar archive Turn in C files, a Makefile, and a report in three parts: 1. use the script command to show that you tested your programs (a good job here may improve your grade) 2. include the "flat profile" only of a gprof run for your mycp program. You should show two results, one with -s and one without. ------ sample gprof report -- last part ------------------------------- (This is for a different program - but this is what we want) Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 88.68 0.47 0.47 24049 0.02 0.02 InsertionSort 5.66 0.50 0.03 10 3.00 3.00 Shellsort 3.77 0.52 0.02 69990 0.00 0.00 Merge 1.89 0.53 0.01 10 1.00 47.97 Qsort 0.00 0.53 0.00 422681 0.00 0.00 Swap 0.00 0.53 0.00 105000 0.00 0.00 PercDown etc... etc... 3. include one paragraph that attempts to explain why mycp with system calls has much different profiling/timing results than mycp without system calls. (compare on a BIG FILE the results between mycp -s file1 file2 and mycp -s file1 file2). In other words, what is the timing difference between open/close/read/write and fopen/fclose/fread/fwrite -- and attempt to explain why this timing difference exists. ------------------------------------------------------------------ library/system calls needed for the assignment Note how to use man command. % man 3 fopen % man 2 read part1: fopen(3)/fread(3)/fwrite(3)/fclose(3) open(2)/close(2)/read(2)/write(2) part2: gettimeofday(2) time(1) gprof(1) (note gprof needs -pg with gcc) To run gprof, first compile with -pg and run your program. When you run the program you get a magic file called gmon.out. Then run gprof mycp which will use gmon.out to give you a stats report on your previous mycp run. Note again: due this twice: with -s and without. For example, cc -pg -o mycp mycp.c ./mycp file1 file2 gprof mycp Do NOT do this: gprof mycp file1 file2 as it will make gprof fail.