CSci351 Introduction to File Processing: Homework 1 Solutions

Due date: Monday, February 11, 2008 in class
Each homework may have a different weight, which depends on its level of difficulty.
Absolutely no copying others' work

  1. Show how to change the permissions on a file myFile so the owner has read, write, and execute permissions, group members have read and execute permission, and others have execute permission.     (05%)
    Ans>
       shell> chmod  751  myFile
  2. Find what value is used to indicate end-of-file in the stdio.h/cstdio of C/C++.     (05%)
    Ans>

  3. Write a C/C++ program to create a file and store a string in it.
    Ans>
       #include  <fstream>
       #include  <iostream>
         using namespace std;
     
       int  main( ) {
         fstream  file;
         file.open( "hw1.txt", fstream::out );
         file << "This is only a test.";
         file.close( );
       }
    Write another C/C++ program to open the file and read the last ten bytes of the string and put it into the variable myString.     (15%)
    Ans>
       #include  <fstream>
       #include  <iostream>
         using namespace std;
     
       int  main( ) {
         fstream  file;
         char     myString[11];
         int      length;
     
         file.open( "hw1.txt", fstream::in );
         file.seekg( 0, ios::end );
         length = file.tellg( );
         file.seekg( length-10, ios::beg ); 
         file >> myString;
         myString[10] = '\0';
         file.close( );
         cout << myString << endl;
       }
  4. Write a C/C++ program to implement the Unix command tail -n, where n is the number of lines from the end of the file to be copied to stdout.     (15%)
    Ans>
       #include  <fstream>
       #include  <iostream>
       #include  <cstring>
         using namespace std;
     
       int  main( int argc, char *args[ ] ) {
         fstream  file;
         char     ch;
         int      i = 0, total = 0, line;
     
         file.open( args[2], fstream::in ); 
         while ( 1 ) {
           if ( ( ch = file.get( ) ) == '\n' )
             total++;
           if ( file.eof( ) )  break;
         }
         file.close( );
     
         file.open( args[2], fstream::in ); 
         strcpy( args[1], args[1]+1 );
         line = atoi( args[1] );
         while ( 1 )
           if ( ( ch = file.get( ) ) == '\n' )
             if ( ++i == total-line )   break;
     
         while ( 1 ) { 
           ch = file.get( ); 
           if ( file.eof( ) )  break;
           cout << ch;
         }
         file.close( );
       }
  5. The ABC 1234 disk drive has the following features: Suppose you have a file with 300,000 200-byte records that you want to store on a 1234 drive. Answer the following questions:
    1. How many blocks can be stored on one track? How many records?     (10%)
      Ans>
        Each block requires 20 × 200 + 150 = 4,150 bytes. One track can hold ⌊20,000/4,150⌋ blocks = 4 blocks = 4 × 20 records = 80 records.

    2. How many cylinders are required to hold the file?
      Ans>
        From the previous answer, we see that one track holds 80 records. So the number of tracks required is ⌈300,000/80⌉ = 3,750. At 40 tracks/cylinder, 3,750 tracks need ⌈3,750/40⌉ = 94 cylinders.

      How much space will go unused due to internal track fragmentation?     (10%)
      Ans>
        Unused space: 4 blocks use 4 × 4,150 = 16,600 bytes, so on each track there are 20,000 – 16,600 = 3,400 bytes. Hence, on 94 cylinders, at 40 tracks per cylinder, 3,400 × 94 × 40 = 12,784,000 bytes go unused.

    3. If the file were stored on contiguous cylinders and if there were no interference from other processing using the disk drive, the average seek time for a random access of the file would be about 10.0 msec and the time to read one track is 12.0 msec. Use this rate to compute the average time needed to access one record randomly.     (10%)
      Ans>
        Approximate access time = seek time + rotational delay + transfer time
        Seek time = 10.0 msec
        Rotational delay = 12.0 / 2 = 6.0 msec
        Transfer time = 12.0 × (4,150/20,000) = 2.49 msec
        So approximate access time = 10.0 + 6.0 + 2.49 = 18.49 msec

  6. Consider a mailing-list file with 1,500,000 100-byte records. The file is to be backed up on a tape drive which has the following features: Answer the following questions.
    1. How many extra records could be accommodated on a 4800-foot tape?     (10%)
      Ans>
        Number of blocks: n = ⌈1,500,000/40⌉ = 37,500 blocks
        Block size: 40 × 100 = 4,000 bytes
        Tape density : 6250 bytes per inch
        Physical block length: b = 4,000 / 6250 = 0.64 inches
        Gap size: g = 0.5 inches
        Space requirement = n × (b + g) = 37,500 × (0.64 + 0.5) = 42,750 inches = 3,562.5 feet
        Extra space: 4,800 - 3,562.5 = 1,237.5 feet = 14,850 inches
        14,850 inches accommodate ⌊14,850 / (0.64 + 0.5)⌋ = 13,026 blocks = 521,040 records

    2. What is the nominal transmission rate?     (05%)
      Ans>
        tape density (bpi) × tape speed (ips) = 6,250 × 300 = 1,875,000 bytes/sec = 1,875 kilobytes/sec

    3. How long would it take to read one block, including the gap?
      Ans>
        From Question 6.a, we know that one block, including the gap, is 0.64 + 0.5 = 1.14 inches.
        At 300 inches per second, one block can be read in 1.14 / 300 = 0.0038 seconds = 3.8 msec.

      What would the effective transmission rate be?
      Ans>
        effective recording density (bpi) × tape speed(ips) =
        [(4,000 bytes/block) / (1.14 inches/block)] × 300 ips = 1,052,632 bytes/sec = 1,053 kilobytes/sec

      How long would it take to read the entire file?     (15%)
      Ans>
        37,500 blocks/file × 0.0038 seconds/block = 142.5 seconds