Project 1A
Terminal I/O and Inter-Process Communication

INTRODUCTION:

In this project, you will build a multi-process telnet-like client and server. Part A of the project (this part) can be broken up into two major steps:

You will be extending the same code in Project 1B, so make sure it is readable and easy to modify.

RELATION TO READING AND LECTURES:

This lab will build on the process and exception discussions in the lecture on processes and exceptions, but it is really about researching and exploiting APIs.

PROJECT OBJECTIVES:

DELIVERABLES:

A single tarball (.tar.gz) containing:

PROJECT DESCRIPTION:

You will write a program that compiles to an executable named lab1a, that accepts the command line argument --shell (explained below).

  1. Study the following manual sections:
  2. Character-at-a-time, full duplex terminal I/O

    Write a program (executable should be called lab1a) to:

    Note: You will surely have your program exit without properly restoring modes, after which your terminal session may seem unusable. Get familiar with the stty sane option of stty(1). If your program exists with your terminal modes in some unusable state, you can usually fix it by typing ^J stty sane ^J, where ^J is a control-J (or linefeed character).

  3. Passing input and output between two processes

    Extend and refactor your program to support a --shell=program argument to pass input/output between the terminal and a shell:

    One trick here is that will not be a strict alternation between input from the keyboard and input from the shell. Either is capable of generating input at any time. If we do a read(2) from the keyboard, we will block until keyboard input becomes available, even if there is input from the shell pipe waiting for us! The poll(2) system call enable us to solve this problem:

    In this stage you will be introducing four significant new elements to your program:

    That is a lot of new non-trivial code, and I don't like trying to debug four new things at the same time. You might find it easier to add and debug these features one at a time:

    1. create the pipes and fork the child process, which replaces its standard input, standard output and standard error with the pipe file descriptors, and simply copies standard input to standard output (until it reaches EOF, a read returns zero).
    2. a new parent process loop reads a character from the keyboard, writes it to the pipe, reads a character from the pipe, and writes it to the display (with the same processing and coonversions you performed in the non-shell case).

      At this point every character should echo twice: once from your keyboard input handler, and again when it is re-played back from the child process. Once this works, you know that data is flowing through both of your pipes.

    3. rewrite the parent-process alternating reads loop to work with polled reads (from the keyboard and pipe), and confirm that is working.
    4. replace the echo loop in the child process with an exec of the shell.

    You should now be able to type shell commands to your program, see them echoed as you type them, and then see the output from the shell (and any command you run under it).

    Because we have disabled all of the normal input processing, the interrupt character (^C) becomes just another character. When your program reads a ^C (0x03) from the keyboard, it should use kill(2) to send a SIGINT to the shell process. Note that the shell will not likely die as an immediate result of receiving this signal, so you should continue processing input from the keyboard to the shell and output from the shell to the screen.

    You may find it easier to tell if your program has recognized a control-C or control-D if, rather than echoing them directly back to the screen, you translate each into a standard graphical representation (echoing a caret in front of the letter: ^C or ^D). The grading program will not look at how these characters are echoed.

    You may find debugging complicated by the fact that it is not obvious which process did or did not receive or generate which character when. If you add a --debug option to your program that enables copious logging (e.g., to stderr) of every processed character and event, you may find that this greatly facilitates the debugging process.

    WARNING:
    As you are testing communication between the parent and sub-process you will surely have situations where the child process (for one reason or another) hangs without exiting. As you try to debug the problem you may have dozens of hung processes in the background. This can become a problem (e.g. when you exceed your quota of maximum allowed processes per user). You should periodically run a process-status command (ps) and see if you have any lab1a (or pty_test) processes running in the background. If so: kill them!

  4. shutdown processing:

    Note that the three normal shut-down scenarios (closing the pipe from the keyboard reader, receiving a SIGPIPE from the keyboard reader, and receiving an EOF from the shell reader) are not mutually independent. It is likely that, no matter how the shut-down is initiated, multiple of these events will occur in a non-deterministic order.

  5. error checking

Summary of exit codes:

SUBMISSION:

Your README file must include lines of the form:

And, if slip days are allowed on this project, and you want to use some, this too must be included in the README file: If, for instance, you wanted to use two slip-days, you would add the following line: Your name, student ID, and email address should also appear as comments at the top of your Makefile and each source file.

Your tarball should have a name of the form lab1a-studentID.tar.gz. You can sanity check your submission with this test script.

We will test it on a departmental Linux server. You would be well advised to test your submission on that platform before submitting it. Depending on the TA policy for your quarter, submissions that do not work on these servers may lose points.

A HISTORICAL NOTE ON CR, LF, AND EOF:

Long ago, when interaction was through mechanical teletypes with moving print-heads and paper rolls, these ASCII codes had the following meanings:

So every line of text ended with <cr><lf> or 0x0D 0x0A. This is still the case in Windows. Other people felt it was archaic foolishness to require two characters to indicate the end of line, and suggested that <lf> or 0x0A (renamed newline) should be all that was required. This is how files are represented in UNIX descendants. But when output is sent to a virtual terminal, the newline is still translated into the two distinct motion characters <cr> and <lf>. Thus:

DOS systems used to put a 0x18 (^Z) as the last character of a file to indicate END OF FILE, while most other operating systems simply ended the data (subsequent reads return nothing).

GRADING:

Points for this project will be awarded:

value feature
Packaging and build (15% total)
5% un-tars expected contents
5% clean build w/default action (no warnings)
3% Makefile has clean and dist targets
2% reasonableness of README contents
Basic Functionality (35% total)
5% correctly detects/reports bad arguments
5% correctly detects/reports system call failures
5% correctly changes console to character-at-a-time, no-echo mode
5% correctly restores terminal modes on exit
5% keyboard input echoed one character at a time
5% keyboard input written to screen one character at a time
5% correct <cr> and <lf> handling
--shell Functionality (50% total)
10% forks a process and execs specified shell
10% correctly passes keyboard input to screen or shell
10% correctly passes output from shell to screen
5% ^D sends EOF to shell
5% ^C sends SIGINT to shell
5% correct <cr> and <lf> handling
5% report shell exit status on exit