1 / 18

CSE 222 Systems Programming

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.

jordangary
Download Presentation

CSE 222 Systems Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 222Systems Programming • Time, Errors, and • File Access • Dr. Jim Holten

  2. Performance Measuring • Timing Program Activities • Time Measurement Resolution • Improving Accuracy • Activity Types to Time CSE 222 2 01/21/09

  3. Measuring Time Offset • Grab start time • Run activity • Grab end time • Get difference

  4. 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

  5. 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

  6. 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

  7. What do you time? • Code algorithms • File I/O delays • Communications delays

  8. 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.

  9. 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!

  10. 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>

  11. 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+

  12. 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>

  13. 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)

  14. 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)

  15. 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)

  16. 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.

  17. 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/"

  18. 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.

More Related