View the step-by-step solution to:

Question

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.

  • open: is a Unix system call that opens a given file.
  • read: is a Unix system call that reads a file into a given buffer.
  • close: is a Unix system call that closes a given file.
  • fopen: is a C/C++ standard I/O function that opens a given file.
  • fgetc: is a C/C++ standard I/O function that reads one byte from a file.
  • fread: is a C/C++ standard I/O function that reads a file into a given data structure.
  • fclose: is a C/C++ standard I/O function that closes a give file.

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 timet= "

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

Complete the main() function so that the program runs as specified above. 

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes