Lab Assignment--Pipes


To get the sample and exercise code, please use the following commands in your cs330 directory:
   curl -O -s https://www.labs.cs.uregina.ca/330/Pipe/Lab8.zip
   unzip Lab8.zip
   
(Please do this lab on os1 or os2)
  1. Try out some pipes and filters on the command line.
    Details:
    1. Start with the following text in a file called colorFile.txt (provided in the code given for the lab):
      This is an example file
      testing the use of grep and sed.
      Sed will modify the spelling of color.
      Red color, blue color, green color, orange color.
      Grep will display specific lines.  
      This is used in our pipes lab.
      Pipes provide the user with a synchronized means of
      interprocess communication.
      Two kinds of pipes exist: named or unnamed.
    2. Use the grep command on colorFile.txt to display all the lines containing the word "sed" (not case sensitive, not as part of another word) with the line number appearing in the output. Store the result in a file called sed.txt. Record the command that you used to do this as a comment at the beginning of your question 2 code.

  2. Using the system calls from this lab, your goal is to create a program that executes the following command which will count the number of occurrences of "pipe" in the file and write the results to newFile.txt.
    grep -o -i pipe colorFile.txt | wc -w > newFile.txt

    More details:

    1. Create two argument vectors:
      1. One for grep -o -i pipe colorFile.txt
      2. One for wc -w

      An example argument vector would be:

      char *myargv[]={(char *)"ls", (char *)"-l", NULL};
    2. create a pipe
    3. fork the process
    4. child will call dup2 to hook standard output to one end of the pipe. Then, execute the grep command using execvp
    5. parent will call dup2 to hook standard input to the other end of the pipe.
    6. parent will also call dup2 to hook standard output to the file called newFile.txt Then, execute the wc command using execvp

Sample run for question 2:

% ./pipes
% cat newFile.txt
3 
%

Deliverables (2 files):