Table Of Contents
Previous Section Next Section

Program 12: Hurry Up and Wait

The code on which this program is based was written by a senior system programmer at a company I worked at a long time ago.

It was designed to send data over a serial line. Although the serial line was capable of doing 960 characters per second, we were lucky to get 300 characters a second.

Why?

  1 /************************************************
  2  * send_file -- Send a file to a remote link    *
  3  * (Stripped down for this example.)            *
  4  ************************************************/
  5 #include <iostream>
  6 #include <fstream>
  7 #include <stdlib.h>
  8
  9 // Size of a block
 10 const int BLOCK_SIZE = 256;
 11
 12 /************************************************
 13  * send_block -- Send a block to the output port*
 14  ************************************************/
 15 void send_block(
 16     std::istream &in_file,   // The file to read
 17     std::ostream &serial_out // The file to write
 18 )
 19 {
 20     int i;      // Character counter
 21
 22     for (i = 0; i < BLOCK_SIZE; ++i) {
 23         int ch; // Character to copy
 24
 25         ch = in_file.get();
 26         serial_out.put(ch);
 27         serial_out.flush();
 28     }
 29 }
 30
 31 int main()
 32 {
 33     // The input file
 34     std::ifstream in_file("file.in");
 35
 36     // The output device (faked)
 37     std::ofstream out_file("/dev/null");
 38
 39     if (in_file.bad())
 40     {
 41         std::cerr <<
 42             "Error: Unable to open input file\n";
 43         exit (8);
 44     }
 45
 46     if (out_file.bad())
 47     {
 48         std::cerr <<
 49             "Error: Unable to open output file\n";
 50         exit (8);
 51     }
 52
 53     while (! in_file.eof())
 54     {
 55         // The original program output
 56         // a block header here
 57         send_block(in_file, out_file);
 58         // The original program output a block
 59         // trailer here. It also checked for
 60         // a response and resent the block
 61         // on error
 62     }
 63     return (0);
 64 }

(Next Hint 183. Answer 65.)

Table Of Contents
Previous Section Next Section