Lab 3 - Signals and select()

Due: Friday at 5:00pm

In this lab, we will explore two concepts central to many network programs: signal masks and select(). These concepts form the basis of allowing a program to use full-duplex communication. Download the code select.c to your current directory with the command:

wget http://www.cs.csubak.edu/~melissa/cs376-s11/select.c
Compile and run the code with the commands:
gcc -o select select.c
./select

Part 1: Signals

Signals are notifications sent by the operating system or the user to processes. The signals can affect the behavior of the process. For example, if you are running a program in the foreground of your shell and you type CTRL-C, this sends the SIGINT signal to the process. By default, the process will terminate when it receives this signal. The coder can also set up signal handlers to change the default behavior of the process when it receives certain specific signals.

Read the manual page on signals using the following command:

man 7 signal
At the start of the code, information about the process's current signal mask is printed out. Some signals can either be ignored entirely (blocked) or a signal handler can be set up. The signal handler is a function in the program which processes the signal. Look at the code and see how SIGCHLD and SIGQUIT are blocked while SIGINT is given a signal handler.

Part 2: select()

The purpose of select() is to give the program a way of waiting for I/O events or errors from specified file descriptors or sockets. For example, if the user types something on the keyboard, this will be an event for the stdin file descriptor. If data is waiting on the network socket, i.e. something can be read with recv(), then this would also be an I/O event.

The second part of the program enters a select loop. You can type a string, give a control sequence (CTRL-C for SIGINT, CTRL-Z for SIGSTOP, etc) or do nothing and wait for select() to time out. If you open a second terminal, you can send additional signals to the program using the kill command. The program prints out examples of using the kill command before printing the prompt.

If you type CTRL-Z, you will suspend the process. To bring it back, use the following command:

fg 1
That will resume the process but not actually print any new prompts.

Part 3: Lab Write-Up

Answer the following questions in Moodle. You can use either the text box or upload a plain text, Open Office or PDF document.
  1. What is the purpose of SIGCHLD and SIGQUIT according to the man page?
  2. What is the full list of signals marked in show_signals as important signals? Just give the signals, not their purpose
  3. Pick three signals from Question 2 that you did not describe in Question 1 and describe their purpose.
  4. Look at the man page for sigemptyset and describe its purpose in this code.
  5. Look at the man page for sigprocmask and describe how it is used in in this code.
  6. Look at the source code. How is the SIGINT (CTRL-C) signal handler set up in main()?
  7. How does the code tell select() to watch stdin for activity?
  8. One of the issues with Lab 2 is there was no way to check both the socket and the keyboard for data at the same time, so we could only recode the programs to use half-duplex communication. How would you use concepts explored in this lab to allow full duplex communication between vcrec and vcsend? Describe the general code changes you would need to make, but do not actually code the changes (we will code the changes for next week's lab).