180 likes | 194 Views
Learn about measuring time, handling errors, and accessing files in CSE 222 systems programming. Explore timing activities, improving accuracy, and dealing with UNIX errors. Understand stream and descriptor file access methods. Complete assignments to practice file operations and error detection.
E N D
CSE 222Systems Programming • Time, Errors, and • File Access • Dr. Jim Holten
Performance Measuring • Timing Program Activities • Time Measurement Resolution • Improving Accuracy • Activity Types to Time CSE 222 2 01/21/09
Measuring Time Offset • Grab start time • Run activity • Grab end time • Get difference
Grab Wallclock Time • time(2) -- seconds since epoch • gettimeofday(2) -- returns timeval structure • tv_sec -- seconds since epoch • tv_usec -- microseconds after last second • times(2), clock(2) for user/system times
How? • Look at the man pages • Need header files • #include <time.h> for time(2) • #include <sys/time.h> for gettimeofday(2) • Other times are more complex
Resolution and Accuracy • Resolution of timer clocks • Variability in concurrent system activities • Fast activities may be repeated many times, then get the averaged • Longer activities may be repeated several times, then use the minimum
What do you time? • Code algorithms • File I/O delays • Communications delays
UNIX Errors (1) • system calls return null instead of a pointer, or -1 instead of an nonnegative integer • errno(3) -- the error for that system call, it is NOT set if there is no error (retains any old errno value) • strerror(3) -- the standard explanatory string for the errno.
UNIX Errors (2) • #include <errno.h> to access errno • #include <string.h> to access strerror • Note the possible errors for each system call as listed in the system call's man page!
Stream File Access • fopen(3), freopen(3), fclose(3) • fprintf(3), fscanf(3) (formatted string I/O) • fgets(3), fputs(3), fgetc(3), fputc(3) • All use pointers to FILE for the stream • #include <stdio.h>
fopen(), freopen() Parameters • A string for the file path, absolute or relative to the current working directory • A string of access mode characters • r, r+ • w, w+ • a, a+
Descriptor File Access • open(2), creat(2), close(2) • read(2), write(2) • A file descriptor is an "int" • #include <sys/types.h> • #include <sys/stat.h> • #include <fcntl.h>
open() Parameters • file path string • open flags (OR'ed together) • O_CREAT, O_EXCL, O_TRUNC, O_APPEND • mode -- combined with umask for new file permissions (mode & !umask)
Useful Calls • fdopen(3) -- file descriptor to FILE pointer • fileno(3) -- FILE pointer to its file descriptor • feof(3) -- returns nonzero if eof was found • fcntl(2) -- control behavior for file descriptor • stat(2), fstat(2), and lstat(2)
More Useful Calls • chmod(2) – change permissions on file • chown(2) – change file ownership and group • unlink(2) – delete a file • mkdir(2), rmdir(2), chdir(2), getcwd(3)
Assignment MP2 (1) • Write a C program to time how long it takes to fopen(), fgets() once, and fclose() 100 files, printing out the total time in seconds, and compute and print the average time in milliseconds. • Then repeat using only the first (0) file, accessing it 100 times.
Assignment MP2 (2) • Then access file "MP2_Problem", detecting all the possible errors for fopen(), fgets(), and fclose(), printing the error string and errno for any errors found, but continuing to the next operation even if the current operation fails. • The files are all in "~jholten/cs222_files/"
Assignment MP2 (3) • Include the UG and Journal. • Include a single output file with the results of the three tests concatenated into the file. • Include all source code.