In this program, need to implement functions by chaining the standard Unix utility programs. In particular, you are asked to produce a program that runs a sequence of commands. For example,

$ ./combo cmd1 cmd2 cmd3

Assuming cmd1, cmd2, and cmd3 are three separate programs, such as ls, cat, sort, and so on. The first program cmd1 reads from the standard input and produces the standard output, which is piped to the second program cmd2 as its standard input. The second program cmd2 reads from the standard input and writes to the standard output, which is piped to the next program, so on and so forth.

The above example runs the same as:

$ cmd1 | cmd2 | cmd3

The combo program must take at least one programs. Note that you're not asked to implement the individual programs, cmd1, cmd2, and cmd3 as in the above example. You are asked instead to fork processes and use exec (or its variants) to run the programs. You are also asked to use pipe and dup (or dup2) to facilitate communication between the processes (i.e., rewiring the standard input and output for the processes before you exec them).

More Details

Start by checking out the examples in the CProg/pipes directory

For the program, the logic is as follows:

  • you should first check to see if there's only one command in the command list (using argc).
  • If so, it'd be easy, just run the command using 'exec' or its variant.
  • If there are two or more commands, we should create a pipe (recall pipe has two file descriptors associated with it, one for read and one for write).
  • After creating the pipe, one can fork the processes.
  • You can make the child process run the first command and the parent process run another instance of combo for the the rest of the commands. (Yes, you can 'exec' combo but this time with all the command-line arguments except argv[1]).
  • Before you run the commands, make sure you 'wire' the standard output of the child process as the write end of the pipe and the standard input of the parent process as the read end of the pipe. To see how this is done, check the mysort.c program in the cprog/pipes directory.

Here's an example:

./combo ls sort
Makefile
combo
combo.c
combo.o

The program runs 'ls' to list the files in the current directory, which is then sorted. The result is printed to the standard output. Here's another example

./combo cat sort wc < Makefile
10 24 137

The first program is 'cat'. Rather than providing a filename as its argument, we redirect the text file 'Makefile' as its standard input. 'cat' lists the content of the file (given as stdin), which is then sorted, and then counted for lines, words, and characters.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.