Laboratory 9

Objectives

  1. Learn to use asynchronous IO using the Unix/Posix funtions.
  2. Understand how asynchronous IO can be used to greatly improve the properties of network programs.

Preparation

In Lab Activities

At this point in time, you have most of the tools you need to do complex programs, except asynchronous IO. This is IO that cannot be specifically timed. For example, if you write a server that opens two sockets to two different clients, either one might send a message a some point in time. If you execute a recv on one of them, you might block forever waiting, while the other one is sending you data. Asynchronous IO allows you to specify that you are interested in any IO and then when something happens, you decide if you want to process the request or not.

Example programs for a client and server pair demonstrate a case where the client handles data from both the keyboard and the socket open to the server asynchronously. Download the two programs, compile them and get them working. They do nothing useful. Then modify the server to handle connections from two separate clients (not using a fork, just by handling requests from both) asynchronously.

There seem to be a lot of calls and different types of things going on here, but it really isn't that bad. Any socket that wants to do asynchronous IO is handled the same way. This program uses select to handle the IO rather than a signal handler. This is the preferred way to do this in modern operating systems because signal handling is both tricky and relatively high overhead. Note that you do have to define an alternative to the default handling for an IO signal, which is to abort the program. All in all, this process is not as clean as you would like, but that's the way it is.