CSC 357 Lab 7
Getting Started with I/O redirection and
pipes



ISSUED: Monday, 21 May 2007
DUE: On or before 11:59:59PM Tuesday 29 May, via handin on falcon/hornet
POINTS POSSIBLE: 100
WEIGHT: 2% of total class grade
READING: Stevens Section 15.2, Lecture Notes Week 8, appropriate man pages

Task:

This lab is about using system functions to accomplish piping and file redirection in a C program. Specifically, you are writing a C program that performs the same actions, and produces the same results, as the following shell commands:
nwc < IN_FILE | sgrep "out of" > OUT_FILE1
nwc < IN_FILE | grep "out of" > OUT_FILE2
diff OUT_FILE1 OUT_FILE2
where IN_FILE, OUT_FILE1, and OUT_FILE2 are arbitrary file names. The point of these shell commands is to run the nwc and sgrep programs, and compare the results of sgrep with UNIX grep.

The name of the C program you are writing is "lab7.c". It takes three command-line arguments for the three files used above, and performs the actions of the shell commands. For example, running

lab7 lab7.c out1 out2
produces the same results as the shell commands
nwc < lab7.c | sgrep "out of" > out1
nwc < lab7.c | grep "out of" > out2
diff out1 out2

Performing the same actions as the shell commands means the following for the lab7 program:

  1. It must use fork and exec to run the programs nwc, sgrep, and diff.
  2. It must use the dup2 system call to perform the redirection of stdin and stdout.
  3. It must use the pipe system call to handle input/output between nwc and sgrep.
  4. It must have the output of diff go to stdout.

The program must run under the following constraints:

  1. It cannot use the system function.
  2. It cannot create any temporary files, i.e., it cannot create any files other than the two outputs specified above.

When lab7 runs, you can assume the executables sgrep and nwc are in the same working directory where lab7 is running. For this purpose, you can copy the executables from the following two locations on hornet:

357/solutions/programs/1/sgrep
357/solutions/programs/2/nwc

Deliverable

The program lab7.c.

Scoring Details

33 1/3 points for each of the three shell commands

Collaboration Allowed

Collaboration with a lab partner IS allowed, but each partner must turn a copy of the work.

How to Submit the Deliverables

Submit the program via handin.


index | lectures | labs | programs | handouts | solutions | examples | documentation | bin