Question

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 pipes on the command line
pipes provide the user with a synchronized means of
interprocess communication.
Two kinds of pipes exist: named or unnamed.

2. Use the cat command on lab.txt. Pipe the result to a sed command and change the word "pipes" to "all 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 -n lab.txt | tee lab~.txt
would display lab.txt to standard output with line numbers and lab~.txt would be a copy of lab.txt with line numbers.

1. Use unnamed pipes to create your own version of tee called mytee, equivalent to the above cat call

2. At the command line you will type the following to be equivalent to the above cat call: mytee lab.txt lab~.txt

3. More details:
a. Check that you have 3 command line arguments.
i. argv[1] will be the file used with cat -n.
ii. argv[2] will be the file you write to
b. create a pipe
c. fork the process
d. child will:
i. call dup2 so that any writes to standard output will go instead to the write end of the pipe.
ii. Then, execute the cat -n command using execl
e. parent will:
i. open argv[2] for writing
ii. read from the pipe until their are no characters left.
iii. write to stdout
iv. write to the file descriptor associated with argv[2]
f. 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

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

// Question 1:
// cat lab.txt | sed 's/pipes/all pipes/' >labnew.txt

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

main(int argc, char *argv[]){
   int f_des[2];
   int fd_out;
   char ch;

   if (argc!=3){
      fprintf(stderr, "Error: not enough arguments\n");
      exit(1);
   }

   if (pipe(f_des)==-1){
      perror("Pipe");
      exit(2);
   }

   switch (fork()){
      case -1: // Error
perror("Fork");
exit(3);

      case 0:   // Child...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Get help from a qualified tutor
Live Chats