CSS 503
Lab Work 3: Unix and Standard I/Os
Professor: Munehiro Fukuda
Lab work date: See the syllabus
#include <fcntl.h> // open #include <unistd.h> // read #include <sys/types.h> // read #include <sys/uio.h> // read #include <stdio.h> // fopen, fread #include <sys/time.h> // gettimeofday #include <iostream> // cout, cerr, endl; using namespace std; struct timeval start, end; // maintain starting and finishing wall time. void startTimer( ) { // memorize the starting time gettimeofday( &start, NULL ); } void stopTimer( char *str ) { // checking the finishing time and computes the elapsed time gettimeofday( &end, NULL ); cout << str << "'s elapsed time\t= " << ( ( end.tv_sec - start.tv_sec ) * 1000000 + (end.tv_usec - start.tv_usec ) ) << endl; } int main( int argc, char *argv[] ) { // validate arguments if ( argc != 3 ) { cerr << "usage: lab3 filename bytes" << endl; return -1; } int bytes = atoi( argv[2] ); if ( bytes < 1 ) { cerr << "usage: lab3 filename bytes" << endl; cerr << "where bytes > 0" << endl; return -1; } char *filename = argv[1]; char *buf = new char[bytes]; // unix i/o int fd = open( filename, O_RDONLY ); if ( fd == -1 ) { cerr << filename << " not found" << endl; return -1; } startTimer( ); while( read( fd, buf, bytes ) > 0 ); stopTimer( "Unix read" ); close( fd ); // standard i/o // write the same program as unix i/o but use fopen(), fgetc(), fread(), and fclose( ) // use fgetc() if bytes == 1 return 0; }The following shows execution outputs when reading a file by 512, 1024, 2048, and 2096 bytes.
[css503@uw1-320-18 lab3]$ ./lab3 hamlet.txt 512 Unix read's elapsed time = 969 Standar fread's elapsed time = 230 [css503@uw1-320-18 lab3]$ ./lab3 hamlet.txt 1024 Unix read's elapsed time = 528 Standar fread's elapsed time = 219 [css503@uw1-320-18 lab3]$ ./lab3 hamlet.txt 2048 Unix read's elapsed time = 358 Standar fread's elapsed time = 219 [css503@uw1-320-18 lab3]$ ./lab3 hamlet.txt 4096 Unix read's elapsed time = 245 Standar fread's elapsed time = 222 [css503@uw1-320-18 lab3]$Complete the main() function so that the program runs as specified above. When the 2nd argument, (i.e., #bytes to read) is 1, you must use fgetc().