Lab 4 - Basic daemon code

Due: Friday, February 1, 2008 at 5:00pm
This lab is worth 10 points.

Get the file daemon.c from http://www.cs.csub.edu/~marc/code/cs376.html. As with prior labs, make sure you also download all supporting files if you are putting this code in a new directory. See Lab 2 for a list of supporting files. Compile the program with the command:

make daemon

The daemon program is a very simple TCP/IP server that will host up to MAX_CHILDREN concurrent clients. It will take whatever is typed in the client application and either print it to screen or optionally print it to file if the -log option is specified. To start daemon use the command:

daemon
OR
daemon -log
This will display the port number to which daemon is bound. Open another terminal and try to telnet to the daemon. For example, if it says it is on port number 19234, the command would be:
telnet localhost 19234
To end the connection, within the telnet application give the keyboard sequence CRTL-]. This causes telnet to suspend the connection and give you a telnet shell which looks like telnet>. From here, you can give the command "close" to terminate the current connection, the command "open <hostname> <portnumber>" to open a new connection (after closing the current one) and the command "quit" to exit.

To kill the daemon process, return to the terminal you were running it in and give the CTRL-C sequence. daemon.c has a signal handler for CTRL-C which will call parent_terminate(). This function cleans up after the process before exiting.

Try running daemon with just one telnet connection at first. Then run daemon with multiple telnet connections at the same time (you will need one terminal for each connection). Pay attention to what is printed to the daemon screen when children connect and disconnect. If running in log mode, you will not see what is typed in the telnet sessions, but you can view the logs once daemon has completed.

Lab Writeup

Answer the following questions as your lab writeup. Email the writeup to me.
  1. Unlike vcrec.c, which could only accept a single connection at a time, daemon.c can accept multiple connections at once. How does it accomplish this?
  2. Which signals will cause daemon.c to exit gracefully via parent_terminate()?
  3. Why does the ctrl-c signal not work to kill the telnet application?
  4. In dialog_with_telnet(), the actual work of sending and receiving data from the children processes is done. How does this code tell when you have terminated the connection?
  5. What is done to clean up a child process/thread upon termination?