Pipe: C program We want to implement the pipe functionality that bash provides f
ID: 3709061 • Letter: P
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 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:
The above will perform the same as the following command run in bash (printing the number of files in /usr/bin):
Here are more specific details about the expected behavior of your program:
1. Print the following error message if no argument is provided.
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.
For example, the following command will have a pipe syntax error:
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().
Explanation / Answer
Enabling your Bash script to make decisions is extremely useful. Conditional execution allows you to control the circumstances where certain programs are executed based on whether those programs succeed or fail, but you can also construct conditional expressions which are logical statements that are either equivalent totrue or false. Conditional expressions either compare two values, or they ask a question about one value. Conditional expressions are always between double brackets ([[ ]]), and they either use logical flags or logical operators. For example, there are several logical flags you could use for comparing two integers. If we wanted to see if one integer was greater than another we could use -gt, the greater than flag. Enter this simple conditional expression into the command line:
The logical expression above is asking: Is 4 greater than 3? No result is printed to the console so let's check the exit status of that expression.
It looks like the exit status of this program is 0, the same exit status as true. This conditional expression is saying that [[ 4 -gt 3 ]] is equivalent to true, which of course we know is logically consistent, 4 is in fact greater than 3! Let's see what happens if we flip the expression around so we're asking if 3 is greater than 4:
Again, nothing is printed to the console so we'll look at the exit status: