Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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     // open                                                                     

   #include    // read                                                                      

   #include // read                                                                     

   #include   // read                                                                      

   #include     // fopen, fread                                                             

   #include  // gettimeofday                                                             

   #include    // 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.

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

include iostream include sys timeh include sys typesh include sys stath include fcntl h include unis... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Java, Enhanced Early Objects

Authors: Cay S Horstmann

7th Edition

1119499097, 9781119499091

More Books

Students also viewed these Operating System questions