3.2 Interprocess Communication
In C using UNIX system calls, write a program that demonstrates using standard UNIX tools to accomplish small tasks. This program will demonstrate the essence of the redirection the UNIX shell provides.
Your program will have two modes: sort mode and grep mode. Your program should use its first command-line argument to determine which mode it runs in: sort for sort mode and grep for grep mode.
In each mode, your program will (1) create two nameless pipes, (2) create a child process, (3) perform channel management such that the child’s output goes to one pipe and its input comes from the other pipe, (4) execute a standard UNIX command in the child, (5) use the parent to analyze the results, and (6) exit with success when the child finishes writing to its output (which occurs when the child exits.)
In sort mode, your program should use the second command-line argument as the name of a file. This file consists of any number of phone records separated by newlines (\n). A phone record has the form LAST FIRST AREA NUM3 NUM4, where each field does not contain spaces and all fields after and including AREA are only ASCII numeric digits. Your parent should read this file and write each line to the child’s input. Your child should exec the sort command so that the data is sorted first by the AREA field and then by the LAST field. Your parent should analyze the output of the child and one summary line for every unique AREA value, separated by newlines (\n). A summary line has the form AREA COUNT where COUNT is the ASCII representation of the number of phone records with that AREA value.
In grep mode, your program should use the second command-line argument as the name of a file. Your parent should read this file and write each line to the child’s input. Your child should exec the grep command so that only lines with 123 in them are printed. Your parent must print out the number of lines that the child prints back.
Your program should have no other output than mentioned above.
Your repository must contain the following files:
A README file that contains a short write-up of your project. It should discuss the approach you took to solving the problem, it should discuss any problems or issues that still remain, it should discuss any interesting things that you learned in the course of the project. Finally, it should provide the grade you believe the project deserves with a detailed explanation of why. This explanation should almost certainly focus on why you believe the program is correct.
Your README must comment on the timing of the various tests.
This file must be in plain-text. If you need formatting, use Markdown. It must be limited to 80 characters wide, unless you are quoting program output.
Your source code, named a2.c.
Three sort mode test input files, named tiny.sort.in (containing a few records), medium.sort.in (containing a 32K of records), huge.sort.in (containing a 8M of records), that contain input for sort mode.
Three sort mode test output files, named tiny.sort.out, medium.sort.out, huge.sort.out, that contains the expected output for the sort mode tests.
Three grep mode test input files, named tiny.grep.in (containing a few lines), medium.grep.in (containing a 32K of lines), huge.grep.in (containing a 8M of lines), that contain input for grep mode.
Three grep mode test output files, named tiny.grep.out, medium.grep.out, huge.grep.out, that contains the expected output for the grep mode tests.
A Makefile that first builds the program and then tests it by running it in each mode with the appropriate test input and compares the actual output to appropriate test output. The Makefile should time your program.
This assignment will be graded based on its correctness and your ability to articulate why it is correct.