17.5 Single Task with No Input

Team-FLY

This part of the project uses a single task that has no input to allow testing of the code to create the task and the pipes for communication without the added complication of monitoring multiple file descriptors for input. The task outputs ASCII text rather than packets.

The dispatcher reads a single NEWTASK packet from standard input, creates the appropriate pipes, and forks the child that executes the task. The dispatcher then monitors the readfd pipe file descriptor for output from the task and forwards what it reads as DATA packets on standard output. When the dispatcher encounters an end-of-file on readfd , it waits for the child task to exit and then exits.

Implement the NTPVM dispatcher as described above. The dispatcher does the following.

  1. Read a packet from standard input, using getpacket . If the packet is not a NEWTASK packet, then exit after outputting an error message.

  2. Create a pipe for communication with a child task.

  3. Fork a child to execute the command given in the NEWTASK packet of step 1. The child should redirect standard input and output to the pipe and close all pipe file descriptors before executing the command. Use the makeargv function of Program 2.2 on page 37 to construct the argument array in the child. If an error occurs, the child just exits after printing an informative message.

  4. Have the parent close all unneeded pipe descriptors so that the parent can detect end-of-file on readfd .

  5. Wait for output from the child on readfd . For this part of the assignment, the child will be executing standard UNIX commands. Assume that the child outputs only text. The dispatcher reads the child task's output from readfd , wraps this output in a DATA packet, and sends the packet to standard output by calling putpacket .

  6. If getpacket returns an error, assume that this is an end-of-file. Close the readfd and writefd descriptors for the task. Send a DONE packet to standard output identifying the task and exit.

The dispatcher should liberally use standard error or the remote logging facility to display informative messages about what it is doing. For example, when it receives something from readfd , the dispatcher should display information about the source task, the number of bytes read and the message read. It is worthwhile to invest time in designing a readable layout for the informative messages so that all the relevant information is available at a glance.

Test the program by using ls -l as the command to be executed.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net