Question
Pipe: C program We want to implement the pipe functionality that bash provides for you. Your task is to develop a program called pipe(source code
Pipe: C program We want to implement the pipe functionality that bash provides for you. Your task is to develop a program called pipe(source code in pipe.c) that accepts a series of arguments from command line that include commands, their arguments, and pipe operators (we use @ character as pipe operator to avoid conflict with shell pipes) between the commands. It then runs the programs one by one while redirecting a command's output as input to the next command using pipes very much like what bash does for you. For example, you can run your program with the following 5 arguments:
pipe ls /usr/bin @ wc -l
The above will perform the same as the following command run in bash (printing the number of files in /usr/bin):
ls /usr/bin @ wc -l
Here are more specific details about the expected behavior of your program:
1. Print the following error message if no argument is provided.
Usage: pipe cmd cmd-arg1 ... [ @ cmd cmd-arg1 ... ]
2. Your arguments may include zero, one, or more pipe operators (@).
3. Note that each pipe operator will be separated by at least one space from its surrounding characters. In other words, it will be by its own one of the command line arguments for your program.
4. There has to be commands before and after each pipe operator. Therefore, there will be always one more command than the number of pipe operators. Your program must print the following error message and terminate if the arguments do not follow the correct syntax.
pipe: syntax error near '@' Usage: pipe cmd cmd-arg1 ... [ @ cmd cmd-arg1 ... ]
For example, the following command will have a pipe syntax error:
pipe ls /usr/bin /usr/lib @ wc -l @
5. Note that commands may be followed by as many arguments as needed (zero or more).
6. Errors as results of executing any of the commands should be printed to stderr as normally expected.
7. Any process as a result of running your program should terminate properly (no orphans/zombies).
Hint: You need to implement this program using system calls and functions you learned in IPC lecture such as fork(), execvp(), pipe(), dup(), and dup2().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started