/* |
| ** pipe3.c -- a smartest pipe example |
| */ |
| |
| #include |
| #include |
| #include |
| |
| int main(void) |
| { |
| int pfds[2]; |
| |
| pipe(pfds); |
| |
| if (!fork()) { |
| close(1); /* close normal stdout */ |
| dup(pfds[1]); /* make stdout same as pfds[1] */ |
| close(pfds[0]); /* we don't need this */ |
| execlp("ls", "ls", NULL); |
| } else { |
| close(0); /* close normal stdin */ |
| dup(pfds[0]); /* make stdin same as pfds[0] */ |
| close(pfds[1]); /* we don't need this */ |
| execlp("wc", "wc", "-l", NULL); |
| } |
| |
| return 0; |
| } |
Task 2: Pipe (27pt.) 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 e 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 1s /usr/bin@ wc -1 The above will perform the same as the following command run in bash (printing the number of files in /usr/bin): 1s /usr/bin wc -1 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-argi e cmd cmd-arg1..] 2. Your arguments may include zero, one, or more pipe operators(e) 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 commandle 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-argi e cmd cmd-arg1..] For example, the following command will have a pipe syntax error: pipe 1s /usr/bin /usr/lib e we -1 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: Refer to pipe3.c in inclass12 for a simple example of piping commands. You need to implement this program using system calls and functions you learned in IPC lecture such as fork0. execvpO. pipe0 dup0. and dup20. Task 2: Pipe (27pt.) 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 e 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 1s /usr/bin@ wc -1 The above will perform the same as the following command run in bash (printing the number of files in /usr/bin): 1s /usr/bin wc -1 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-argi e cmd cmd-arg1..] 2. Your arguments may include zero, one, or more pipe operators(e) 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 commandle 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-argi e cmd cmd-arg1..] For example, the following command will have a pipe syntax error: pipe 1s /usr/bin /usr/lib e we -1 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: Refer to pipe3.c in inclass12 for a simple example of piping commands. You need to implement this program using system calls and functions you learned in IPC lecture such as fork0. execvpO. pipe0 dup0. and dup20