Last modified: 2016-10-04
This is a course page of
David Casperson
Associate Professor
Computer Science
University of Northern British Columbia
Score 4 Board image

Further information on the CPSC 101 Project

Pipes

Location

A Pipe class can be found on galaxy.unbc.ca in the directory /export/research/casper/cpsc101/
Pipes/. Directly in that directory can be found Pipe.h and a Pipe.o files.

Support

Under the src subdirectory of that directory can be found the source code used to create the Pipe class, for those that wish to try to compile it with a different compiler or operating system. Warning: I know that the code does not compile using gcc 2.95.

Under the examples subdirectory there are various examples of how to use the Pipe class. As of 2007-02-25, the examples consisted of a simple echo class.

A Silly Opponent

There is a very determinedly simple-minded computer opponent in /export/research/casper/cpsc101/Pipes/examples/Opponents/. This class aborts whenever it doesn't receive a perfectly formed command. Otherwise, it plays in a determinedly stupid fashion.

A Simple Tester

In /export/research/casper/cpsc101/Pipes/examples/tester there is a simple test driver to test your computer opponent for conformity to the communication protocol.

There is complete source code for the tester and a Makefile here. There is also a precompiled test program test1 that takes one command-line argument, the name of a computer opponent to test. For instance, the following commands

  cd /export/research/casper/cpsc101/Pipes/examples/Opponents/
  /export/research/casper/cpsc101/Pipes/examples/tester/test1 dumbOpponent

produces

testQuit passed.
testRestart passed.
testPlayColors passed.
testFirstMove passed.
testSecondMove passed.

This testing isn't definitive, but you may be able to write your own code to add tests to those given.

Class description

class Pipe
{
public:
    explicit Pipe(std::string progname) ;
    ~Pipe() ;
    std::istream * const getInputStream() const ;
    std::ostream * const getOutputStream() const ;
    pid_t getPid() const ;
private: …
} ;

The Pipe class contains the following methods.

Name Meaning
Pipe The constructor begins executing the program named in its string argument as a separate process. This process receives as its standard input (cin) any information written to the output stream of the pipe. Any information that it writes to standard output (cout) is accessible through the input stream of the pipe.

The constructor aborts if it cannot create a pipe, cannot fork a new process, or cannot exec the requested program.

~Pipe The destructor terminates the execution of the process started by the constructor if it hasn't already terminated execution. It also ensures that the input and output streams of the pipe are closed.
getInputStream This method returns a pointer object that can be used to access output of the process started by the constructor. The pointer should be non-null whenever the object exists.
getOutputStream This method returns a pointer object that can be used to supply input to the process started by the constructor. The pointer should be non-null whenever the pipe object exists.
getPid This method returns the process id (an integer) of the program that the pipe is communicating with.

Commentary

Note that every time that a program creates a Pipe object it acquires access to one more istream and one more ostream. Thus a tournament director that has four current Pipe objects has four istreams and four ostreams, not including std::cin, std::cout, or std::cerr.

Note, on the other hand, that a program that is started by a Pipe object does not have direct access to the keyboard or the display through std::cin and std::cout as these are connected directly to the pipe.

Home page Semesters Site Map
go back Winter 2007 go forward
2017-11 other links

Dates
CPSC 101
Syllabus
Old Exams
Labs
Project
David’s Schedule