Operating System代写:CS308 Nameless Pipes

Introduction

Nameless pipes可以做很多事情,比如在父子进程中,通过nameless pipes进行父子进程的通信。结合Unix的系统工具,甚至可以将输入输出流通过fd传给子进程。下面的作业便是通过nameless pipes将数据传递给Unix的sort和grep工具。

Requirement

This problem will require you to write two different, but similar, programs that each spawn a child, let the child process load itself with a Linux/UNIX type command (the grep and sort commands will be used in this assignment), and then send data to and retrieve data from the child process. Each of your programs will need to:
Create 2 nameless pipes

  1. Create a child process
  2. The parent will be responsible for managing any data files that the child command will need sent via the pipe
  3. The parent will collect all output from the child command sent through the pipe, process it as required and write required results to the standard output
  4. The parent will terminate with success when the pipe being read from the child command shows EOF
  5. Perform the appropriate channel management with the processes’ standard IO channels and the pipe channels to create a circular connection through the UNIX command
  • standard input to the command child will be sent as output from the parent
  • input to the parent will be sent by the child command as standard output
  • the parent will use the appropriate pipe channels to talk to the child

You must implement your programs to work with the sort and grep commands as follows:

For the sort command

from your parent process, open the file for reading

  1. Read each line of the file and write it into (the parent’s) OUT pipe to the child process that has exec’d sort
  2. Read the file back from the child running sort from the (parent’s) IN pipe, and write the required results to stdout (or a file if you choose)
  3. sort must sort the data sent by the parent first by phone number area code and then by last name within each area code
  4. Your required output will be a summary of the sorted results returned by the child running sort that consists of a listing of each unique area code and the number of people found in the data sent to sort for that code

For the grep command

from your parent process, open the file for reading
Read each line of the file and write it into (the parent’s) OUT pipe to a child process that exec’d grep

  1. The child process running grep must filter its incoming lines, looking for any line that has the string “123” in it
  2. The child process running grep must write back each matching line to the parent process by writing it into the (parent’s) IN pipe
  3. You (the parent) must read back grep’s output from your IN pipe and print out a single number indicating how many lines had the matching string as your report. You must count the lines returned by grep in your parent process (do not have grep count the lines)
  4. You must now repeat this grep exercise using the file

Summary

熟悉父子进程和nameless pipe的话,这个作业难度并不高。不过需要注意的是,对EOF的处理,稍有不慎,程序将卡死在sort和grep中,无法退出。