Lab Assignment--Pipes


(Please do this lab in LINUX)

Part 1- Command Line Pipes and Filters

Try out some pipes and filters on the command line.

  1. Copy the following text into a file called lab.txt.
    This is an example file testing the use of 
    peeps on the command line. peeps provide the 
    user with a synchronized means of interprocess 
    communication.  
    Two kinds of peeps exist: named or unnamed.
  2. Use the cat command on lab.txt. Pipe the result to a sed command and change all instances of the word "peeps" to "pipes". Store the results in a file called labnew.txt
  3. Record the command that you used to do this. Save this command in a comment on the first line of your Part 2 code.

Part 2 - System Calls for Pipes

Most Unix systems include a utility program called tee that copies standard input to standard output and a file descriptor passed on the command line. For instance,

% cat -b lab.txt | tee lab~.txt 
would display lab.txt to standard output with line numbers on nonempty lines and lab~.txt would be a copy of lab.txt with line numbers on nonempty lines.

  1. Use unnamed pipes to create your own version of tee called mytee, equivalent to the above cat command
  2. At the command line you will type the following to be equivalent to the above cat command:
  3. mytee lab.txt lab~.txt
  4. More details:
    1. Check that you have 3 command line arguments.
      1. argv[1] will be the file used with cat -b.
      2. argv[2] will be the file you write to
    2. create a pipe
    3. fork the process
    4. child will:
      1. call dup2 so that any writes to standard output will go instead to the write end of the pipe.
      2. Then, execute the cat -b command using execl
    5. parent will:
      1. open argv[2] for writing
      2. read from the pipe until there are no characters left.
      3. write to stdout
      4. write to the file descriptor associated with argv[2]
      Note: do not use exec for the parent
    6. don't forget to handle the error situation of a fork

Sample run for question 2:

% cat lab6.txt
Signals Records
Cs330 Midterm

Door
Cat

AI
Pipes Rule
Management Information Systems
Child Parent GrandParent
% mytee lab6.txt lab6~.txt
     1  Signals Records
     2  Cs330 Midterm

     3  Door
     4  Cat

     5  AI
     6  Pipes Rule
     7  Management Information Systems
     8  Child Parent GrandParent
% cat lab6~.txt
     1  Signals Records
     2  Cs330 Midterm

     3  Door
     4  Cat

     5  AI
     6  Pipes Rule
     7  Management Information Systems
     8  Child Parent GrandParent


Deliverables:

Submit 2 files to URCourses

  1. mytee.cpp (code)
  2. Script containing the following commands:
    1. ls -l (Goal: show that lab6.txt exists, but lab6~.txt does not)
    2. cat lab6.txt (Goal: display the contents of the file with no line numbers)
    3. mytee (Result: error message - not enough arguments)
    4. mytee lab6.txt (Result: error message - not enough arguments)
    5. mytee lab6.txt lab6~.txt (Result: will print to the screen and create and write to lab6~.txt)
    6. cat lab6~.txt (Goal: show what is in the file)

Notes