CSS 503
Lab Work 3: Unix and Standard I/Os

Professor: Munehiro Fukuda
Lab work date: See the syllabus


1. Purpose

This laboratory work intends to familiarize you with Unix and C/C++ Standard I/Os. You will code a program that reads a given file using Unix read() system call as well as C/C++ fgetc()/fread() functions.

2. System Calls

You will use the following Unix and C/C++ Standard I/O functions. Check the specification of each function using man.

3. Statement of Work

The following code is a template for measuring time elapsed to read a given file with Unix read() as well as that elapsed to read the same file with C/C++ Stardard's fread(). The program reads a file name and the number of bytes to read per read( ) or fread( ).
   #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().

4. What to Turn in

Turn in the following materials at the end of class:
  1. Your lab3.cpp
  2. Your execution output
If your time runs out, you may submit it together with programming assignment 3.