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:

This assignment will be graded based on its correctness and your ability to articulate why it is correct.