next up previous
Next: About this document ...

Programming Project Four
Computer Science 4334 -- Fall 2000
Operating Systems

CSCI4334 Assigned: Wednesday October 25, 2000
  Due: Wednesday November 8, 2000

You are to complete the ``Laboratory Exercise: Refining the Shell'' on page 243 of the textbook.

Most of the description in the section are clear. A few issues are discussed in more details here to bring your attention.

  1. The requirement Your program will be able to execute commands in the following form
    $ls | wc
    This phenomena is refered to as pipe. The idea is to send the output of previous command (e.g. ls) as the input of the next command (e.g. wc which stands for word count). The command ls | wc actually counts the number of files in the current directory. UNIX has the capability of supporting many levels of pipe. For example, you can execute the following command
    $cat test.c | grep m | wc
    What does it do? It first types out a C program test.c. The output is sent to the next command grep which searchs for the lines containing letter 'm'. The output of grep is then passed to wc. The complete command counts how many lines in test.c contain the letter 'm'. Supporting multiple levels of pipes is difficult. Your program doesn't have to do that. The requirement is to support one level of pipe.

    Second form of command your program needs to support is to execute the program in the background. This is realized by trailing an '&' at the end of the command. The following command will be executed in the background.
    $ls &

  2. How to redirect I/O: The textbook has much detailed description. The system call dup() has to be used to 'map' the input or output to a file. Each process has a list of file descriptors which are stored in an array. The first element of the array fd[0] is standard input stdin, the second one fd[1] is for standard output stdout, and the third one fd[2] is for standard error stderr. The dup() system call (make sure you read man dup) takes a file descriptor (an integer) as the parameter and returns a new file descriptor that duplicates the old one. The new file descriptor is drawn from the all available descriptors and it is always the lowest one that is not in use. So if the standard input file whose ID is 0 is closed, dup() will return the 0 as the new file descriptor is associated with the file. If the standard output file whose ID is 1 is closed, dup() will return 1 as the new descriptor. The following complete, but short example illustrates the idea.
    1#1

Extra credit (5%) will be given if your program can handle multiple pipes.




next up previous
Next: About this document ...
Meng Xiannong 2006-07-14